import { Ast } from './parser'; import { AggregateFunctions, ContainsNull, GenericRelationship, GenericSchema, GenericTable, IsNonEmptyArray, TablesAndViews, UnionToArray } from './types'; export declare type IsAny = 0 extends 1 & T ? true : false; export declare type SelectQueryError = { error: true; } & Message; export declare type DeduplicateRelationships = T extends readonly [ infer First, ...infer Rest ] ? First extends Rest[number] ? DeduplicateRelationships : [First, ...DeduplicateRelationships] : T; export declare type GetFieldNodeResultName = Field['alias'] extends string ? Field['alias'] : Field['aggregateFunction'] extends AggregateFunctions ? Field['aggregateFunction'] : Field['name']; declare type FilterRelationNodes = UnionToArray<{ [K in keyof Nodes]: Nodes[K] extends Ast.SpreadNode ? Nodes[K]['target'] : Nodes[K] extends Ast.FieldNode ? IsNonEmptyArray extends true ? Nodes[K] : never : never; }[number]>; declare type ResolveRelationships = UnionToArray<{ [K in keyof Nodes]: Nodes[K] extends Ast.FieldNode ? ResolveRelationship extends infer Relation ? Relation extends { relation: { referencedRelation: string; foreignKeyName: string; match: string; }; from: string; } ? { referencedTable: Relation['relation']['referencedRelation']; fkName: Relation['relation']['foreignKeyName']; from: Relation['from']; match: Relation['relation']['match']; fieldName: GetFieldNodeResultName; } : Relation : never : never; }>[0]; /** * Checks if a relation is implicitly referenced twice, requiring disambiguation */ declare type IsDoubleReference = T extends { referencedTable: infer RT; fieldName: infer FN; match: infer M; } ? M extends 'col' | 'refrel' ? U extends { referencedTable: RT; fieldName: FN; match: M; } ? true : false : false : false; /** * Compares one element with all other elements in the array to find duplicates */ declare type CheckDuplicates = Arr extends [infer Head, ...infer Tail] ? IsDoubleReference extends true ? Head | CheckDuplicates : CheckDuplicates : never; /** * Iterates over the elements of the array to find duplicates */ declare type FindDuplicatesWithinDeduplicated = Arr extends [infer Head, ...infer Tail] ? CheckDuplicates | FindDuplicatesWithinDeduplicated : never; declare type FindDuplicates = FindDuplicatesWithinDeduplicated>; export declare type CheckDuplicateEmbededReference = FilterRelationNodes extends infer RelationsNodes ? RelationsNodes extends Ast.FieldNode[] ? ResolveRelationships extends infer ResolvedRels ? ResolvedRels extends unknown[] ? FindDuplicates 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; /** * Returns a boolean representing whether there is a foreign key referencing * a given relation. */ declare type HasFKeyToFRel = Relationships extends [infer R] ? R extends { referencedRelation: FRelName; } ? true : false : Relationships extends [infer R, ...infer Rest] ? HasFKeyToFRel extends true ? true : HasFKeyToFRel : false; /** * Checks if there is more than one relation to a given foreign relation name in the Relationships. */ declare type HasMultipleFKeysToFRelDeduplicated = Relationships extends [ infer R, ...infer Rest ] ? R extends { referencedRelation: FRelName; } ? HasFKeyToFRel extends true ? true : HasMultipleFKeysToFRelDeduplicated : HasMultipleFKeysToFRelDeduplicated : false; declare type HasMultipleFKeysToFRel = HasMultipleFKeysToFRelDeduplicated>; declare type CheckRelationshipError & string, FoundRelation> = FoundRelation extends SelectQueryError ? FoundRelation : FoundRelation extends { relation: { referencedRelation: infer RelatedRelationName; name: string; }; direction: 'reverse'; } ? RelatedRelationName extends string ? HasMultipleFKeysToFRel 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}! ?`> : FoundRelation : never : FoundRelation extends { relation: { referencedRelation: infer RelatedRelationName; name: string; }; direction: 'forward'; from: infer From; } ? RelatedRelationName extends string ? From extends keyof TablesAndViews & string ? HasMultipleFKeysToFRel[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}! ?`> : FoundRelation : never : never : FoundRelation; /** * Resolves relationships for embedded resources and retrieves the referenced Table */ export declare type ResolveRelationship & string> = ResolveReverseRelationship extends infer ReverseRelationship ? ReverseRelationship extends false ? CheckRelationshipError> : CheckRelationshipError : never; /** * Resolves reverse relationships (from children to parent) */ declare type ResolveReverseRelationship & string> = FindFieldMatchingRelationships extends infer FoundRelation ? FoundRelation extends never ? false : FoundRelation extends { referencedRelation: infer RelatedRelationName; } ? RelatedRelationName extends string ? RelatedRelationName extends keyof TablesAndViews ? FoundRelation extends { hint: string; } ? { referencedTable: TablesAndViews[RelatedRelationName]; relation: FoundRelation; direction: 'reverse'; from: CurrentTableOrView; } : HasMultipleFKeysToFRel 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}! ?`> : { referencedTable: TablesAndViews[RelatedRelationName]; relation: FoundRelation; direction: 'reverse'; from: CurrentTableOrView; } : SelectQueryError<`Relation '${RelatedRelationName}' not found in schema.`> : false : false : false; export declare type FindMatchingTableRelationships = 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 : FindMatchingTableRelationships : false : false : false; export declare type FindMatchingViewRelationships = 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 : FindMatchingViewRelationships : false : false : false; export declare type FindMatchingHintTableRelationships = 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 : FindMatchingHintTableRelationships : false : false : false; export declare type FindMatchingHintViewRelationships = 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 : FindMatchingHintViewRelationships : false : false : false; declare type IsColumnsNullable, Columns extends (keyof Table['Row'])[]> = Columns extends [infer Column, ...infer Rest] ? Column extends keyof Table['Row'] ? ContainsNull extends true ? true : IsColumnsNullable : false : false; export declare type IsRelationNullable
= IsColumnsNullable; declare type TableForwardRelationships = TName extends keyof TablesAndViews ? UnionToArray>> extends infer R ? R extends (GenericRelationship & { from: keyof TablesAndViews; })[] ? R : [] : [] : []; declare type RecursivelyFindRelationships> = Keys extends infer K ? K extends keyof TablesAndViews ? FilterRelationships[K]['Relationships'], TName, K> extends never ? RecursivelyFindRelationships> : FilterRelationships[K]['Relationships'], TName, K> | RecursivelyFindRelationships> : false : false; declare type FilterRelationships = R extends readonly (infer Rel)[] ? Rel extends { referencedRelation: TName; } ? Rel & { from: From; } : never : never; export declare type ResolveForwardRelationship & string> = FindFieldMatchingRelationships[Field['name']]['Relationships'], Ast.FieldNode & { name: CurrentTableOrView; hint: Field['hint']; }> extends infer FoundByName ? FoundByName extends GenericRelationship ? { referencedTable: TablesAndViews[Field['name']]; relation: FoundByName; direction: 'forward'; from: Field['name']; type: 'found-by-name'; } : FindFieldMatchingRelationships, Field> extends infer FoundByMatch ? FoundByMatch extends GenericRelationship & { from: keyof TablesAndViews; } ? { referencedTable: TablesAndViews[FoundByMatch['from']]; relation: FoundByMatch; direction: 'forward'; from: CurrentTableOrView; type: 'found-by-match'; } : FindJoinTableRelationship extends infer FoundByJoinTable ? FoundByJoinTable extends GenericRelationship ? { referencedTable: TablesAndViews[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']}`>; /** * Given a CurrentTableOrView, finds all join tables to this relation. * For example, if products and categories are linked via product_categories table: * * @example * Given: * - CurrentTableView = 'products' * - FieldName = "categories" * * It should return this relationship from product_categories: * { * foreignKeyName: "product_categories_category_id_fkey", * columns: ["category_id"], * isOneToOne: false, * referencedRelation: "categories", * referencedColumns: ["id"] * } */ declare type ResolveJoinTableRelationship & string, FieldName extends string> = { [TableName in keyof TablesAndViews]: DeduplicateRelationships[TableName]['Relationships']> extends readonly (infer Rel)[] ? Rel extends { referencedRelation: CurrentTableOrView; } ? DeduplicateRelationships[TableName]['Relationships']> extends readonly (infer OtherRel)[] ? OtherRel extends { referencedRelation: FieldName; } ? OtherRel : never : never : never : never; }[keyof TablesAndViews]; export declare type FindJoinTableRelationship & string, FieldName extends string> = ResolveJoinTableRelationship extends infer Result ? [Result] extends [never] ? false : Result : never; /** * Finds a matching relationship based on the FieldNode's name and optional hint. */ export declare type FindFieldMatchingRelationships = Field extends { hint: string; } ? FindMatchingHintTableRelationships extends GenericRelationship ? FindMatchingHintTableRelationships & { branch: 'found-in-table-via-hint'; hint: Field['hint']; } : FindMatchingHintViewRelationships extends GenericRelationship ? FindMatchingHintViewRelationships & { branch: 'found-in-view-via-hint'; hint: Field['hint']; } : SelectQueryError<'Failed to find matching relation via hint'> : FindMatchingTableRelationships extends GenericRelationship ? FindMatchingTableRelationships & { branch: 'found-in-table-via-name'; name: Field['name']; } : FindMatchingViewRelationships extends GenericRelationship ? FindMatchingViewRelationships & { branch: 'found-in-view-via-name'; name: Field['name']; } : SelectQueryError<'Failed to find matching relation via name'>; export declare type JsonPathToAccessor = Path extends `${infer P1}->${infer P2}` ? P2 extends `>${infer Rest}` ? JsonPathToAccessor<`${P1}.${Rest}`> : P2 extends string ? JsonPathToAccessor<`${P1}.${P2}`> : Path : Path extends `>${infer Rest}` ? JsonPathToAccessor : Path extends `${infer P1}::${infer _}` ? JsonPathToAccessor : Path extends `${infer P1}${')' | ','}${infer _}` ? P1 : Path; export declare type JsonPathToType = Path extends '' ? T : ContainsNull extends true ? JsonPathToType, Path> : Path extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? JsonPathToType : never : Path extends keyof T ? T[Path] : never; export declare type IsStringUnion = string extends T ? false : T extends string ? [T] extends [never] ? false : true : false; export {}; //# sourceMappingURL=utils.d.ts.map