@supabase/postgrest-js
Advanced tools
Comparing version 1.17.2 to 1.17.3
@@ -9,3 +9,3 @@ import { SimplifyDeep } from '../types'; | ||
*/ | ||
export declare type ParseQuery<Query extends string> = string extends Query ? GenericStringError : ParseNodes<EatWhitespace<Query>> extends [infer Nodes extends Ast.Node[], `${infer Remainder}`] ? EatWhitespace<Remainder> extends '' ? SimplifyDeep<Nodes> : ParserError<`Unexpected input: ${Remainder}`> : ParseNodes<EatWhitespace<Query>>; | ||
export declare type ParseQuery<Query extends string> = string extends Query ? GenericStringError : ParseNodes<EatWhitespace<Query>> extends [infer Nodes, `${infer Remainder}`] ? Nodes extends Ast.Node[] ? EatWhitespace<Remainder> extends '' ? SimplifyDeep<Nodes> : ParserError<`Unexpected input: ${Remainder}`> : ParserError<'Invalid nodes array structure'> : ParseNodes<EatWhitespace<Query>>; | ||
/** | ||
@@ -23,5 +23,5 @@ * Notes: all `Parse*` types assume that their input strings have their whitespace | ||
declare type ParseNodesHelper<Input extends string, Nodes extends Ast.Node[]> = ParseNode<Input> extends [ | ||
infer Node extends Ast.Node, | ||
infer Node, | ||
`${infer Remainder}` | ||
] ? EatWhitespace<Remainder> extends `,${infer Remainder}` ? ParseNodesHelper<EatWhitespace<Remainder>, [...Nodes, Node]> : [[...Nodes, Node], EatWhitespace<Remainder>] : ParseNode<Input>; | ||
] ? Node extends Ast.Node ? EatWhitespace<Remainder> extends `,${infer Remainder}` ? ParseNodesHelper<EatWhitespace<Remainder>, [...Nodes, Node]> : [[...Nodes, Node], EatWhitespace<Remainder>] : ParserError<'Invalid node type in nodes helper'> : ParseNode<Input>; | ||
/** | ||
@@ -35,14 +35,8 @@ * Parses a node. | ||
*/ | ||
declare type ParseNode<Input extends string> = Input extends '' ? ParserError<'Empty string'> : Input extends `*${infer Remainder}` ? [Ast.StarNode, EatWhitespace<Remainder>] : Input extends `...${infer Remainder}` ? ParseField<EatWhitespace<Remainder>> extends [ | ||
infer TargetField extends Ast.FieldNode, | ||
`${infer Remainder}` | ||
] ? [{ | ||
declare type ParseNode<Input extends string> = Input extends '' ? ParserError<'Empty string'> : Input extends `*${infer Remainder}` ? [Ast.StarNode, EatWhitespace<Remainder>] : Input extends `...${infer Remainder}` ? ParseField<EatWhitespace<Remainder>> extends [infer TargetField, `${infer Remainder}`] ? TargetField extends Ast.FieldNode ? [{ | ||
type: 'spread'; | ||
target: TargetField; | ||
}, EatWhitespace<Remainder>] : ParserError<`Unable to parse spread resource at \`${Input}\``> : ParseIdentifier<Input> extends [infer NameOrAlias, `${infer Remainder}`] ? EatWhitespace<Remainder> extends `::${infer _}` ? ParseField<Input> : EatWhitespace<Remainder> extends `:${infer Remainder}` ? ParseField<EatWhitespace<Remainder>> extends [ | ||
infer Field extends Ast.FieldNode, | ||
`${infer Remainder}` | ||
] ? [Omit<Field, 'alias'> & { | ||
}, EatWhitespace<Remainder>] : ParserError<'Invalid target field type in spread'> : ParserError<`Unable to parse spread resource at \`${Input}\``> : ParseIdentifier<Input> extends [infer NameOrAlias, `${infer Remainder}`] ? EatWhitespace<Remainder> extends `::${infer _}` ? ParseField<Input> : EatWhitespace<Remainder> extends `:${infer Remainder}` ? ParseField<EatWhitespace<Remainder>> extends [infer Field, `${infer Remainder}`] ? Field extends Ast.FieldNode ? [Omit<Field, 'alias'> & { | ||
alias: NameOrAlias; | ||
}, EatWhitespace<Remainder>] : ParserError<`Unable to parse renamed field at \`${Input}\``> : ParseField<Input> : ParserError<`Expected identifier at \`${Input}\``>; | ||
}, EatWhitespace<Remainder>] : ParserError<'Invalid field type in alias parsing'> : ParserError<`Unable to parse renamed field at \`${Input}\``> : ParseField<Input> : ParserError<`Expected identifier at \`${Input}\``>; | ||
/** | ||
@@ -61,6 +55,3 @@ * Parses a field without preceding alias. | ||
*/ | ||
declare type ParseField<Input extends string> = Input extends '' ? ParserError<'Empty string'> : ParseIdentifier<Input> extends [infer Name, `${infer Remainder}`] ? Name extends 'count' ? ParseCountField<Input> : Remainder extends `!inner${infer Remainder}` ? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
`${infer Remainder}` | ||
] ? [ | ||
declare type ParseField<Input extends string> = Input extends '' ? ParserError<'Empty string'> : ParseIdentifier<Input> extends [infer Name, `${infer Remainder}`] ? Name extends 'count' ? ParseCountField<Input> : Remainder extends `!inner${infer Remainder}` ? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [infer Children, `${infer Remainder}`] ? Children extends Ast.Node[] ? [ | ||
{ | ||
@@ -73,6 +64,3 @@ type: 'field'; | ||
Remainder | ||
] : CreateParserErrorIfRequired<ParseEmbeddedResource<EatWhitespace<Remainder>>, `Expected embedded resource after "!inner" at \`${Remainder}\``> : EatWhitespace<Remainder> extends `!left${infer Remainder}` ? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
`${infer Remainder}` | ||
] ? [ | ||
] : ParserError<'Invalid children array in inner join'> : CreateParserErrorIfRequired<ParseEmbeddedResource<EatWhitespace<Remainder>>, `Expected embedded resource after "!inner" at \`${Remainder}\``> : EatWhitespace<Remainder> extends `!left${infer Remainder}` ? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [infer Children, `${infer Remainder}`] ? Children extends Ast.Node[] ? [ | ||
{ | ||
@@ -84,6 +72,6 @@ type: 'field'; | ||
EatWhitespace<Remainder> | ||
] : CreateParserErrorIfRequired<ParseEmbeddedResource<EatWhitespace<Remainder>>, `Expected embedded resource after "!left" at \`${EatWhitespace<Remainder>}\``> : EatWhitespace<Remainder> extends `!${infer Remainder}` ? ParseIdentifier<EatWhitespace<Remainder>> extends [infer Hint, `${infer Remainder}`] ? EatWhitespace<Remainder> extends `!inner${infer Remainder}` ? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
] : ParserError<'Invalid children array in left join'> : CreateParserErrorIfRequired<ParseEmbeddedResource<EatWhitespace<Remainder>>, `Expected embedded resource after "!left" at \`${EatWhitespace<Remainder>}\``> : EatWhitespace<Remainder> extends `!${infer Remainder}` ? ParseIdentifier<EatWhitespace<Remainder>> extends [infer Hint, `${infer Remainder}`] ? EatWhitespace<Remainder> extends `!inner${infer Remainder}` ? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children, | ||
`${infer Remainder}` | ||
] ? [ | ||
] ? Children extends Ast.Node[] ? [ | ||
{ | ||
@@ -97,6 +85,6 @@ type: 'field'; | ||
EatWhitespace<Remainder> | ||
] : ParseEmbeddedResource<EatWhitespace<Remainder>> : ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
] : ParserError<'Invalid children array in hint inner join'> : ParseEmbeddedResource<EatWhitespace<Remainder>> : ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children, | ||
`${infer Remainder}` | ||
] ? [ | ||
] ? Children extends Ast.Node[] ? [ | ||
{ | ||
@@ -109,6 +97,3 @@ type: 'field'; | ||
EatWhitespace<Remainder> | ||
] : ParseEmbeddedResource<EatWhitespace<Remainder>> : ParserError<`Expected identifier after "!" at \`${EatWhitespace<Remainder>}\``> : EatWhitespace<Remainder> extends `(${infer _}` ? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
`${infer Remainder}` | ||
] ? [ | ||
] : ParserError<'Invalid children array in hint'> : ParseEmbeddedResource<EatWhitespace<Remainder>> : ParserError<`Expected identifier after "!" at \`${EatWhitespace<Remainder>}\``> : EatWhitespace<Remainder> extends `(${infer _}` ? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [infer Children, `${infer Remainder}`] ? Children extends Ast.Node[] ? [ | ||
{ | ||
@@ -120,3 +105,3 @@ type: 'field'; | ||
EatWhitespace<Remainder> | ||
] : ParseEmbeddedResource<EatWhitespace<Remainder>> : ParseNonEmbeddedResourceField<Input> : ParserError<`Expected identifier at \`${Input}\``>; | ||
] : ParserError<'Invalid children array in field'> : ParseEmbeddedResource<EatWhitespace<Remainder>> : ParseNonEmbeddedResourceField<Input> : ParserError<`Expected identifier at \`${Input}\``>; | ||
declare type ParseCountField<Input extends string> = ParseIdentifier<Input> extends [ | ||
@@ -145,6 +130,3 @@ 'count', | ||
*/ | ||
declare type ParseEmbeddedResource<Input extends string> = Input extends `(${infer Remainder}` ? EatWhitespace<Remainder> extends `)${infer Remainder}` ? [[], EatWhitespace<Remainder>] : ParseNodes<EatWhitespace<Remainder>> extends [ | ||
infer Nodes extends Ast.Node[], | ||
`${infer Remainder}` | ||
] ? EatWhitespace<Remainder> extends `)${infer Remainder}` ? [Nodes, EatWhitespace<Remainder>] : ParserError<`Expected ")" at \`${EatWhitespace<Remainder>}\``> : ParseNodes<EatWhitespace<Remainder>> : ParserError<`Expected "(" at \`${Input}\``>; | ||
declare type ParseEmbeddedResource<Input extends string> = Input extends `(${infer Remainder}` ? EatWhitespace<Remainder> extends `)${infer Remainder}` ? [[], EatWhitespace<Remainder>] : ParseNodes<EatWhitespace<Remainder>> extends [infer Nodes, `${infer Remainder}`] ? Nodes extends Ast.Node[] ? EatWhitespace<Remainder> extends `)${infer Remainder}` ? [Nodes, EatWhitespace<Remainder>] : ParserError<`Expected ")" at \`${EatWhitespace<Remainder>}\``> : ParserError<'Invalid nodes array in embedded resource'> : ParseNodes<EatWhitespace<Remainder>> : ParserError<`Expected "(" at \`${Input}\``>; | ||
/** | ||
@@ -151,0 +133,0 @@ * Parses a field excluding embedded resources, without preceding field renaming. |
@@ -15,3 +15,3 @@ import { GenericTable } from '../types'; | ||
*/ | ||
export declare type GetResult<Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName, Relationships, Query extends string> = IsAny<Schema> extends true ? ParseQuery<Query> extends infer ParsedQuery extends Ast.Node[] ? RelationName extends string ? ProcessNodesWithoutSchema<ParsedQuery> : any : any : Relationships extends null ? ParseQuery<Query> extends infer ParsedQuery extends Ast.Node[] ? RPCCallNodes<ParsedQuery, RelationName extends string ? RelationName : 'rpc_call', Row> : Row : ParseQuery<Query> extends infer ParsedQuery ? ParsedQuery extends Ast.Node[] ? RelationName extends string ? Relationships extends GenericRelationship[] ? ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery> : SelectQueryError<'Invalid Relationships cannot infer result type'> : SelectQueryError<'Invalid RelationName cannot infer result type'> : ParsedQuery : never; | ||
export declare type GetResult<Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName, Relationships, Query extends string> = IsAny<Schema> extends true ? ParseQuery<Query> extends infer ParsedQuery ? ParsedQuery extends Ast.Node[] ? RelationName extends string ? ProcessNodesWithoutSchema<ParsedQuery> : any : ParsedQuery : any : Relationships extends null ? ParseQuery<Query> extends infer ParsedQuery ? ParsedQuery extends Ast.Node[] ? RPCCallNodes<ParsedQuery, RelationName extends string ? RelationName : 'rpc_call', Row> : ParsedQuery : Row : ParseQuery<Query> extends infer ParsedQuery ? ParsedQuery extends Ast.Node[] ? RelationName extends string ? Relationships extends GenericRelationship[] ? ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery> : SelectQueryError<'Invalid Relationships cannot infer result type'> : SelectQueryError<'Invalid RelationName cannot infer result type'> : ParsedQuery : never; | ||
declare type ProcessSimpleFieldWithoutSchema<Field extends Ast.FieldNode> = Field['aggregateFunction'] extends AggregateFunctions ? { | ||
@@ -36,3 +36,3 @@ [K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes ? TypeScriptTypes<Field['castType']> : number; | ||
*/ | ||
declare type ProcessNodesWithoutSchema<Nodes extends Ast.Node[], Acc extends Record<string, unknown> = {}> = Nodes extends [infer FirstNode extends Ast.Node, ...infer RestNodes extends Ast.Node[]] ? ProcessNodeWithoutSchema<FirstNode> extends infer FieldResult ? FieldResult extends Record<string, unknown> ? ProcessNodesWithoutSchema<RestNodes, Acc & FieldResult> : FieldResult : any : Prettify<Acc>; | ||
declare type ProcessNodesWithoutSchema<Nodes extends Ast.Node[], Acc extends Record<string, unknown> = {}> = Nodes extends [infer FirstNode, ...infer RestNodes] ? FirstNode extends Ast.Node ? RestNodes extends Ast.Node[] ? ProcessNodeWithoutSchema<FirstNode> extends infer FieldResult ? FieldResult extends Record<string, unknown> ? ProcessNodesWithoutSchema<RestNodes, Acc & FieldResult> : FieldResult : any : any : any : Prettify<Acc>; | ||
/** | ||
@@ -49,3 +49,3 @@ * Processes a single Node from a select chained after a rpc call | ||
*/ | ||
export declare type RPCCallNodes<Nodes extends Ast.Node[], RelationName extends string, Row extends Record<string, unknown>, Acc extends Record<string, unknown> = {}> = Nodes extends [infer FirstNode extends Ast.Node, ...infer RestNodes extends Ast.Node[]] ? ProcessRPCNode<Row, RelationName, FirstNode> extends infer FieldResult ? FieldResult extends Record<string, unknown> ? RPCCallNodes<RestNodes, RelationName, Row, Acc & FieldResult> : FieldResult extends SelectQueryError<infer E> ? SelectQueryError<E> : SelectQueryError<'Could not retrieve a valid record or error value'> : SelectQueryError<'Processing node failed.'> : Prettify<Acc>; | ||
export declare type RPCCallNodes<Nodes extends Ast.Node[], RelationName extends string, Row extends Record<string, unknown>, Acc extends Record<string, unknown> = {}> = Nodes extends [infer FirstNode, ...infer RestNodes] ? FirstNode extends Ast.Node ? RestNodes extends Ast.Node[] ? ProcessRPCNode<Row, RelationName, FirstNode> extends infer FieldResult ? FieldResult extends Record<string, unknown> ? RPCCallNodes<RestNodes, RelationName, Row, Acc & FieldResult> : FieldResult extends SelectQueryError<infer E> ? SelectQueryError<E> : SelectQueryError<'Could not retrieve a valid record or error value'> : SelectQueryError<'Processing node failed.'> : SelectQueryError<'Invalid rest nodes array in RPC call'> : SelectQueryError<'Invalid first node in RPC call'> : Prettify<Acc>; | ||
/** | ||
@@ -61,3 +61,3 @@ * Recursively processes an array of Nodes and accumulates the resulting TypeScript type. | ||
*/ | ||
export declare type ProcessNodes<Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName extends string, Relationships extends GenericRelationship[], Nodes extends Ast.Node[], Acc extends Record<string, unknown> = {}> = CheckDuplicateEmbededReference<Schema, RelationName, Relationships, Nodes> extends false ? Nodes extends [infer FirstNode extends Ast.Node, ...infer RestNodes extends Ast.Node[]] ? ProcessNode<Schema, Row, RelationName, Relationships, FirstNode> extends infer FieldResult ? FieldResult extends Record<string, unknown> ? ProcessNodes<Schema, Row, RelationName, Relationships, RestNodes, Acc & FieldResult> : FieldResult extends SelectQueryError<infer E> ? SelectQueryError<E> : SelectQueryError<'Could not retrieve a valid record or error value'> : SelectQueryError<'Processing node failed.'> : Prettify<Acc> : Prettify<CheckDuplicateEmbededReference<Schema, RelationName, Relationships, Nodes>>; | ||
export declare type ProcessNodes<Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName extends string, Relationships extends GenericRelationship[], Nodes extends Ast.Node[], Acc extends Record<string, unknown> = {}> = CheckDuplicateEmbededReference<Schema, RelationName, Relationships, Nodes> extends false ? Nodes extends [infer FirstNode, ...infer RestNodes] ? FirstNode extends Ast.Node ? RestNodes extends Ast.Node[] ? ProcessNode<Schema, Row, RelationName, Relationships, FirstNode> extends infer FieldResult ? FieldResult extends Record<string, unknown> ? ProcessNodes<Schema, Row, RelationName, Relationships, RestNodes, Acc & FieldResult> : FieldResult extends SelectQueryError<infer E> ? SelectQueryError<E> : SelectQueryError<'Could not retrieve a valid record or error value'> : SelectQueryError<'Processing node failed.'> : SelectQueryError<'Invalid rest nodes array type in ProcessNodes'> : SelectQueryError<'Invalid first node type in ProcessNodes'> : Prettify<Acc> : Prettify<CheckDuplicateEmbededReference<Schema, RelationName, Relationships, Nodes>>; | ||
/** | ||
@@ -64,0 +64,0 @@ * Processes a single Node and returns the resulting TypeScript type. |
@@ -12,9 +12,9 @@ import { Ast } from './parser'; | ||
declare type ResolveRelationships<Schema extends GenericSchema, RelationName extends string, Relationships extends GenericRelationship[], Nodes extends Ast.FieldNode[]> = UnionToArray<{ | ||
[K in keyof Nodes]: ResolveRelationship<Schema, Relationships, Nodes[K], RelationName> extends infer Relation ? Relation extends { | ||
[K in keyof Nodes]: Nodes[K] extends Ast.FieldNode ? ResolveRelationship<Schema, Relationships, Nodes[K], RelationName> extends infer Relation ? Relation extends { | ||
relation: { | ||
referencedRelation: any; | ||
foreignKeyName: any; | ||
match: any; | ||
referencedRelation: string; | ||
foreignKeyName: string; | ||
match: string; | ||
}; | ||
from: any; | ||
from: string; | ||
} ? { | ||
@@ -26,3 +26,3 @@ referencedTable: Relation['relation']['referencedRelation']; | ||
fieldName: GetFieldNodeResultName<Nodes[K]>; | ||
} : never : never; | ||
} : Relation : never : never; | ||
}>[0]; | ||
@@ -35,8 +35,8 @@ /** | ||
fieldName: infer FN; | ||
match: infer M extends 'col' | 'refrel'; | ||
} ? U extends { | ||
match: infer M; | ||
} ? M extends 'col' | 'refrel' ? U extends { | ||
referencedTable: RT; | ||
fieldName: FN; | ||
match: M; | ||
} ? true : false : false; | ||
} ? true : false : false : false; | ||
/** | ||
@@ -50,7 +50,7 @@ * Compares one element with all other elements in the array to find duplicates | ||
declare type FindDuplicates<Arr extends any[]> = Arr extends [infer Head, ...infer Tail] ? CheckDuplicates<Tail, Head> | FindDuplicates<Tail> : never; | ||
export declare type CheckDuplicateEmbededReference<Schema extends GenericSchema, RelationName extends string, Relationships extends GenericRelationship[], Nodes extends Ast.Node[]> = FilterRelationNodes<Nodes> extends infer RelationsNodes extends Ast.FieldNode[] ? ResolveRelationships<Schema, RelationName, Relationships, RelationsNodes> extends infer ResolvedRels ? ResolvedRels extends unknown[] ? FindDuplicates<ResolvedRels> extends infer Duplicates ? Duplicates extends never ? false : Duplicates extends { | ||
fieldName: infer FieldName extends string; | ||
} ? { | ||
export declare type CheckDuplicateEmbededReference<Schema extends GenericSchema, RelationName extends string, Relationships extends GenericRelationship[], Nodes extends Ast.Node[]> = FilterRelationNodes<Nodes> extends infer RelationsNodes ? RelationsNodes extends Ast.FieldNode[] ? ResolveRelationships<Schema, RelationName, Relationships, RelationsNodes> extends infer ResolvedRels ? ResolvedRels extends unknown[] ? FindDuplicates<ResolvedRels> extends infer Duplicates ? Duplicates extends never ? false : Duplicates extends { | ||
fieldName: infer FieldName; | ||
} ? FieldName extends string ? { | ||
[K in FieldName]: SelectQueryError<`table "${RelationName}" specified more than once use hinting for desambiguation`>; | ||
} : false : false : false : false : false; | ||
} : false : false : false : false : false : false : false; | ||
/** | ||
@@ -74,14 +74,14 @@ * Returns a boolean representing whether there is a foreign key referencing | ||
relation: { | ||
referencedRelation: infer RelatedRelationName extends string; | ||
referencedRelation: infer RelatedRelationName; | ||
name: string; | ||
}; | ||
direction: 'reverse'; | ||
} ? HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true ? SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`> : FoundRelation : FoundRelation extends { | ||
} ? RelatedRelationName extends string ? HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true ? SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`> : FoundRelation : never : FoundRelation extends { | ||
relation: { | ||
referencedRelation: infer RelatedRelationName extends string; | ||
referencedRelation: infer RelatedRelationName; | ||
name: string; | ||
}; | ||
direction: 'forward'; | ||
from: infer From extends keyof TablesAndViews<Schema> & string; | ||
} ? HasMultipleFKeysToFRel<RelatedRelationName, TablesAndViews<Schema>[From]['Relationships']> extends true ? SelectQueryError<`Could not embed because more than one relationship was found for '${From}' and '${RelatedRelationName}' you need to hint the column with ${From}!<columnName> ?`> : FoundRelation : FoundRelation; | ||
from: infer From; | ||
} ? RelatedRelationName extends string ? From extends keyof TablesAndViews<Schema> & string ? HasMultipleFKeysToFRel<RelatedRelationName, TablesAndViews<Schema>[From]['Relationships']> extends true ? SelectQueryError<`Could not embed because more than one relationship was found for '${From}' and '${RelatedRelationName}' you need to hint the column with ${From}!<columnName> ?`> : FoundRelation : never : never : FoundRelation; | ||
/** | ||
@@ -95,4 +95,4 @@ * Resolves relationships for embedded resources and retrieves the referenced Table | ||
declare type ResolveReverseRelationship<Schema extends GenericSchema, Relationships extends GenericRelationship[], Field extends Ast.FieldNode, CurrentTableOrView extends keyof TablesAndViews<Schema> & string> = FindFieldMatchingRelationships<Schema, Relationships, Field> extends infer FoundRelation ? FoundRelation extends never ? false : FoundRelation extends { | ||
referencedRelation: infer RelatedRelationName extends string; | ||
} ? RelatedRelationName extends keyof TablesAndViews<Schema> ? FoundRelation extends { | ||
referencedRelation: infer RelatedRelationName; | ||
} ? RelatedRelationName extends string ? RelatedRelationName extends keyof TablesAndViews<Schema> ? FoundRelation extends { | ||
hint: string; | ||
@@ -109,4 +109,4 @@ } ? { | ||
from: CurrentTableOrView; | ||
} : SelectQueryError<`Relation '${RelatedRelationName}' not found in schema.`> : false : false; | ||
export declare type FindMatchingTableRelationships<Schema extends GenericSchema, Relationships extends GenericRelationship[], value extends string> = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]] ? R extends { | ||
} : SelectQueryError<`Relation '${RelatedRelationName}' not found in schema.`> : false : false : false; | ||
export declare type FindMatchingTableRelationships<Schema extends GenericSchema, Relationships extends GenericRelationship[], value extends string> = Relationships extends [infer R, ...infer Rest] ? Rest extends GenericRelationship[] ? R extends { | ||
referencedRelation: infer ReferencedRelation; | ||
@@ -125,4 +125,4 @@ } ? ReferencedRelation extends keyof Schema['Tables'] ? R extends { | ||
match: 'col'; | ||
} : FindMatchingTableRelationships<Schema, Rest, value> : FindMatchingTableRelationships<Schema, Rest, value> : false : false; | ||
export declare type FindMatchingViewRelationships<Schema extends GenericSchema, Relationships extends GenericRelationship[], value extends string> = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]] ? R extends { | ||
} : FindMatchingTableRelationships<Schema, Rest, value> : FindMatchingTableRelationships<Schema, Rest, value> : false : false : false; | ||
export declare type FindMatchingViewRelationships<Schema extends GenericSchema, Relationships extends GenericRelationship[], value extends string> = Relationships extends [infer R, ...infer Rest] ? Rest extends GenericRelationship[] ? R extends { | ||
referencedRelation: infer ReferencedRelation; | ||
@@ -141,4 +141,4 @@ } ? ReferencedRelation extends keyof Schema['Views'] ? R extends { | ||
match: 'col'; | ||
} : FindMatchingViewRelationships<Schema, Rest, value> : FindMatchingViewRelationships<Schema, Rest, value> : false : false; | ||
export declare type FindMatchingHintTableRelationships<Schema extends GenericSchema, Relationships extends GenericRelationship[], hint extends string, name extends string> = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]] ? R extends { | ||
} : FindMatchingViewRelationships<Schema, Rest, value> : FindMatchingViewRelationships<Schema, Rest, value> : false : false : false; | ||
export declare type FindMatchingHintTableRelationships<Schema extends GenericSchema, Relationships extends GenericRelationship[], hint extends string, name extends string> = Relationships extends [infer R, ...infer Rest] ? Rest extends GenericRelationship[] ? R extends { | ||
referencedRelation: infer ReferencedRelation; | ||
@@ -157,4 +157,4 @@ } ? ReferencedRelation extends name ? R extends { | ||
match: 'col'; | ||
} : FindMatchingHintTableRelationships<Schema, Rest, hint, name> : FindMatchingHintTableRelationships<Schema, Rest, hint, name> : false : false; | ||
export declare type FindMatchingHintViewRelationships<Schema extends GenericSchema, Relationships extends GenericRelationship[], hint extends string, name extends string> = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]] ? R extends { | ||
} : FindMatchingHintTableRelationships<Schema, Rest, hint, name> : FindMatchingHintTableRelationships<Schema, Rest, hint, name> : false : false : false; | ||
export declare type FindMatchingHintViewRelationships<Schema extends GenericSchema, Relationships extends GenericRelationship[], hint extends string, name extends string> = Relationships extends [infer R, ...infer Rest] ? Rest extends GenericRelationship[] ? R extends { | ||
referencedRelation: infer ReferencedRelation; | ||
@@ -173,8 +173,8 @@ } ? ReferencedRelation extends name ? R extends { | ||
match: 'col'; | ||
} : FindMatchingHintViewRelationships<Schema, Rest, hint, name> : FindMatchingHintViewRelationships<Schema, Rest, hint, name> : false : false; | ||
} : FindMatchingHintViewRelationships<Schema, Rest, hint, name> : FindMatchingHintViewRelationships<Schema, Rest, hint, name> : false : false : false; | ||
declare type IsColumnsNullable<Table extends Pick<GenericTable, 'Row'>, Columns extends (keyof Table['Row'])[]> = Columns extends [infer Column, ...infer Rest] ? Column extends keyof Table['Row'] ? ContainsNull<Table['Row'][Column]> extends true ? true : IsColumnsNullable<Table, Rest extends (keyof Table['Row'])[] ? Rest : []> : false : false; | ||
export declare type IsRelationNullable<Table extends GenericTable, Relation extends GenericRelationship> = IsColumnsNullable<Table, Relation['columns']>; | ||
declare type TableForwardRelationships<Schema extends GenericSchema, TName> = TName extends keyof TablesAndViews<Schema> ? UnionToArray<RecursivelyFindRelationships<Schema, TName, keyof TablesAndViews<Schema>>> extends infer R extends (GenericRelationship & { | ||
declare type TableForwardRelationships<Schema extends GenericSchema, TName> = TName extends keyof TablesAndViews<Schema> ? UnionToArray<RecursivelyFindRelationships<Schema, TName, keyof TablesAndViews<Schema>>> extends infer R ? R extends (GenericRelationship & { | ||
from: keyof TablesAndViews<Schema>; | ||
})[] ? R : [] : []; | ||
})[] ? R : [] : [] : []; | ||
declare type RecursivelyFindRelationships<Schema extends GenericSchema, TName, Keys extends keyof TablesAndViews<Schema>> = Keys extends infer K ? K extends keyof TablesAndViews<Schema> ? FilterRelationships<TablesAndViews<Schema>[K]['Relationships'], TName, K> extends never ? RecursivelyFindRelationships<Schema, TName, Exclude<Keys, K>> : FilterRelationships<TablesAndViews<Schema>[K]['Relationships'], TName, K> | RecursivelyFindRelationships<Schema, TName, Exclude<Keys, K>> : false : false; | ||
@@ -186,6 +186,6 @@ declare type FilterRelationships<R, TName, From> = R extends readonly (infer Rel)[] ? Rel extends { | ||
} : never : never; | ||
declare type ResolveForwardRelationship<Schema extends GenericSchema, Field extends Ast.FieldNode, CurrentTableOrView extends keyof TablesAndViews<Schema> & string> = FindFieldMatchingRelationships<Schema, TablesAndViews<Schema>[Field['name']]['Relationships'], Ast.FieldNode & { | ||
export declare type ResolveForwardRelationship<Schema extends GenericSchema, Field extends Ast.FieldNode, CurrentTableOrView extends keyof TablesAndViews<Schema> & string> = FindFieldMatchingRelationships<Schema, TablesAndViews<Schema>[Field['name']]['Relationships'], Ast.FieldNode & { | ||
name: CurrentTableOrView; | ||
hint: Field['hint']; | ||
}> extends infer FoundByName extends GenericRelationship ? { | ||
}> extends infer FoundByName ? FoundByName extends GenericRelationship ? { | ||
referencedTable: TablesAndViews<Schema>[Field['name']]; | ||
@@ -196,3 +196,3 @@ relation: FoundByName; | ||
type: 'found-by-name'; | ||
} : FindFieldMatchingRelationships<Schema, TableForwardRelationships<Schema, CurrentTableOrView>, Field> extends infer FoundByMatch extends GenericRelationship & { | ||
} : FindFieldMatchingRelationships<Schema, TableForwardRelationships<Schema, CurrentTableOrView>, Field> extends infer FoundByMatch ? FoundByMatch extends GenericRelationship & { | ||
from: keyof TablesAndViews<Schema>; | ||
@@ -205,3 +205,3 @@ } ? { | ||
type: 'found-by-match'; | ||
} : FindJoinTableRelationship<Schema, CurrentTableOrView, Field['name']> extends infer FoundByJoinTable extends GenericRelationship ? { | ||
} : FindJoinTableRelationship<Schema, CurrentTableOrView, Field['name']> extends infer FoundByJoinTable ? FoundByJoinTable extends GenericRelationship ? { | ||
referencedTable: TablesAndViews<Schema>[FoundByJoinTable['referencedRelation']]; | ||
@@ -214,3 +214,3 @@ relation: FoundByJoinTable & { | ||
type: 'found-by-join-table'; | ||
} : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`>; | ||
} : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`>; | ||
/** | ||
@@ -234,3 +234,3 @@ * Given a CurrentTableOrView, finds all join tables to this relation. | ||
*/ | ||
export declare type FindJoinTableRelationship<Schema extends GenericSchema, CurrentTableOrView extends keyof TablesAndViews<Schema> & string, FieldName extends string> = { | ||
declare type ResolveJoinTableRelationship<Schema extends GenericSchema, CurrentTableOrView extends keyof TablesAndViews<Schema> & string, FieldName extends string> = { | ||
[TableName in keyof TablesAndViews<Schema>]: TablesAndViews<Schema>[TableName]['Relationships'] extends readonly (infer Rel)[] ? Rel extends { | ||
@@ -242,2 +242,3 @@ referencedRelation: CurrentTableOrView; | ||
}[keyof TablesAndViews<Schema>]; | ||
export declare type FindJoinTableRelationship<Schema extends GenericSchema, CurrentTableOrView extends keyof TablesAndViews<Schema> & string, FieldName extends string> = ResolveJoinTableRelationship<Schema, CurrentTableOrView, FieldName> extends infer Result ? [Result] extends [never] ? false : Result : never; | ||
/** | ||
@@ -247,13 +248,13 @@ * Finds a matching relationship based on the FieldNode's name and optional hint. | ||
export declare type FindFieldMatchingRelationships<Schema extends GenericSchema, Relationships extends GenericRelationship[], Field extends Ast.FieldNode> = Field extends { | ||
hint: infer Hint extends string; | ||
} ? FindMatchingHintTableRelationships<Schema, Relationships, Hint, Field['name']> extends infer TableRelationViaHint extends GenericRelationship ? TableRelationViaHint & { | ||
hint: string; | ||
} ? FindMatchingHintTableRelationships<Schema, Relationships, Field['hint'], Field['name']> extends GenericRelationship ? FindMatchingHintTableRelationships<Schema, Relationships, Field['hint'], Field['name']> & { | ||
branch: 'found-in-table-via-hint'; | ||
hint: Field['hint']; | ||
} : FindMatchingHintViewRelationships<Schema, Relationships, Hint, Field['name']> extends infer TableViewViaHint extends GenericRelationship ? TableViewViaHint & { | ||
} : FindMatchingHintViewRelationships<Schema, Relationships, Field['hint'], Field['name']> extends GenericRelationship ? FindMatchingHintViewRelationships<Schema, Relationships, Field['hint'], Field['name']> & { | ||
branch: 'found-in-view-via-hint'; | ||
hint: Field['hint']; | ||
} : SelectQueryError<'Failed to find matching relation via hint'> : FindMatchingTableRelationships<Schema, Relationships, Field['name']> extends infer TableRelationViaName extends GenericRelationship ? TableRelationViaName & { | ||
} : SelectQueryError<'Failed to find matching relation via hint'> : FindMatchingTableRelationships<Schema, Relationships, Field['name']> extends GenericRelationship ? FindMatchingTableRelationships<Schema, Relationships, Field['name']> & { | ||
branch: 'found-in-table-via-name'; | ||
name: Field['name']; | ||
} : FindMatchingViewRelationships<Schema, Relationships, Field['name']> extends infer ViewRelationViaName extends GenericRelationship ? ViewRelationViaName & { | ||
} : FindMatchingViewRelationships<Schema, Relationships, Field['name']> extends GenericRelationship ? FindMatchingViewRelationships<Schema, Relationships, Field['name']> & { | ||
branch: 'found-in-view-via-name'; | ||
@@ -260,0 +261,0 @@ name: Field['name']; |
{ | ||
"name": "@supabase/postgrest-js", | ||
"version": "1.17.2", | ||
"version": "1.17.3", | ||
"description": "Isomorphic PostgREST client", | ||
@@ -63,5 +63,5 @@ "keywords": [ | ||
"typedoc": "^0.22.16", | ||
"typescript": "~4.7", | ||
"typescript": "4.5.5", | ||
"wait-for-localhost-cli": "^3.0.0" | ||
} | ||
} |
@@ -15,6 +15,8 @@ // Credits to @bnjmnt4n (https://www.npmjs.com/package/postgrest-query) | ||
? GenericStringError | ||
: ParseNodes<EatWhitespace<Query>> extends [infer Nodes extends Ast.Node[], `${infer Remainder}`] | ||
? EatWhitespace<Remainder> extends '' | ||
? SimplifyDeep<Nodes> | ||
: ParserError<`Unexpected input: ${Remainder}`> | ||
: ParseNodes<EatWhitespace<Query>> extends [infer Nodes, `${infer Remainder}`] | ||
? Nodes extends Ast.Node[] | ||
? EatWhitespace<Remainder> extends '' | ||
? SimplifyDeep<Nodes> | ||
: ParserError<`Unexpected input: ${Remainder}`> | ||
: ParserError<'Invalid nodes array structure'> | ||
: ParseNodes<EatWhitespace<Query>> | ||
@@ -38,10 +40,11 @@ | ||
type ParseNodesHelper<Input extends string, Nodes extends Ast.Node[]> = ParseNode<Input> extends [ | ||
infer Node extends Ast.Node, | ||
infer Node, | ||
`${infer Remainder}` | ||
] | ||
? EatWhitespace<Remainder> extends `,${infer Remainder}` | ||
? ParseNodesHelper<EatWhitespace<Remainder>, [...Nodes, Node]> | ||
: [[...Nodes, Node], EatWhitespace<Remainder>] | ||
? Node extends Ast.Node | ||
? EatWhitespace<Remainder> extends `,${infer Remainder}` | ||
? ParseNodesHelper<EatWhitespace<Remainder>, [...Nodes, Node]> | ||
: [[...Nodes, Node], EatWhitespace<Remainder>] | ||
: ParserError<'Invalid node type in nodes helper'> | ||
: ParseNode<Input> | ||
/** | ||
@@ -62,7 +65,6 @@ * Parses a node. | ||
Input extends `...${infer Remainder}` | ||
? ParseField<EatWhitespace<Remainder>> extends [ | ||
infer TargetField extends Ast.FieldNode, | ||
`${infer Remainder}` | ||
] | ||
? [{ type: 'spread'; target: TargetField }, EatWhitespace<Remainder>] | ||
? ParseField<EatWhitespace<Remainder>> extends [infer TargetField, `${infer Remainder}`] | ||
? TargetField extends Ast.FieldNode | ||
? [{ type: 'spread'; target: TargetField }, EatWhitespace<Remainder>] | ||
: ParserError<'Invalid target field type in spread'> | ||
: ParserError<`Unable to parse spread resource at \`${Input}\``> | ||
@@ -75,7 +77,6 @@ : ParseIdentifier<Input> extends [infer NameOrAlias, `${infer Remainder}`] | ||
? // `alias:` | ||
ParseField<EatWhitespace<Remainder>> extends [ | ||
infer Field extends Ast.FieldNode, | ||
`${infer Remainder}` | ||
] | ||
? [Omit<Field, 'alias'> & { alias: NameOrAlias }, EatWhitespace<Remainder>] | ||
ParseField<EatWhitespace<Remainder>> extends [infer Field, `${infer Remainder}`] | ||
? Field extends Ast.FieldNode | ||
? [Omit<Field, 'alias'> & { alias: NameOrAlias }, EatWhitespace<Remainder>] | ||
: ParserError<'Invalid field type in alias parsing'> | ||
: ParserError<`Unable to parse renamed field at \`${Input}\``> | ||
@@ -105,8 +106,7 @@ : // Otherwise, just parse it as a field without alias. | ||
: Remainder extends `!inner${infer Remainder}` | ||
? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
`${infer Remainder}` | ||
] | ||
? // `field!inner(nodes)` | ||
[{ type: 'field'; name: Name; innerJoin: true; children: Children }, Remainder] | ||
? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [infer Children, `${infer Remainder}`] | ||
? Children extends Ast.Node[] | ||
? // `field!inner(nodes)` | ||
[{ type: 'field'; name: Name; innerJoin: true; children: Children }, Remainder] | ||
: ParserError<'Invalid children array in inner join'> | ||
: CreateParserErrorIfRequired< | ||
@@ -117,9 +117,8 @@ ParseEmbeddedResource<EatWhitespace<Remainder>>, | ||
: EatWhitespace<Remainder> extends `!left${infer Remainder}` | ||
? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
`${infer Remainder}` | ||
] | ||
? // `field!left(nodes)` | ||
// !left is a noise word - treat it the same way as a non-`!inner`. | ||
[{ type: 'field'; name: Name; children: Children }, EatWhitespace<Remainder>] | ||
? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [infer Children, `${infer Remainder}`] | ||
? Children extends Ast.Node[] | ||
? // `field!left(nodes)` | ||
// !left is a noise word - treat it the same way as a non-`!inner`. | ||
[{ type: 'field'; name: Name; children: Children }, EatWhitespace<Remainder>] | ||
: ParserError<'Invalid children array in left join'> | ||
: CreateParserErrorIfRequired< | ||
@@ -133,26 +132,32 @@ ParseEmbeddedResource<EatWhitespace<Remainder>>, | ||
? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
infer Children, | ||
`${infer Remainder}` | ||
] | ||
? // `field!hint!inner(nodes)` | ||
[ | ||
{ type: 'field'; name: Name; hint: Hint; innerJoin: true; children: Children }, | ||
EatWhitespace<Remainder> | ||
] | ||
? Children extends Ast.Node[] | ||
? // `field!hint!inner(nodes)` | ||
[ | ||
{ type: 'field'; name: Name; hint: Hint; innerJoin: true; children: Children }, | ||
EatWhitespace<Remainder> | ||
] | ||
: ParserError<'Invalid children array in hint inner join'> | ||
: ParseEmbeddedResource<EatWhitespace<Remainder>> | ||
: ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
infer Children, | ||
`${infer Remainder}` | ||
] | ||
? // `field!hint(nodes)` | ||
[{ type: 'field'; name: Name; hint: Hint; children: Children }, EatWhitespace<Remainder>] | ||
? Children extends Ast.Node[] | ||
? // `field!hint(nodes)` | ||
[ | ||
{ type: 'field'; name: Name; hint: Hint; children: Children }, | ||
EatWhitespace<Remainder> | ||
] | ||
: ParserError<'Invalid children array in hint'> | ||
: ParseEmbeddedResource<EatWhitespace<Remainder>> | ||
: ParserError<`Expected identifier after "!" at \`${EatWhitespace<Remainder>}\``> | ||
: EatWhitespace<Remainder> extends `(${infer _}` | ||
? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [ | ||
infer Children extends Ast.Node[], | ||
`${infer Remainder}` | ||
] | ||
? // `field(nodes)` | ||
[{ type: 'field'; name: Name; children: Children }, EatWhitespace<Remainder>] | ||
? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [infer Children, `${infer Remainder}`] | ||
? Children extends Ast.Node[] | ||
? // `field(nodes)` | ||
[{ type: 'field'; name: Name; children: Children }, EatWhitespace<Remainder>] | ||
: ParserError<'Invalid children array in field'> | ||
: // Return error if start of embedded resource was detected but not found. | ||
@@ -194,9 +199,8 @@ ParseEmbeddedResource<EatWhitespace<Remainder>> | ||
? [[], EatWhitespace<Remainder>] | ||
: ParseNodes<EatWhitespace<Remainder>> extends [ | ||
infer Nodes extends Ast.Node[], | ||
`${infer Remainder}` | ||
] | ||
? EatWhitespace<Remainder> extends `)${infer Remainder}` | ||
? [Nodes, EatWhitespace<Remainder>] | ||
: ParserError<`Expected ")" at \`${EatWhitespace<Remainder>}\``> | ||
: ParseNodes<EatWhitespace<Remainder>> extends [infer Nodes, `${infer Remainder}`] | ||
? Nodes extends Ast.Node[] | ||
? EatWhitespace<Remainder> extends `)${infer Remainder}` | ||
? [Nodes, EatWhitespace<Remainder>] | ||
: ParserError<`Expected ")" at \`${EatWhitespace<Remainder>}\``> | ||
: ParserError<'Invalid nodes array in embedded resource'> | ||
: ParseNodes<EatWhitespace<Remainder>> | ||
@@ -203,0 +207,0 @@ : ParserError<`Expected "(" at \`${Input}\``> |
@@ -38,10 +38,14 @@ import { GenericTable } from '../types' | ||
> = IsAny<Schema> extends true | ||
? ParseQuery<Query> extends infer ParsedQuery extends Ast.Node[] | ||
? RelationName extends string | ||
? ProcessNodesWithoutSchema<ParsedQuery> | ||
: any | ||
? ParseQuery<Query> extends infer ParsedQuery | ||
? ParsedQuery extends Ast.Node[] | ||
? RelationName extends string | ||
? ProcessNodesWithoutSchema<ParsedQuery> | ||
: any | ||
: ParsedQuery | ||
: any | ||
: Relationships extends null // For .rpc calls the passed relationships will be null in that case, the result will always be the function return type | ||
? ParseQuery<Query> extends infer ParsedQuery extends Ast.Node[] | ||
? RPCCallNodes<ParsedQuery, RelationName extends string ? RelationName : 'rpc_call', Row> | ||
? ParseQuery<Query> extends infer ParsedQuery | ||
? ParsedQuery extends Ast.Node[] | ||
? RPCCallNodes<ParsedQuery, RelationName extends string ? RelationName : 'rpc_call', Row> | ||
: ParsedQuery | ||
: Row | ||
@@ -115,7 +119,11 @@ : ParseQuery<Query> extends infer ParsedQuery | ||
Acc extends Record<string, unknown> = {} | ||
> = Nodes extends [infer FirstNode extends Ast.Node, ...infer RestNodes extends Ast.Node[]] | ||
? ProcessNodeWithoutSchema<FirstNode> extends infer FieldResult | ||
? FieldResult extends Record<string, unknown> | ||
? ProcessNodesWithoutSchema<RestNodes, Acc & FieldResult> | ||
: FieldResult | ||
> = Nodes extends [infer FirstNode, ...infer RestNodes] | ||
? FirstNode extends Ast.Node | ||
? RestNodes extends Ast.Node[] | ||
? ProcessNodeWithoutSchema<FirstNode> extends infer FieldResult | ||
? FieldResult extends Record<string, unknown> | ||
? ProcessNodesWithoutSchema<RestNodes, Acc & FieldResult> | ||
: FieldResult | ||
: any | ||
: any | ||
: any | ||
@@ -149,10 +157,14 @@ : Prettify<Acc> | ||
Acc extends Record<string, unknown> = {} // Acc is now an object | ||
> = Nodes extends [infer FirstNode extends Ast.Node, ...infer RestNodes extends Ast.Node[]] | ||
? ProcessRPCNode<Row, RelationName, FirstNode> extends infer FieldResult | ||
? FieldResult extends Record<string, unknown> | ||
? RPCCallNodes<RestNodes, RelationName, Row, Acc & FieldResult> | ||
: FieldResult extends SelectQueryError<infer E> | ||
? SelectQueryError<E> | ||
: SelectQueryError<'Could not retrieve a valid record or error value'> | ||
: SelectQueryError<'Processing node failed.'> | ||
> = Nodes extends [infer FirstNode, ...infer RestNodes] | ||
? FirstNode extends Ast.Node | ||
? RestNodes extends Ast.Node[] | ||
? ProcessRPCNode<Row, RelationName, FirstNode> extends infer FieldResult | ||
? FieldResult extends Record<string, unknown> | ||
? RPCCallNodes<RestNodes, RelationName, Row, Acc & FieldResult> | ||
: FieldResult extends SelectQueryError<infer E> | ||
? SelectQueryError<E> | ||
: SelectQueryError<'Could not retrieve a valid record or error value'> | ||
: SelectQueryError<'Processing node failed.'> | ||
: SelectQueryError<'Invalid rest nodes array in RPC call'> | ||
: SelectQueryError<'Invalid first node in RPC call'> | ||
: Prettify<Acc> | ||
@@ -178,10 +190,14 @@ | ||
> = CheckDuplicateEmbededReference<Schema, RelationName, Relationships, Nodes> extends false | ||
? Nodes extends [infer FirstNode extends Ast.Node, ...infer RestNodes extends Ast.Node[]] | ||
? ProcessNode<Schema, Row, RelationName, Relationships, FirstNode> extends infer FieldResult | ||
? FieldResult extends Record<string, unknown> | ||
? ProcessNodes<Schema, Row, RelationName, Relationships, RestNodes, Acc & FieldResult> | ||
: FieldResult extends SelectQueryError<infer E> | ||
? SelectQueryError<E> | ||
: SelectQueryError<'Could not retrieve a valid record or error value'> | ||
: SelectQueryError<'Processing node failed.'> | ||
? Nodes extends [infer FirstNode, ...infer RestNodes] | ||
? FirstNode extends Ast.Node | ||
? RestNodes extends Ast.Node[] | ||
? ProcessNode<Schema, Row, RelationName, Relationships, FirstNode> extends infer FieldResult | ||
? FieldResult extends Record<string, unknown> | ||
? ProcessNodes<Schema, Row, RelationName, Relationships, RestNodes, Acc & FieldResult> | ||
: FieldResult extends SelectQueryError<infer E> | ||
? SelectQueryError<E> | ||
: SelectQueryError<'Could not retrieve a valid record or error value'> | ||
: SelectQueryError<'Processing node failed.'> | ||
: SelectQueryError<'Invalid rest nodes array type in ProcessNodes'> | ||
: SelectQueryError<'Invalid first node type in ProcessNodes'> | ||
: Prettify<Acc> | ||
@@ -188,0 +204,0 @@ : Prettify<CheckDuplicateEmbededReference<Schema, RelationName, Relationships, Nodes>> |
@@ -41,23 +41,20 @@ import { Ast } from './parser' | ||
> = UnionToArray<{ | ||
[K in keyof Nodes]: ResolveRelationship< | ||
Schema, | ||
Relationships, | ||
Nodes[K], | ||
RelationName | ||
> extends infer Relation | ||
? Relation extends { | ||
relation: { | ||
referencedRelation: any | ||
foreignKeyName: any | ||
match: any | ||
[K in keyof Nodes]: Nodes[K] extends Ast.FieldNode | ||
? ResolveRelationship<Schema, Relationships, Nodes[K], RelationName> extends infer Relation | ||
? Relation extends { | ||
relation: { | ||
referencedRelation: string | ||
foreignKeyName: string | ||
match: string | ||
} | ||
from: string | ||
} | ||
from: any | ||
} | ||
? { | ||
referencedTable: Relation['relation']['referencedRelation'] | ||
fkName: Relation['relation']['foreignKeyName'] | ||
from: Relation['from'] | ||
match: Relation['relation']['match'] | ||
fieldName: GetFieldNodeResultName<Nodes[K]> | ||
} | ||
? { | ||
referencedTable: Relation['relation']['referencedRelation'] | ||
fkName: Relation['relation']['foreignKeyName'] | ||
from: Relation['from'] | ||
match: Relation['relation']['match'] | ||
fieldName: GetFieldNodeResultName<Nodes[K]> | ||
} | ||
: Relation | ||
: never | ||
@@ -73,6 +70,8 @@ : never | ||
fieldName: infer FN | ||
match: infer M extends 'col' | 'refrel' | ||
match: infer M | ||
} | ||
? U extends { referencedTable: RT; fieldName: FN; match: M } | ||
? true | ||
? M extends 'col' | 'refrel' | ||
? U extends { referencedTable: RT; fieldName: FN; match: M } | ||
? true | ||
: false | ||
: false | ||
@@ -102,17 +101,21 @@ : false | ||
Nodes extends Ast.Node[] | ||
> = FilterRelationNodes<Nodes> extends infer RelationsNodes extends Ast.FieldNode[] | ||
? ResolveRelationships< | ||
Schema, | ||
RelationName, | ||
Relationships, | ||
RelationsNodes | ||
> extends infer ResolvedRels | ||
? ResolvedRels extends unknown[] | ||
? FindDuplicates<ResolvedRels> extends infer Duplicates | ||
? Duplicates extends never | ||
? false | ||
: Duplicates extends { fieldName: infer FieldName extends string } | ||
? { | ||
[K in FieldName]: SelectQueryError<`table "${RelationName}" specified more than once use hinting for desambiguation`> | ||
} | ||
> = FilterRelationNodes<Nodes> extends infer RelationsNodes | ||
? RelationsNodes extends Ast.FieldNode[] | ||
? ResolveRelationships< | ||
Schema, | ||
RelationName, | ||
Relationships, | ||
RelationsNodes | ||
> extends infer ResolvedRels | ||
? ResolvedRels extends unknown[] | ||
? FindDuplicates<ResolvedRels> extends infer Duplicates | ||
? Duplicates extends never | ||
? false | ||
: Duplicates extends { fieldName: infer FieldName } | ||
? FieldName extends string | ||
? { | ||
[K in FieldName]: SelectQueryError<`table "${RelationName}" specified more than once use hinting for desambiguation`> | ||
} | ||
: false | ||
: false | ||
: false | ||
@@ -161,3 +164,3 @@ : false | ||
relation: { | ||
referencedRelation: infer RelatedRelationName extends string | ||
referencedRelation: infer RelatedRelationName | ||
name: string | ||
@@ -167,24 +170,29 @@ } | ||
} | ||
? // We check if there is possible confusion with other relations with this table | ||
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true | ||
? // If there is, postgrest will fail at runtime, and require desambiguation via hinting | ||
SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`> | ||
: FoundRelation | ||
? RelatedRelationName extends string | ||
? // We check if there is possible confusion with other relations with this table | ||
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true | ||
? // If there is, postgrest will fail at runtime, and require desambiguation via hinting | ||
SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`> | ||
: FoundRelation | ||
: never | ||
: // Same check for forward relationships, but we must gather the relationships from the found relation | ||
FoundRelation extends { | ||
relation: { | ||
referencedRelation: infer RelatedRelationName extends string | ||
referencedRelation: infer RelatedRelationName | ||
name: string | ||
} | ||
direction: 'forward' | ||
from: infer From extends keyof TablesAndViews<Schema> & string | ||
from: infer From | ||
} | ||
? HasMultipleFKeysToFRel< | ||
RelatedRelationName, | ||
TablesAndViews<Schema>[From]['Relationships'] | ||
> extends true | ||
? SelectQueryError<`Could not embed because more than one relationship was found for '${From}' and '${RelatedRelationName}' you need to hint the column with ${From}!<columnName> ?`> | ||
: FoundRelation | ||
? RelatedRelationName extends string | ||
? From extends keyof TablesAndViews<Schema> & string | ||
? HasMultipleFKeysToFRel< | ||
RelatedRelationName, | ||
TablesAndViews<Schema>[From]['Relationships'] | ||
> extends true | ||
? SelectQueryError<`Could not embed because more than one relationship was found for '${From}' and '${RelatedRelationName}' you need to hint the column with ${From}!<columnName> ?`> | ||
: FoundRelation | ||
: never | ||
: never | ||
: FoundRelation | ||
/** | ||
@@ -225,22 +233,24 @@ * Resolves relationships for embedded resources and retrieves the referenced Table | ||
? false | ||
: FoundRelation extends { referencedRelation: infer RelatedRelationName extends string } | ||
? RelatedRelationName extends keyof TablesAndViews<Schema> | ||
? // If the relation was found via hinting we just return it without any more checks | ||
FoundRelation extends { hint: string } | ||
? { | ||
referencedTable: TablesAndViews<Schema>[RelatedRelationName] | ||
relation: FoundRelation | ||
direction: 'reverse' | ||
from: CurrentTableOrView | ||
} | ||
: // If the relation was found via implicit relation naming, we must ensure there is no conflicting matches | ||
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true | ||
? SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`> | ||
: { | ||
referencedTable: TablesAndViews<Schema>[RelatedRelationName] | ||
relation: FoundRelation | ||
direction: 'reverse' | ||
from: CurrentTableOrView | ||
} | ||
: SelectQueryError<`Relation '${RelatedRelationName}' not found in schema.`> | ||
: FoundRelation extends { referencedRelation: infer RelatedRelationName } | ||
? RelatedRelationName extends string | ||
? RelatedRelationName extends keyof TablesAndViews<Schema> | ||
? // If the relation was found via hinting we just return it without any more checks | ||
FoundRelation extends { hint: string } | ||
? { | ||
referencedTable: TablesAndViews<Schema>[RelatedRelationName] | ||
relation: FoundRelation | ||
direction: 'reverse' | ||
from: CurrentTableOrView | ||
} | ||
: // If the relation was found via implicit relation naming, we must ensure there is no conflicting matches | ||
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true | ||
? SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`> | ||
: { | ||
referencedTable: TablesAndViews<Schema>[RelatedRelationName] | ||
relation: FoundRelation | ||
direction: 'reverse' | ||
from: CurrentTableOrView | ||
} | ||
: SelectQueryError<`Relation '${RelatedRelationName}' not found in schema.`> | ||
: false | ||
: false | ||
@@ -253,13 +263,15 @@ : false | ||
value extends string | ||
> = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]] | ||
? R extends { referencedRelation: infer ReferencedRelation } | ||
? ReferencedRelation extends keyof Schema['Tables'] | ||
? R extends { foreignKeyName: value } | ||
? R & { match: 'fkname' } | ||
: R extends { referencedRelation: value } | ||
? R & { match: 'refrel' } | ||
: R extends { columns: [value] } | ||
? R & { match: 'col' } | ||
> = Relationships extends [infer R, ...infer Rest] | ||
? Rest extends GenericRelationship[] | ||
? R extends { referencedRelation: infer ReferencedRelation } | ||
? ReferencedRelation extends keyof Schema['Tables'] | ||
? R extends { foreignKeyName: value } | ||
? R & { match: 'fkname' } | ||
: R extends { referencedRelation: value } | ||
? R & { match: 'refrel' } | ||
: R extends { columns: [value] } | ||
? R & { match: 'col' } | ||
: FindMatchingTableRelationships<Schema, Rest, value> | ||
: FindMatchingTableRelationships<Schema, Rest, value> | ||
: FindMatchingTableRelationships<Schema, Rest, value> | ||
: false | ||
: false | ||
@@ -272,13 +284,15 @@ : false | ||
value extends string | ||
> = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]] | ||
? R extends { referencedRelation: infer ReferencedRelation } | ||
? ReferencedRelation extends keyof Schema['Views'] | ||
? R extends { foreignKeyName: value } | ||
? R & { match: 'fkname' } | ||
: R extends { referencedRelation: value } | ||
? R & { match: 'refrel' } | ||
: R extends { columns: [value] } | ||
? R & { match: 'col' } | ||
> = Relationships extends [infer R, ...infer Rest] | ||
? Rest extends GenericRelationship[] | ||
? R extends { referencedRelation: infer ReferencedRelation } | ||
? ReferencedRelation extends keyof Schema['Views'] | ||
? R extends { foreignKeyName: value } | ||
? R & { match: 'fkname' } | ||
: R extends { referencedRelation: value } | ||
? R & { match: 'refrel' } | ||
: R extends { columns: [value] } | ||
? R & { match: 'col' } | ||
: FindMatchingViewRelationships<Schema, Rest, value> | ||
: FindMatchingViewRelationships<Schema, Rest, value> | ||
: FindMatchingViewRelationships<Schema, Rest, value> | ||
: false | ||
: false | ||
@@ -292,16 +306,17 @@ : false | ||
name extends string | ||
> = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]] | ||
? R extends { referencedRelation: infer ReferencedRelation } | ||
? ReferencedRelation extends name | ||
? R extends { foreignKeyName: hint } | ||
? R & { match: 'fkname' } | ||
: R extends { referencedRelation: hint } | ||
? R & { match: 'refrel' } | ||
: R extends { columns: [hint] } | ||
? R & { match: 'col' } | ||
> = Relationships extends [infer R, ...infer Rest] | ||
? Rest extends GenericRelationship[] | ||
? R extends { referencedRelation: infer ReferencedRelation } | ||
? ReferencedRelation extends name | ||
? R extends { foreignKeyName: hint } | ||
? R & { match: 'fkname' } | ||
: R extends { referencedRelation: hint } | ||
? R & { match: 'refrel' } | ||
: R extends { columns: [hint] } | ||
? R & { match: 'col' } | ||
: FindMatchingHintTableRelationships<Schema, Rest, hint, name> | ||
: FindMatchingHintTableRelationships<Schema, Rest, hint, name> | ||
: FindMatchingHintTableRelationships<Schema, Rest, hint, name> | ||
: false | ||
: false | ||
: false | ||
export type FindMatchingHintViewRelationships< | ||
@@ -312,13 +327,15 @@ Schema extends GenericSchema, | ||
name extends string | ||
> = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]] | ||
? R extends { referencedRelation: infer ReferencedRelation } | ||
? ReferencedRelation extends name | ||
? R extends { foreignKeyName: hint } | ||
? R & { match: 'fkname' } | ||
: R extends { referencedRelation: hint } | ||
? R & { match: 'refrel' } | ||
: R extends { columns: [hint] } | ||
? R & { match: 'col' } | ||
> = Relationships extends [infer R, ...infer Rest] | ||
? Rest extends GenericRelationship[] | ||
? R extends { referencedRelation: infer ReferencedRelation } | ||
? ReferencedRelation extends name | ||
? R extends { foreignKeyName: hint } | ||
? R & { match: 'fkname' } | ||
: R extends { referencedRelation: hint } | ||
? R & { match: 'refrel' } | ||
: R extends { columns: [hint] } | ||
? R & { match: 'col' } | ||
: FindMatchingHintViewRelationships<Schema, Rest, hint, name> | ||
: FindMatchingHintViewRelationships<Schema, Rest, hint, name> | ||
: FindMatchingHintViewRelationships<Schema, Rest, hint, name> | ||
: false | ||
: false | ||
@@ -350,4 +367,6 @@ : false | ||
RecursivelyFindRelationships<Schema, TName, keyof TablesAndViews<Schema>> | ||
> extends infer R extends (GenericRelationship & { from: keyof TablesAndViews<Schema> })[] | ||
? R | ||
> extends infer R | ||
? R extends (GenericRelationship & { from: keyof TablesAndViews<Schema> })[] | ||
? R | ||
: [] | ||
: [] | ||
@@ -376,4 +395,3 @@ : [] | ||
// Find a relationship from the parent to the childrens | ||
type ResolveForwardRelationship< | ||
export type ResolveForwardRelationship< | ||
Schema extends GenericSchema, | ||
@@ -386,42 +404,42 @@ Field extends Ast.FieldNode, | ||
Ast.FieldNode & { name: CurrentTableOrView; hint: Field['hint'] } | ||
> extends infer FoundByName extends GenericRelationship | ||
? { | ||
referencedTable: TablesAndViews<Schema>[Field['name']] | ||
relation: FoundByName | ||
direction: 'forward' | ||
from: Field['name'] | ||
type: 'found-by-name' | ||
} | ||
: // The Field['name'] can sometimes be a reference to the related foreign key | ||
// In that case, we can't use the Field['name'] to get back the relations, instead, we will find all relations pointing | ||
// to our current table or view, and search if we can find a match in it | ||
FindFieldMatchingRelationships< | ||
Schema, | ||
TableForwardRelationships<Schema, CurrentTableOrView>, | ||
Field | ||
> extends infer FoundByMatch extends GenericRelationship & { | ||
from: keyof TablesAndViews<Schema> | ||
} | ||
? { | ||
referencedTable: TablesAndViews<Schema>[FoundByMatch['from']] | ||
relation: FoundByMatch | ||
direction: 'forward' | ||
from: CurrentTableOrView | ||
type: 'found-by-match' | ||
} | ||
: // Forward relations can also alias other tables via tables joins relationships | ||
// in such cases we crawl all the tables looking for a join table between our current table | ||
// and the Field['name'] desired desitnation | ||
FindJoinTableRelationship< | ||
Schema, | ||
CurrentTableOrView, | ||
Field['name'] | ||
> extends infer FoundByJoinTable extends GenericRelationship | ||
? { | ||
referencedTable: TablesAndViews<Schema>[FoundByJoinTable['referencedRelation']] | ||
relation: FoundByJoinTable & { match: 'refrel' } | ||
direction: 'forward' | ||
from: CurrentTableOrView | ||
type: 'found-by-join-table' | ||
} | ||
> extends infer FoundByName | ||
? FoundByName extends GenericRelationship | ||
? { | ||
referencedTable: TablesAndViews<Schema>[Field['name']] | ||
relation: FoundByName | ||
direction: 'forward' | ||
from: Field['name'] | ||
type: 'found-by-name' | ||
} | ||
: FindFieldMatchingRelationships< | ||
Schema, | ||
TableForwardRelationships<Schema, CurrentTableOrView>, | ||
Field | ||
> extends infer FoundByMatch | ||
? FoundByMatch extends GenericRelationship & { | ||
from: keyof TablesAndViews<Schema> | ||
} | ||
? { | ||
referencedTable: TablesAndViews<Schema>[FoundByMatch['from']] | ||
relation: FoundByMatch | ||
direction: 'forward' | ||
from: CurrentTableOrView | ||
type: 'found-by-match' | ||
} | ||
: FindJoinTableRelationship< | ||
Schema, | ||
CurrentTableOrView, | ||
Field['name'] | ||
> extends infer FoundByJoinTable | ||
? FoundByJoinTable extends GenericRelationship | ||
? { | ||
referencedTable: TablesAndViews<Schema>[FoundByJoinTable['referencedRelation']] | ||
relation: FoundByJoinTable & { match: 'refrel' } | ||
direction: 'forward' | ||
from: CurrentTableOrView | ||
type: 'found-by-join-table' | ||
} | ||
: SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> | ||
: SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> | ||
: SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> | ||
: SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> | ||
@@ -447,3 +465,3 @@ | ||
*/ | ||
export type FindJoinTableRelationship< | ||
type ResolveJoinTableRelationship< | ||
Schema extends GenericSchema, | ||
@@ -464,2 +482,11 @@ CurrentTableOrView extends keyof TablesAndViews<Schema> & string, | ||
export type FindJoinTableRelationship< | ||
Schema extends GenericSchema, | ||
CurrentTableOrView extends keyof TablesAndViews<Schema> & string, | ||
FieldName extends string | ||
> = ResolveJoinTableRelationship<Schema, CurrentTableOrView, FieldName> extends infer Result | ||
? [Result] extends [never] | ||
? false | ||
: Result | ||
: never | ||
/** | ||
@@ -472,10 +499,10 @@ * Finds a matching relationship based on the FieldNode's name and optional hint. | ||
Field extends Ast.FieldNode | ||
> = Field extends { hint: infer Hint extends string } | ||
> = Field extends { hint: string } | ||
? FindMatchingHintTableRelationships< | ||
Schema, | ||
Relationships, | ||
Hint, | ||
Field['hint'], | ||
Field['name'] | ||
> extends infer TableRelationViaHint extends GenericRelationship | ||
? TableRelationViaHint & { | ||
> extends GenericRelationship | ||
? FindMatchingHintTableRelationships<Schema, Relationships, Field['hint'], Field['name']> & { | ||
branch: 'found-in-table-via-hint' | ||
@@ -487,6 +514,6 @@ hint: Field['hint'] | ||
Relationships, | ||
Hint, | ||
Field['hint'], | ||
Field['name'] | ||
> extends infer TableViewViaHint extends GenericRelationship | ||
? TableViewViaHint & { | ||
> extends GenericRelationship | ||
? FindMatchingHintViewRelationships<Schema, Relationships, Field['hint'], Field['name']> & { | ||
branch: 'found-in-view-via-hint' | ||
@@ -496,17 +523,9 @@ hint: Field['hint'] | ||
: SelectQueryError<'Failed to find matching relation via hint'> | ||
: FindMatchingTableRelationships< | ||
Schema, | ||
Relationships, | ||
Field['name'] | ||
> extends infer TableRelationViaName extends GenericRelationship | ||
? TableRelationViaName & { | ||
: FindMatchingTableRelationships<Schema, Relationships, Field['name']> extends GenericRelationship | ||
? FindMatchingTableRelationships<Schema, Relationships, Field['name']> & { | ||
branch: 'found-in-table-via-name' | ||
name: Field['name'] | ||
} | ||
: FindMatchingViewRelationships< | ||
Schema, | ||
Relationships, | ||
Field['name'] | ||
> extends infer ViewRelationViaName extends GenericRelationship | ||
? ViewRelationViaName & { | ||
: FindMatchingViewRelationships<Schema, Relationships, Field['name']> extends GenericRelationship | ||
? FindMatchingViewRelationships<Schema, Relationships, Field['name']> & { | ||
branch: 'found-in-view-via-name' | ||
@@ -513,0 +532,0 @@ name: Field['name'] |
@@ -1,1 +0,1 @@ | ||
export const version = '1.17.2' | ||
export const version = '1.17.3' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
322481
5597