import { GenericTable } from '../types'; import { ContainsNull, GenericRelationship, PostgreSQLTypes } from './types'; import { Ast, ParseQuery } from './parser'; import { AggregateFunctions, ExtractFirstProperty, GenericSchema, IsNonEmptyArray, Prettify, TablesAndViews, TypeScriptTypes } from './types'; import { CheckDuplicateEmbededReference, GetFieldNodeResultName, IsAny, IsRelationNullable, IsStringUnion, JsonPathToType, ResolveRelationship, SelectQueryError } from './utils'; /** * Main entry point for constructing the result type of a PostgREST query. * * @param Schema - Database schema. * @param Row - The type of a row in the current table. * @param RelationName - The name of the current table or view. * @param Relationships - Relationships of the current table. * @param Query - The select query string literal to parse. */ export declare type GetResult, RelationName, Relationships, Query extends string> = IsAny extends true ? ParseQuery extends infer ParsedQuery ? ParsedQuery extends Ast.Node[] ? RelationName extends string ? ProcessNodesWithoutSchema : any : ParsedQuery : any : Relationships extends null ? ParseQuery extends infer ParsedQuery ? ParsedQuery extends Ast.Node[] ? RPCCallNodes : ParsedQuery : Row : ParseQuery extends infer ParsedQuery ? ParsedQuery extends Ast.Node[] ? RelationName extends string ? Relationships extends GenericRelationship[] ? ProcessNodes : SelectQueryError<'Invalid Relationships cannot infer result type'> : SelectQueryError<'Invalid RelationName cannot infer result type'> : ParsedQuery : never; declare type ProcessSimpleFieldWithoutSchema = Field['aggregateFunction'] extends AggregateFunctions ? { [K in GetFieldNodeResultName]: Field['castType'] extends PostgreSQLTypes ? TypeScriptTypes : number; } : { [K in GetFieldNodeResultName]: Field['castType'] extends PostgreSQLTypes ? TypeScriptTypes : any; }; declare type ProcessFieldNodeWithoutSchema = IsNonEmptyArray extends true ? { [K in GetFieldNodeResultName]: Node['children'] extends Ast.Node[] ? ProcessNodesWithoutSchema[] : ProcessSimpleFieldWithoutSchema; } : ProcessSimpleFieldWithoutSchema; /** * Processes a single Node without schema and returns the resulting TypeScript type. */ declare type ProcessNodeWithoutSchema = Node extends Ast.StarNode ? any : Node extends Ast.SpreadNode ? Node['target']['children'] extends Ast.StarNode[] ? any : Node['target']['children'] extends Ast.FieldNode[] ? { [P in Node['target']['children'][number] as GetFieldNodeResultName

]: P['castType'] extends PostgreSQLTypes ? TypeScriptTypes : any; } : any : Node extends Ast.FieldNode ? ProcessFieldNodeWithoutSchema : any; /** * Processes nodes when Schema is any, providing basic type inference */ declare type ProcessNodesWithoutSchema = {}> = Nodes extends [infer FirstNode, ...infer RestNodes] ? FirstNode extends Ast.Node ? RestNodes extends Ast.Node[] ? ProcessNodeWithoutSchema extends infer FieldResult ? FieldResult extends Record ? ProcessNodesWithoutSchema : FieldResult : any : any : any : Prettify; /** * Processes a single Node from a select chained after a rpc call * * @param Row - The type of a row in the current table. * @param RelationName - The name of the current rpc function * @param NodeType - The Node to process. */ export declare type ProcessRPCNode, RelationName extends string, NodeType extends Ast.Node> = NodeType['type'] extends Ast.StarNode['type'] ? Row : NodeType['type'] extends Ast.FieldNode['type'] ? ProcessSimpleField> : SelectQueryError<'RPC Unsupported node type.'>; /** * Process select call that can be chained after an rpc call */ export declare type RPCCallNodes, Acc extends Record = {}> = Nodes extends [infer FirstNode, ...infer RestNodes] ? FirstNode extends Ast.Node ? RestNodes extends Ast.Node[] ? ProcessRPCNode extends infer FieldResult ? FieldResult extends Record ? RPCCallNodes : FieldResult extends SelectQueryError ? SelectQueryError : 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; /** * Recursively processes an array of Nodes and accumulates the resulting TypeScript type. * * @param Schema - Database schema. * @param Row - The type of a row in the current table. * @param RelationName - The name of the current table or view. * @param Relationships - Relationships of the current table. * @param Nodes - An array of AST nodes to process. * @param Acc - Accumulator for the constructed type. */ export declare type ProcessNodes, RelationName extends string, Relationships extends GenericRelationship[], Nodes extends Ast.Node[], Acc extends Record = {}> = CheckDuplicateEmbededReference extends false ? Nodes extends [infer FirstNode, ...infer RestNodes] ? FirstNode extends Ast.Node ? RestNodes extends Ast.Node[] ? ProcessNode extends infer FieldResult ? FieldResult extends Record ? ProcessNodes : FieldResult extends SelectQueryError ? SelectQueryError : 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 : Prettify>; /** * Processes a single Node and returns the resulting TypeScript type. * * @param Schema - Database schema. * @param Row - The type of a row in the current table. * @param RelationName - The name of the current table or view. * @param Relationships - Relationships of the current table. * @param NodeType - The Node to process. */ export declare type ProcessNode, RelationName extends string, Relationships extends GenericRelationship[], NodeType extends Ast.Node> = NodeType['type'] extends Ast.StarNode['type'] ? Row : NodeType['type'] extends Ast.SpreadNode['type'] ? ProcessSpreadNode> : NodeType['type'] extends Ast.FieldNode['type'] ? ProcessFieldNode> : SelectQueryError<'Unsupported node type.'>; /** * Processes a FieldNode and returns the resulting TypeScript type. * * @param Schema - Database schema. * @param Row - The type of a row in the current table. * @param RelationName - The name of the current table or view. * @param Relationships - Relationships of the current table. * @param Field - The FieldNode to process. */ declare type ProcessFieldNode, RelationName extends string, Relationships extends GenericRelationship[], Field extends Ast.FieldNode> = Field['children'] extends [] ? {} : IsNonEmptyArray extends true ? ProcessEmbeddedResource : ProcessSimpleField; declare type ResolveJsonPathType = Path extends string ? JsonPathToType extends never ? TypeScriptTypes : JsonPathToType extends infer PathResult ? PathResult extends string ? PathResult : IsStringUnion extends true ? PathResult : CastType extends 'json' ? PathResult : TypeScriptTypes : TypeScriptTypes : TypeScriptTypes; /** * Processes a simple field (without embedded resources). * * @param Row - The type of a row in the current table. * @param RelationName - The name of the current table or view. * @param Field - The FieldNode to process. */ declare type ProcessSimpleField, RelationName extends string, Field extends Ast.FieldNode> = Field['name'] extends keyof Row | 'count' ? Field['aggregateFunction'] extends AggregateFunctions ? { [K in GetFieldNodeResultName]: Field['castType'] extends PostgreSQLTypes ? TypeScriptTypes : number; } : { [K in GetFieldNodeResultName]: Field['castType'] extends PostgreSQLTypes ? ResolveJsonPathType : Row[Field['name']]; } : SelectQueryError<`column '${Field['name']}' does not exist on '${RelationName}'.`>; /** * Processes an embedded resource (relation). * * @param Schema - Database schema. * @param Row - The type of a row in the current table. * @param RelationName - The name of the current table or view. * @param Relationships - Relationships of the current table. * @param Field - The FieldNode to process. */ export declare type ProcessEmbeddedResource & string> = ResolveRelationship extends infer Resolved ? Resolved extends { referencedTable: Pick; relation: GenericRelationship & { match: 'refrel' | 'col' | 'fkname'; }; direction: string; } ? ProcessEmbeddedResourceResult : { [K in GetFieldNodeResultName]: Resolved; } : { [K in GetFieldNodeResultName]: SelectQueryError<'Failed to resolve relationship.'> & string; }; /** * Helper type to process the result of an embedded resource. */ declare type ProcessEmbeddedResourceResult; relation: GenericRelationship & { match: 'refrel' | 'col' | 'fkname'; }; direction: string; }, Field extends Ast.FieldNode, CurrentTableOrView extends keyof TablesAndViews> = ProcessNodes extends Ast.Node[] ? Exclude : []> extends infer ProcessedChildren ? { [K in GetFieldNodeResultName]: Resolved['direction'] extends 'forward' ? Field extends { innerJoin: true; } ? Resolved['relation']['isOneToOne'] extends true ? ProcessedChildren : ProcessedChildren[] : Resolved['relation']['isOneToOne'] extends true ? ProcessedChildren | null : ProcessedChildren[] : Resolved['relation']['referencedRelation'] extends CurrentTableOrView ? Resolved['relation']['match'] extends 'col' ? IsRelationNullable[CurrentTableOrView], Resolved['relation']> extends true ? ProcessedChildren | null : ProcessedChildren : ProcessedChildren[] : IsRelationNullable[CurrentTableOrView], Resolved['relation']> extends true ? Field extends { innerJoin: true; } ? ProcessedChildren : ProcessedChildren | null : ProcessedChildren; } : { [K in GetFieldNodeResultName]: SelectQueryError<'Failed to process embedded resource nodes.'> & string; }; /** * Processes a SpreadNode by processing its target node. * * @param Schema - Database schema. * @param Row - The type of a row in the current table. * @param RelationName - The name of the current table or view. * @param Relationships - Relationships of the current table. * @param Spread - The SpreadNode to process. */ declare type ProcessSpreadNode, RelationName extends string, Relationships extends GenericRelationship[], Spread extends Ast.SpreadNode> = ProcessNode extends infer Result ? Result extends SelectQueryError ? SelectQueryError : ExtractFirstProperty extends unknown[] ? { [K in Spread['target']['name']]: SelectQueryError<`"${RelationName}" and "${Spread['target']['name']}" do not form a many-to-one or one-to-one relationship spread not possible`>; } : ProcessSpreadNodeResult : never; /** * Helper type to process the result of a spread node. */ declare type ProcessSpreadNodeResult = Result extends Record | null> ? Result : ExtractFirstProperty extends infer SpreadedObject ? ContainsNull extends true ? Exclude<{ [K in keyof SpreadedObject]: SpreadedObject[K] | null; }, null> : Exclude<{ [K in keyof SpreadedObject]: SpreadedObject[K]; }, null> : SelectQueryError<'An error occurred spreading the object'>; export {}; //# sourceMappingURL=result.d.ts.map