@supabase/postgrest-js
Advanced tools
Comparing version
@@ -17,4 +17,4 @@ import PostgrestClient from './PostgrestClient'; | ||
export default _default; | ||
export type { PostgrestResponse, PostgrestResponseFailure, PostgrestResponseSuccess, PostgrestSingleResponse, PostgrestMaybeSingleResponse, } from './types'; | ||
export type { PostgrestResponse, PostgrestResponseFailure, PostgrestResponseSuccess, PostgrestSingleResponse, PostgrestMaybeSingleResponse, ClientServerOptions, } from './types'; | ||
export type { GetResult as UnstableGetResult } from './select-query-parser/result'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,4 +0,4 @@ | ||
import type { Fetch, PostgrestSingleResponse, PostgrestResponseSuccess, CheckMatchingArrayTypes, MergePartialResult, IsValidResultOverride } from './types'; | ||
import type { Fetch, PostgrestSingleResponse, PostgrestResponseSuccess, CheckMatchingArrayTypes, MergePartialResult, IsValidResultOverride, ClientServerOptions } from './types'; | ||
import { ContainsNull } from './select-query-parser/types'; | ||
export default abstract class PostgrestBuilder<Result, ThrowOnError extends boolean = false> implements PromiseLike<ThrowOnError extends true ? PostgrestResponseSuccess<Result> : PostgrestSingleResponse<Result>> { | ||
export default abstract class PostgrestBuilder<ClientOptions extends ClientServerOptions, Result, ThrowOnError extends boolean = false> implements PromiseLike<ThrowOnError extends true ? PostgrestResponseSuccess<Result> : PostgrestSingleResponse<Result>> { | ||
protected method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'; | ||
@@ -13,3 +13,3 @@ protected url: URL; | ||
protected isMaybeSingle: boolean; | ||
constructor(builder: PostgrestBuilder<Result>); | ||
constructor(builder: PostgrestBuilder<ClientOptions, Result>); | ||
/** | ||
@@ -21,3 +21,3 @@ * If there's an error with the query, throwOnError will reject the promise by | ||
*/ | ||
throwOnError(): this & PostgrestBuilder<Result, true>; | ||
throwOnError(): this & PostgrestBuilder<ClientOptions, Result, true>; | ||
/** | ||
@@ -34,3 +34,3 @@ * Set an HTTP header for the request. | ||
*/ | ||
returns<NewResult>(): PostgrestBuilder<CheckMatchingArrayTypes<Result, NewResult>, ThrowOnError>; | ||
returns<NewResult>(): PostgrestBuilder<ClientOptions, CheckMatchingArrayTypes<Result, NewResult>, ThrowOnError>; | ||
/** | ||
@@ -62,4 +62,4 @@ * Override the type of the returned `data` field in the response. | ||
merge: true; | ||
}>(): PostgrestBuilder<IsValidResultOverride<Result, NewResult, false, false> extends true ? ContainsNull<Result> extends true ? MergePartialResult<NewResult, NonNullable<Result>, Options> | null : MergePartialResult<NewResult, Result, Options> : CheckMatchingArrayTypes<Result, NewResult>, ThrowOnError>; | ||
}>(): PostgrestBuilder<ClientOptions, IsValidResultOverride<Result, NewResult, false, false> extends true ? ContainsNull<Result> extends true ? MergePartialResult<NewResult, NonNullable<Result>, Options> | null : MergePartialResult<NewResult, Result, Options> : CheckMatchingArrayTypes<Result, NewResult>, ThrowOnError>; | ||
} | ||
//# sourceMappingURL=PostgrestBuilder.d.ts.map |
import PostgrestQueryBuilder from './PostgrestQueryBuilder'; | ||
import PostgrestFilterBuilder from './PostgrestFilterBuilder'; | ||
import { Fetch, GenericSchema } from './types'; | ||
import { Fetch, GenericSchema, ClientServerOptions, GetGenericDatabaseWithOptions } from './types'; | ||
/** | ||
@@ -14,3 +14,5 @@ * PostgREST client. | ||
*/ | ||
export default class PostgrestClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any> { | ||
export default class PostgrestClient<Database = any, ClientOptions extends ClientServerOptions = GetGenericDatabaseWithOptions<Database, { | ||
PostgrestVersion: '12'; | ||
}>['options'], SchemaName extends string & keyof GetGenericDatabaseWithOptions<Database>['db'] = 'public' extends keyof GetGenericDatabaseWithOptions<Database>['db'] ? 'public' : string & keyof GetGenericDatabaseWithOptions<Database>['db'], Schema extends GenericSchema = GetGenericDatabaseWithOptions<Database>['db'][SchemaName] extends GenericSchema ? GetGenericDatabaseWithOptions<Database>['db'][SchemaName] : any> { | ||
url: string; | ||
@@ -34,4 +36,4 @@ headers: Record<string, string>; | ||
}); | ||
from<TableName extends string & keyof Schema['Tables'], Table extends Schema['Tables'][TableName]>(relation: TableName): PostgrestQueryBuilder<Schema, Table, TableName>; | ||
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(relation: ViewName): PostgrestQueryBuilder<Schema, View, ViewName>; | ||
from<TableName extends string & keyof Schema['Tables'], Table extends Schema['Tables'][TableName]>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>; | ||
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(relation: ViewName): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName>; | ||
/** | ||
@@ -44,3 +46,3 @@ * Select a schema to query or perform an function (rpc) call. | ||
*/ | ||
schema<DynamicSchema extends string & keyof Database>(schema: DynamicSchema): PostgrestClient<Database, DynamicSchema, Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any>; | ||
schema<DynamicSchema extends string & keyof GetGenericDatabaseWithOptions<Database>['db']>(schema: DynamicSchema): PostgrestClient<Database, ClientOptions, DynamicSchema, Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any>; | ||
/** | ||
@@ -73,4 +75,4 @@ * Perform a function call. | ||
count?: 'exact' | 'planned' | 'estimated'; | ||
}): PostgrestFilterBuilder<Schema, Fn['Returns'] extends any[] ? Fn['Returns'][number] extends Record<string, unknown> ? Fn['Returns'][number] : never : never, Fn['Returns'], FnName, null>; | ||
}): PostgrestFilterBuilder<ClientOptions, Schema, Fn['Returns'] extends any[] ? Fn['Returns'][number] extends Record<string, unknown> ? Fn['Returns'][number] : never : never, Fn['Returns'], FnName, null>; | ||
} | ||
//# sourceMappingURL=PostgrestClient.d.ts.map |
import PostgrestTransformBuilder from './PostgrestTransformBuilder'; | ||
import { JsonPathToAccessor, JsonPathToType } from './select-query-parser/utils'; | ||
import { GenericSchema } from './types'; | ||
import { ClientServerOptions, GenericSchema } from './types'; | ||
declare type FilterOperator = 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'like' | 'ilike' | 'is' | 'in' | 'cs' | 'cd' | 'sl' | 'sr' | 'nxl' | 'nxr' | 'adj' | 'ov' | 'fts' | 'plfts' | 'phfts' | 'wfts'; | ||
@@ -8,3 +8,6 @@ export declare type IsStringOperator<Path extends string> = Path extends `${string}->>${string}` ? true : false; | ||
declare type ResolveFilterRelationshipValue<Schema extends GenericSchema, RelationshipTable extends string, RelationshipColumn extends string> = Schema['Tables'] & Schema['Views'] extends infer TablesAndViews ? RelationshipTable extends keyof TablesAndViews ? 'Row' extends keyof TablesAndViews[RelationshipTable] ? RelationshipColumn extends keyof TablesAndViews[RelationshipTable]['Row'] ? TablesAndViews[RelationshipTable]['Row'][RelationshipColumn] : unknown : unknown : unknown : never; | ||
export default class PostgrestFilterBuilder<Schema extends GenericSchema, Row extends Record<string, unknown>, Result, RelationName = unknown, Relationships = unknown> extends PostgrestTransformBuilder<Schema, Row, Result, RelationName, Relationships> { | ||
export declare type InvalidMethodError<S extends string> = { | ||
Error: S; | ||
}; | ||
export default class PostgrestFilterBuilder<ClientOptions extends ClientServerOptions, Schema extends GenericSchema, Row extends Record<string, unknown>, Result, RelationName = unknown, Relationships = unknown> extends PostgrestTransformBuilder<ClientOptions, Schema, Row, Result, RelationName, Relationships> { | ||
/** | ||
@@ -11,0 +14,0 @@ * Match only rows where `column` is equal to `value`. |
import PostgrestFilterBuilder from './PostgrestFilterBuilder'; | ||
import { GetResult } from './select-query-parser/result'; | ||
import { Fetch, GenericSchema, GenericTable, GenericView } from './types'; | ||
export default class PostgrestQueryBuilder<Schema extends GenericSchema, Relation extends GenericTable | GenericView, RelationName = unknown, Relationships = Relation extends { | ||
import { ClientServerOptions, Fetch, GenericSchema, GenericTable, GenericView } from './types'; | ||
export default class PostgrestQueryBuilder<ClientOptions extends ClientServerOptions, Schema extends GenericSchema, Relation extends GenericTable | GenericView, RelationName = unknown, Relationships = Relation extends { | ||
Relationships: infer R; | ||
@@ -38,6 +38,6 @@ } ? R : unknown> { | ||
*/ | ||
select<Query extends string = '*', ResultOne = GetResult<Schema, Relation['Row'], RelationName, Relationships, Query>>(columns?: Query, { head, count, }?: { | ||
select<Query extends string = '*', ResultOne = GetResult<Schema, Relation['Row'], RelationName, Relationships, Query, ClientOptions>>(columns?: Query, { head, count, }?: { | ||
head?: boolean; | ||
count?: 'exact' | 'planned' | 'estimated'; | ||
}): PostgrestFilterBuilder<Schema, Relation['Row'], ResultOne[], RelationName, Relationships>; | ||
}): PostgrestFilterBuilder<ClientOptions, Schema, Relation['Row'], ResultOne[], RelationName, Relationships>; | ||
insert<Row extends Relation extends { | ||
@@ -47,3 +47,3 @@ Insert: unknown; | ||
count?: 'exact' | 'planned' | 'estimated'; | ||
}): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships>; | ||
}): PostgrestFilterBuilder<ClientOptions, Schema, Relation['Row'], null, RelationName, Relationships>; | ||
insert<Row extends Relation extends { | ||
@@ -54,3 +54,3 @@ Insert: unknown; | ||
defaultToNull?: boolean; | ||
}): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships>; | ||
}): PostgrestFilterBuilder<ClientOptions, Schema, Relation['Row'], null, RelationName, Relationships>; | ||
upsert<Row extends Relation extends { | ||
@@ -62,3 +62,3 @@ Insert: unknown; | ||
count?: 'exact' | 'planned' | 'estimated'; | ||
}): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships>; | ||
}): PostgrestFilterBuilder<ClientOptions, Schema, Relation['Row'], null, RelationName, Relationships>; | ||
upsert<Row extends Relation extends { | ||
@@ -71,3 +71,3 @@ Insert: unknown; | ||
defaultToNull?: boolean; | ||
}): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships>; | ||
}): PostgrestFilterBuilder<ClientOptions, Schema, Relation['Row'], null, RelationName, Relationships>; | ||
/** | ||
@@ -98,3 +98,3 @@ * Perform an UPDATE on the table or view. | ||
count?: 'exact' | 'planned' | 'estimated'; | ||
}): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships>; | ||
}): PostgrestFilterBuilder<ClientOptions, Schema, Relation['Row'], null, RelationName, Relationships>; | ||
/** | ||
@@ -121,4 +121,4 @@ * Perform a DELETE on the table or view. | ||
count?: 'exact' | 'planned' | 'estimated'; | ||
}): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships>; | ||
}): PostgrestFilterBuilder<ClientOptions, Schema, Relation['Row'], null, RelationName, Relationships>; | ||
} | ||
//# sourceMappingURL=PostgrestQueryBuilder.d.ts.map |
import PostgrestBuilder from './PostgrestBuilder'; | ||
import { GetResult } from './select-query-parser/result'; | ||
import { GenericSchema, CheckMatchingArrayTypes } from './types'; | ||
export default class PostgrestTransformBuilder<Schema extends GenericSchema, Row extends Record<string, unknown>, Result, RelationName = unknown, Relationships = unknown> extends PostgrestBuilder<Result> { | ||
import { GenericSchema, CheckMatchingArrayTypes, ClientServerOptions } from './types'; | ||
export default class PostgrestTransformBuilder<ClientOptions extends ClientServerOptions, Schema extends GenericSchema, Row extends Record<string, unknown>, Result, RelationName = unknown, Relationships = unknown, Method = unknown> extends PostgrestBuilder<ClientOptions, Result> { | ||
/** | ||
@@ -14,3 +14,3 @@ * Perform a SELECT on the query result. | ||
*/ | ||
select<Query extends string = '*', NewResultOne = GetResult<Schema, Row, RelationName, Relationships, Query>>(columns?: Query): PostgrestTransformBuilder<Schema, Row, NewResultOne[], RelationName, Relationships>; | ||
select<Query extends string = '*', NewResultOne = GetResult<Schema, Row, RelationName, Relationships, Query, ClientOptions>>(columns?: Query): PostgrestTransformBuilder<ClientOptions, Schema, Row, NewResultOne[], RelationName, Relationships, Method>; | ||
order<ColumnName extends string & keyof Row>(column: ColumnName, options?: { | ||
@@ -87,3 +87,3 @@ ascending?: boolean; | ||
*/ | ||
single<ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never>(): PostgrestBuilder<ResultOne>; | ||
single<ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never>(): PostgrestBuilder<ClientOptions, ResultOne>; | ||
/** | ||
@@ -95,11 +95,11 @@ * Return `data` as a single object instead of an array of objects. | ||
*/ | ||
maybeSingle<ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never>(): PostgrestBuilder<ResultOne | null>; | ||
maybeSingle<ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never>(): PostgrestBuilder<ClientOptions, ResultOne | null>; | ||
/** | ||
* Return `data` as a string in CSV format. | ||
*/ | ||
csv(): PostgrestBuilder<string>; | ||
csv(): PostgrestBuilder<ClientOptions, string>; | ||
/** | ||
* Return `data` as an object in [GeoJSON](https://geojson.org) format. | ||
*/ | ||
geojson(): PostgrestBuilder<Record<string, unknown>>; | ||
geojson(): PostgrestBuilder<ClientOptions, Record<string, unknown>>; | ||
/** | ||
@@ -137,3 +137,3 @@ * Return `data` as the EXPLAIN plan for the query. | ||
format?: 'json' | 'text'; | ||
}): PostgrestBuilder<Record<string, unknown>[]> | PostgrestBuilder<string>; | ||
}): PostgrestBuilder<ClientOptions, Record<string, unknown>[]> | PostgrestBuilder<ClientOptions, string>; | ||
/** | ||
@@ -151,4 +151,4 @@ * Rollback the query. | ||
*/ | ||
returns<NewResult>(): PostgrestTransformBuilder<Schema, Row, CheckMatchingArrayTypes<Result, NewResult>, RelationName, Relationships>; | ||
returns<NewResult>(): PostgrestTransformBuilder<ClientOptions, Schema, Row, CheckMatchingArrayTypes<Result, NewResult>, RelationName, Relationships, Method>; | ||
} | ||
//# sourceMappingURL=PostgrestTransformBuilder.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { GenericTable } from '../types'; | ||
import { ClientServerOptions, GenericTable } from '../types'; | ||
import { ContainsNull, GenericRelationship, PostgreSQLTypes } from './types'; | ||
@@ -6,2 +6,3 @@ import { Ast, ParseQuery } from './parser'; | ||
import { CheckDuplicateEmbededReference, GetFieldNodeResultName, IsAny, IsRelationNullable, IsStringUnion, JsonPathToType, ResolveRelationship, SelectQueryError } from './utils'; | ||
export declare type SpreadOnManyEnabled<PostgrestVersion extends string | undefined> = PostgrestVersion extends `13${string}` ? true : false; | ||
/** | ||
@@ -16,3 +17,3 @@ * Main entry point for constructing the result type of a PostgREST query. | ||
*/ | ||
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; | ||
export declare type GetResult<Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName, Relationships, Query extends string, ClientOptions extends ClientServerOptions> = 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<ClientOptions, 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 ? { | ||
@@ -58,3 +59,3 @@ [K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes ? TypeScriptTypes<Field['castType']> : number; | ||
*/ | ||
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>>; | ||
export declare type ProcessNodes<ClientOptions extends ClientServerOptions, 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<ClientOptions, Schema, Row, RelationName, Relationships, FirstNode> extends infer FieldResult ? FieldResult extends Record<string, unknown> ? ProcessNodes<ClientOptions, 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>>; | ||
/** | ||
@@ -69,3 +70,3 @@ * Processes a single Node and returns the resulting TypeScript type. | ||
*/ | ||
export declare type ProcessNode<Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName extends string, Relationships extends GenericRelationship[], NodeType extends Ast.Node> = NodeType['type'] extends Ast.StarNode['type'] ? Row : NodeType['type'] extends Ast.SpreadNode['type'] ? ProcessSpreadNode<Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.SpreadNode>> : NodeType['type'] extends Ast.FieldNode['type'] ? ProcessFieldNode<Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.FieldNode>> : SelectQueryError<'Unsupported node type.'>; | ||
export declare type ProcessNode<ClientOptions extends ClientServerOptions, Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName extends string, Relationships extends GenericRelationship[], NodeType extends Ast.Node> = NodeType['type'] extends Ast.StarNode['type'] ? Row : NodeType['type'] extends Ast.SpreadNode['type'] ? ProcessSpreadNode<ClientOptions, Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.SpreadNode>> : NodeType['type'] extends Ast.FieldNode['type'] ? ProcessFieldNode<ClientOptions, Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.FieldNode>> : SelectQueryError<'Unsupported node type.'>; | ||
/** | ||
@@ -80,3 +81,3 @@ * Processes a FieldNode and returns the resulting TypeScript type. | ||
*/ | ||
declare type ProcessFieldNode<Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName extends string, Relationships extends GenericRelationship[], Field extends Ast.FieldNode> = Field['children'] extends [] ? {} : IsNonEmptyArray<Field['children']> extends true ? ProcessEmbeddedResource<Schema, Relationships, Field, RelationName> : ProcessSimpleField<Row, RelationName, Field>; | ||
declare type ProcessFieldNode<ClientOptions extends ClientServerOptions, Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName extends string, Relationships extends GenericRelationship[], Field extends Ast.FieldNode> = Field['children'] extends [] ? {} : IsNonEmptyArray<Field['children']> extends true ? ProcessEmbeddedResource<ClientOptions, Schema, Relationships, Field, RelationName> : ProcessSimpleField<Row, RelationName, Field>; | ||
declare type ResolveJsonPathType<Value, Path extends string | undefined, CastType extends PostgreSQLTypes> = Path extends string ? JsonPathToType<Value, Path> extends never ? TypeScriptTypes<CastType> : JsonPathToType<Value, Path> extends infer PathResult ? PathResult extends string ? PathResult : IsStringUnion<PathResult> extends true ? PathResult : CastType extends 'json' ? PathResult : TypeScriptTypes<CastType> : TypeScriptTypes<CastType> : TypeScriptTypes<CastType>; | ||
@@ -104,3 +105,3 @@ /** | ||
*/ | ||
export declare type ProcessEmbeddedResource<Schema extends GenericSchema, Relationships extends GenericRelationship[], Field extends Ast.FieldNode, CurrentTableOrView extends keyof TablesAndViews<Schema> & string> = ResolveRelationship<Schema, Relationships, Field, CurrentTableOrView> extends infer Resolved ? Resolved extends { | ||
export declare type ProcessEmbeddedResource<ClientOptions extends ClientServerOptions, Schema extends GenericSchema, Relationships extends GenericRelationship[], Field extends Ast.FieldNode, CurrentTableOrView extends keyof TablesAndViews<Schema> & string> = ResolveRelationship<Schema, Relationships, Field, CurrentTableOrView> extends infer Resolved ? Resolved extends { | ||
referencedTable: Pick<GenericTable, 'Row' | 'Relationships'>; | ||
@@ -111,3 +112,3 @@ relation: GenericRelationship & { | ||
direction: string; | ||
} ? ProcessEmbeddedResourceResult<Schema, Resolved, Field, CurrentTableOrView> : { | ||
} ? ProcessEmbeddedResourceResult<ClientOptions, Schema, Resolved, Field, CurrentTableOrView> : { | ||
[K in GetFieldNodeResultName<Field>]: Resolved; | ||
@@ -120,3 +121,3 @@ } : { | ||
*/ | ||
declare type ProcessEmbeddedResourceResult<Schema extends GenericSchema, Resolved extends { | ||
declare type ProcessEmbeddedResourceResult<ClientOptions extends ClientServerOptions, Schema extends GenericSchema, Resolved extends { | ||
referencedTable: Pick<GenericTable, 'Row' | 'Relationships'>; | ||
@@ -127,3 +128,3 @@ relation: GenericRelationship & { | ||
direction: string; | ||
}, Field extends Ast.FieldNode, CurrentTableOrView extends keyof TablesAndViews<Schema>> = ProcessNodes<Schema, Resolved['referencedTable']['Row'], Field['name'], Resolved['referencedTable']['Relationships'], Field['children'] extends undefined ? [] : Exclude<Field['children'], undefined> extends Ast.Node[] ? Exclude<Field['children'], undefined> : []> extends infer ProcessedChildren ? { | ||
}, Field extends Ast.FieldNode, CurrentTableOrView extends keyof TablesAndViews<Schema>> = ProcessNodes<ClientOptions, Schema, Resolved['referencedTable']['Row'], Field['name'], Resolved['referencedTable']['Relationships'], Field['children'] extends undefined ? [] : Exclude<Field['children'], undefined> extends Ast.Node[] ? Exclude<Field['children'], undefined> : []> extends infer ProcessedChildren ? { | ||
[K in GetFieldNodeResultName<Field>]: Resolved['direction'] extends 'forward' ? Field extends { | ||
@@ -146,6 +147,13 @@ innerJoin: true; | ||
*/ | ||
declare type ProcessSpreadNode<Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName extends string, Relationships extends GenericRelationship[], Spread extends Ast.SpreadNode> = ProcessNode<Schema, Row, RelationName, Relationships, Spread['target']> extends infer Result ? Result extends SelectQueryError<infer E> ? SelectQueryError<E> : ExtractFirstProperty<Result> extends unknown[] ? { | ||
declare type ProcessSpreadNode<ClientOptions extends ClientServerOptions, Schema extends GenericSchema, Row extends Record<string, unknown>, RelationName extends string, Relationships extends GenericRelationship[], Spread extends Ast.SpreadNode> = ProcessNode<ClientOptions, Schema, Row, RelationName, Relationships, Spread['target']> extends infer Result ? Result extends SelectQueryError<infer E> ? SelectQueryError<E> : ExtractFirstProperty<Result> extends unknown[] ? SpreadOnManyEnabled<ClientOptions['PostgrestVersion']> extends true ? ProcessManyToManySpreadNodeResult<Result> : { | ||
[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<Result> : never; | ||
/** | ||
* Helper type to process the result of a many-to-many spread node. | ||
* Converts all fields in the spread object into arrays. | ||
*/ | ||
declare type ProcessManyToManySpreadNodeResult<Result> = Result extends Record<string, SelectQueryError<string> | null> ? Result : ExtractFirstProperty<Result> extends infer SpreadedObject ? SpreadedObject extends Array<Record<string, unknown>> ? { | ||
[K in keyof SpreadedObject[number]]: Array<SpreadedObject[number][K]>; | ||
} : SelectQueryError<'An error occurred spreading the many-to-many object'> : SelectQueryError<'An error occurred spreading the many-to-many object'>; | ||
/** | ||
* Helper type to process the result of a spread node. | ||
@@ -152,0 +160,0 @@ */ |
import PostgrestError from './PostgrestError'; | ||
import { ContainsNull } from './select-query-parser/types'; | ||
import { SelectQueryError } from './select-query-parser/utils'; | ||
import { IsAny, SelectQueryError } from './select-query-parser/utils'; | ||
export declare type Fetch = typeof fetch; | ||
@@ -60,2 +60,13 @@ /** | ||
}; | ||
export declare type ClientServerOptions = { | ||
PostgrestVersion?: string; | ||
}; | ||
export declare type DatabaseWithOptions<Database, Options extends ClientServerOptions> = { | ||
db: Database; | ||
options: Options; | ||
}; | ||
declare const INTERNAL_SUPABASE_OPTIONS = "__InternalSupabase"; | ||
export declare type GetGenericDatabaseWithOptions<Database, Opts extends ClientServerOptions = { | ||
PostgrestVersion: '12'; | ||
}> = IsAny<Database> extends true ? DatabaseWithOptions<Database, Opts> : typeof INTERNAL_SUPABASE_OPTIONS extends keyof Database ? Database[typeof INTERNAL_SUPABASE_OPTIONS] extends ClientServerOptions ? DatabaseWithOptions<Omit<Database, typeof INTERNAL_SUPABASE_OPTIONS>, Database[typeof INTERNAL_SUPABASE_OPTIONS]> : DatabaseWithOptions<Omit<Database, typeof INTERNAL_SUPABASE_OPTIONS>, Opts> : DatabaseWithOptions<Database, Opts>; | ||
export declare type Prettify<T> = { | ||
@@ -62,0 +73,0 @@ [K in keyof T]: T[K]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const INTERNAL_SUPABASE_OPTIONS = '__InternalSupabase'; | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@supabase/postgrest-js", | ||
"version": "1.19.4", | ||
"version": "1.20.0", | ||
"description": "Isomorphic PostgREST client", | ||
@@ -42,6 +42,8 @@ "keywords": [ | ||
"test:run": "jest --runInBand --coverage", | ||
"test:update": "run-s db:clean db:run && jest --runInBand --updateSnapshot && run-s db:clean", | ||
"test:update": "run-s db:clean db:run db:generate-test-types && jest --runInBand --updateSnapshot && run-s db:clean", | ||
"test:types": "run-s build && tsd --files 'test/**/*.test-d.ts'", | ||
"test:types:watch": "run-s build && tsd --files 'test/**/*.test-d.ts' --watch", | ||
"db:clean": "cd test/db && docker compose down --volumes", | ||
"db:run": "cd test/db && docker compose up --detach && wait-for-localhost 3000" | ||
"db:run": "cd test/db && docker compose up --detach && wait-for-localhost 3000", | ||
"db:generate-test-types": "cd test/db && docker compose up --detach && wait-for-localhost 8080 && curl --location 'http://0.0.0.0:8080/generators/typescript?included_schemas=public,personal&detect_one_to_one_relationships=true' > ../types.generated.ts && sed -i 's/export type Json = .*/export type Json = unknown;/' ../types.generated.ts" | ||
}, | ||
@@ -48,0 +50,0 @@ "dependencies": { |
@@ -31,2 +31,3 @@ // Always update wrapper.mjs when updating this file. | ||
PostgrestMaybeSingleResponse, | ||
ClientServerOptions, | ||
} from './types' | ||
@@ -33,0 +34,0 @@ // https://github.com/supabase/postgrest-js/issues/551 |
@@ -11,2 +11,3 @@ // @ts-ignore | ||
IsValidResultOverride, | ||
ClientServerOptions, | ||
} from './types' | ||
@@ -16,4 +17,7 @@ import PostgrestError from './PostgrestError' | ||
export default abstract class PostgrestBuilder<Result, ThrowOnError extends boolean = false> | ||
implements | ||
export default abstract class PostgrestBuilder< | ||
ClientOptions extends ClientServerOptions, | ||
Result, | ||
ThrowOnError extends boolean = false | ||
> implements | ||
PromiseLike< | ||
@@ -33,3 +37,3 @@ ThrowOnError extends true ? PostgrestResponseSuccess<Result> : PostgrestSingleResponse<Result> | ||
constructor(builder: PostgrestBuilder<Result>) { | ||
constructor(builder: PostgrestBuilder<ClientOptions, Result>) { | ||
this.method = builder.method | ||
@@ -59,5 +63,5 @@ this.url = builder.url | ||
*/ | ||
throwOnError(): this & PostgrestBuilder<Result, true> { | ||
throwOnError(): this & PostgrestBuilder<ClientOptions, Result, true> { | ||
this.shouldThrowOnError = true | ||
return this as this & PostgrestBuilder<Result, true> | ||
return this as this & PostgrestBuilder<ClientOptions, Result, true> | ||
} | ||
@@ -231,5 +235,10 @@ | ||
*/ | ||
returns<NewResult>(): PostgrestBuilder<CheckMatchingArrayTypes<Result, NewResult>, ThrowOnError> { | ||
returns<NewResult>(): PostgrestBuilder< | ||
ClientOptions, | ||
CheckMatchingArrayTypes<Result, NewResult>, | ||
ThrowOnError | ||
> { | ||
/* istanbul ignore next */ | ||
return this as unknown as PostgrestBuilder< | ||
ClientOptions, | ||
CheckMatchingArrayTypes<Result, NewResult>, | ||
@@ -266,2 +275,3 @@ ThrowOnError | ||
>(): PostgrestBuilder< | ||
ClientOptions, | ||
IsValidResultOverride<Result, NewResult, false, false> extends true | ||
@@ -276,2 +286,3 @@ ? // Preserve the optionality of the result if the overriden type is an object (case of chaining with `maybeSingle`) | ||
return this as unknown as PostgrestBuilder< | ||
ClientOptions, | ||
IsValidResultOverride<Result, NewResult, false, false> extends true | ||
@@ -278,0 +289,0 @@ ? // Preserve the optionality of the result if the overriden type is an object (case of chaining with `maybeSingle`) |
@@ -5,3 +5,3 @@ import PostgrestQueryBuilder from './PostgrestQueryBuilder' | ||
import { DEFAULT_HEADERS } from './constants' | ||
import { Fetch, GenericSchema } from './types' | ||
import { Fetch, GenericSchema, ClientServerOptions, GetGenericDatabaseWithOptions } from './types' | ||
@@ -20,7 +20,12 @@ /** | ||
Database = any, | ||
SchemaName extends string & keyof Database = 'public' extends keyof Database | ||
ClientOptions extends ClientServerOptions = GetGenericDatabaseWithOptions< | ||
Database, | ||
{ PostgrestVersion: '12' } | ||
>['options'], | ||
SchemaName extends string & | ||
keyof GetGenericDatabaseWithOptions<Database>['db'] = 'public' extends keyof GetGenericDatabaseWithOptions<Database>['db'] | ||
? 'public' | ||
: string & keyof Database, | ||
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema | ||
? Database[SchemaName] | ||
: string & keyof GetGenericDatabaseWithOptions<Database>['db'], | ||
Schema extends GenericSchema = GetGenericDatabaseWithOptions<Database>['db'][SchemaName] extends GenericSchema | ||
? GetGenericDatabaseWithOptions<Database>['db'][SchemaName] | ||
: any | ||
@@ -64,6 +69,6 @@ > { | ||
Table extends Schema['Tables'][TableName] | ||
>(relation: TableName): PostgrestQueryBuilder<Schema, Table, TableName> | ||
>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName> | ||
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>( | ||
relation: ViewName | ||
): PostgrestQueryBuilder<Schema, View, ViewName> | ||
): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName> | ||
/** | ||
@@ -74,3 +79,3 @@ * Perform a query on a table or a view. | ||
*/ | ||
from(relation: string): PostgrestQueryBuilder<Schema, any, any> { | ||
from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any, any> { | ||
const url = new URL(`${this.url}/${relation}`) | ||
@@ -91,6 +96,7 @@ return new PostgrestQueryBuilder(url, { | ||
*/ | ||
schema<DynamicSchema extends string & keyof Database>( | ||
schema<DynamicSchema extends string & keyof GetGenericDatabaseWithOptions<Database>['db']>( | ||
schema: DynamicSchema | ||
): PostgrestClient< | ||
Database, | ||
ClientOptions, | ||
DynamicSchema, | ||
@@ -142,2 +148,3 @@ Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any | ||
): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
@@ -185,4 +192,4 @@ Fn['Returns'] extends any[] | ||
allowEmpty: false, | ||
} as unknown as PostgrestBuilder<Fn['Returns']>) | ||
} as unknown as PostgrestBuilder<ClientOptions, Fn['Returns']>) | ||
} | ||
} |
import PostgrestTransformBuilder from './PostgrestTransformBuilder' | ||
import { JsonPathToAccessor, JsonPathToType } from './select-query-parser/utils' | ||
import { GenericSchema } from './types' | ||
import { ClientServerOptions, GenericSchema } from './types' | ||
@@ -72,3 +72,6 @@ type FilterOperator = | ||
export type InvalidMethodError<S extends string> = { Error: S } | ||
export default class PostgrestFilterBuilder< | ||
ClientOptions extends ClientServerOptions, | ||
Schema extends GenericSchema, | ||
@@ -79,3 +82,10 @@ Row extends Record<string, unknown>, | ||
Relationships = unknown | ||
> extends PostgrestTransformBuilder<Schema, Row, Result, RelationName, Relationships> { | ||
> extends PostgrestTransformBuilder< | ||
ClientOptions, | ||
Schema, | ||
Row, | ||
Result, | ||
RelationName, | ||
Relationships | ||
> { | ||
/** | ||
@@ -82,0 +92,0 @@ * Match only rows where `column` is equal to `value`. |
import PostgrestBuilder from './PostgrestBuilder' | ||
import PostgrestFilterBuilder from './PostgrestFilterBuilder' | ||
import { GetResult } from './select-query-parser/result' | ||
import { Fetch, GenericSchema, GenericTable, GenericView } from './types' | ||
import { ClientServerOptions, Fetch, GenericSchema, GenericTable, GenericView } from './types' | ||
export default class PostgrestQueryBuilder< | ||
ClientOptions extends ClientServerOptions, | ||
Schema extends GenericSchema, | ||
@@ -59,3 +60,10 @@ Relation extends GenericTable | GenericView, | ||
Query extends string = '*', | ||
ResultOne = GetResult<Schema, Relation['Row'], RelationName, Relationships, Query> | ||
ResultOne = GetResult< | ||
Schema, | ||
Relation['Row'], | ||
RelationName, | ||
Relationships, | ||
Query, | ||
ClientOptions | ||
> | ||
>( | ||
@@ -70,3 +78,10 @@ columns?: Query, | ||
} = {} | ||
): PostgrestFilterBuilder<Schema, Relation['Row'], ResultOne[], RelationName, Relationships> { | ||
): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
Relation['Row'], | ||
ResultOne[], | ||
RelationName, | ||
Relationships | ||
> { | ||
const method = head ? 'HEAD' : 'GET' | ||
@@ -99,3 +114,3 @@ // Remove whitespaces except when quoted | ||
allowEmpty: false, | ||
} as unknown as PostgrestBuilder<ResultOne[]>) | ||
} as unknown as PostgrestBuilder<ClientOptions, ResultOne[]>) | ||
} | ||
@@ -109,3 +124,10 @@ | ||
} | ||
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> | ||
): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
Relation['Row'], | ||
null, | ||
RelationName, | ||
Relationships | ||
> | ||
insert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>( | ||
@@ -117,3 +139,10 @@ values: Row[], | ||
} | ||
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> | ||
): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
Relation['Row'], | ||
null, | ||
RelationName, | ||
Relationships | ||
> | ||
/** | ||
@@ -154,3 +183,10 @@ * Perform an INSERT into the table or view. | ||
} = {} | ||
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> { | ||
): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
Relation['Row'], | ||
null, | ||
RelationName, | ||
Relationships | ||
> { | ||
const method = 'POST' | ||
@@ -186,3 +222,3 @@ | ||
allowEmpty: false, | ||
} as unknown as PostgrestBuilder<null>) | ||
} as unknown as PostgrestBuilder<ClientOptions, null>) | ||
} | ||
@@ -198,3 +234,10 @@ | ||
} | ||
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> | ||
): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
Relation['Row'], | ||
null, | ||
RelationName, | ||
Relationships | ||
> | ||
upsert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>( | ||
@@ -208,3 +251,10 @@ values: Row[], | ||
} | ||
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> | ||
): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
Relation['Row'], | ||
null, | ||
RelationName, | ||
Relationships | ||
> | ||
/** | ||
@@ -261,3 +311,10 @@ * Perform an UPSERT on the table or view. Depending on the column(s) passed | ||
} = {} | ||
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> { | ||
): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
Relation['Row'], | ||
null, | ||
RelationName, | ||
Relationships | ||
> { | ||
const method = 'POST' | ||
@@ -295,3 +352,3 @@ | ||
allowEmpty: false, | ||
} as unknown as PostgrestBuilder<null>) | ||
} as unknown as PostgrestBuilder<ClientOptions, null>) | ||
} | ||
@@ -327,3 +384,10 @@ | ||
} = {} | ||
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> { | ||
): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
Relation['Row'], | ||
null, | ||
RelationName, | ||
Relationships | ||
> { | ||
const method = 'PATCH' | ||
@@ -347,3 +411,3 @@ const prefersHeaders = [] | ||
allowEmpty: false, | ||
} as unknown as PostgrestBuilder<null>) | ||
} as unknown as PostgrestBuilder<ClientOptions, null>) | ||
} | ||
@@ -374,3 +438,10 @@ | ||
count?: 'exact' | 'planned' | 'estimated' | ||
} = {}): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> { | ||
} = {}): PostgrestFilterBuilder< | ||
ClientOptions, | ||
Schema, | ||
Relation['Row'], | ||
null, | ||
RelationName, | ||
Relationships | ||
> { | ||
const method = 'DELETE' | ||
@@ -393,4 +464,4 @@ const prefersHeaders = [] | ||
allowEmpty: false, | ||
} as unknown as PostgrestBuilder<null>) | ||
} as unknown as PostgrestBuilder<ClientOptions, null>) | ||
} | ||
} |
import PostgrestBuilder from './PostgrestBuilder' | ||
import { GetResult } from './select-query-parser/result' | ||
import { GenericSchema, CheckMatchingArrayTypes } from './types' | ||
import { GenericSchema, CheckMatchingArrayTypes, ClientServerOptions } from './types' | ||
export default class PostgrestTransformBuilder< | ||
ClientOptions extends ClientServerOptions, | ||
Schema extends GenericSchema, | ||
@@ -10,4 +11,5 @@ Row extends Record<string, unknown>, | ||
RelationName = unknown, | ||
Relationships = unknown | ||
> extends PostgrestBuilder<Result> { | ||
Relationships = unknown, | ||
Method = unknown | ||
> extends PostgrestBuilder<ClientOptions, Result> { | ||
/** | ||
@@ -24,6 +26,14 @@ * Perform a SELECT on the query result. | ||
Query extends string = '*', | ||
NewResultOne = GetResult<Schema, Row, RelationName, Relationships, Query> | ||
NewResultOne = GetResult<Schema, Row, RelationName, Relationships, Query, ClientOptions> | ||
>( | ||
columns?: Query | ||
): PostgrestTransformBuilder<Schema, Row, NewResultOne[], RelationName, Relationships> { | ||
): PostgrestTransformBuilder< | ||
ClientOptions, | ||
Schema, | ||
Row, | ||
NewResultOne[], | ||
RelationName, | ||
Relationships, | ||
Method | ||
> { | ||
// Remove whitespaces except when quoted | ||
@@ -49,2 +59,3 @@ let quoted = false | ||
return this as unknown as PostgrestTransformBuilder< | ||
ClientOptions, | ||
Schema, | ||
@@ -54,3 +65,4 @@ Row, | ||
RelationName, | ||
Relationships | ||
Relationships, | ||
Method | ||
> | ||
@@ -195,7 +207,8 @@ } | ||
*/ | ||
single< | ||
ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never | ||
>(): PostgrestBuilder<ResultOne> { | ||
single<ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never>(): PostgrestBuilder< | ||
ClientOptions, | ||
ResultOne | ||
> { | ||
this.headers['Accept'] = 'application/vnd.pgrst.object+json' | ||
return this as unknown as PostgrestBuilder<ResultOne> | ||
return this as unknown as PostgrestBuilder<ClientOptions, ResultOne> | ||
} | ||
@@ -211,3 +224,3 @@ | ||
ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never | ||
>(): PostgrestBuilder<ResultOne | null> { | ||
>(): PostgrestBuilder<ClientOptions, ResultOne | null> { | ||
// Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361 | ||
@@ -221,3 +234,3 @@ // Issue persists e.g. for `.insert([...]).select().maybeSingle()` | ||
this.isMaybeSingle = true | ||
return this as unknown as PostgrestBuilder<ResultOne | null> | ||
return this as unknown as PostgrestBuilder<ClientOptions, ResultOne | null> | ||
} | ||
@@ -228,5 +241,5 @@ | ||
*/ | ||
csv(): PostgrestBuilder<string> { | ||
csv(): PostgrestBuilder<ClientOptions, string> { | ||
this.headers['Accept'] = 'text/csv' | ||
return this as unknown as PostgrestBuilder<string> | ||
return this as unknown as PostgrestBuilder<ClientOptions, string> | ||
} | ||
@@ -237,5 +250,5 @@ | ||
*/ | ||
geojson(): PostgrestBuilder<Record<string, unknown>> { | ||
geojson(): PostgrestBuilder<ClientOptions, Record<string, unknown>> { | ||
this.headers['Accept'] = 'application/geo+json' | ||
return this as unknown as PostgrestBuilder<Record<string, unknown>> | ||
return this as unknown as PostgrestBuilder<ClientOptions, Record<string, unknown>> | ||
} | ||
@@ -282,3 +295,5 @@ | ||
format?: 'json' | 'text' | ||
} = {}): PostgrestBuilder<Record<string, unknown>[]> | PostgrestBuilder<string> { | ||
} = {}): | ||
| PostgrestBuilder<ClientOptions, Record<string, unknown>[]> | ||
| PostgrestBuilder<ClientOptions, string> { | ||
const options = [ | ||
@@ -298,4 +313,5 @@ analyze ? 'analyze' : null, | ||
] = `application/vnd.pgrst.plan+${format}; for="${forMediatype}"; options=${options};` | ||
if (format === 'json') return this as unknown as PostgrestBuilder<Record<string, unknown>[]> | ||
else return this as unknown as PostgrestBuilder<string> | ||
if (format === 'json') | ||
return this as unknown as PostgrestBuilder<ClientOptions, Record<string, unknown>[]> | ||
else return this as unknown as PostgrestBuilder<ClientOptions, string> | ||
} | ||
@@ -324,2 +340,3 @@ | ||
returns<NewResult>(): PostgrestTransformBuilder< | ||
ClientOptions, | ||
Schema, | ||
@@ -329,5 +346,7 @@ Row, | ||
RelationName, | ||
Relationships | ||
Relationships, | ||
Method | ||
> { | ||
return this as unknown as PostgrestTransformBuilder< | ||
ClientOptions, | ||
Schema, | ||
@@ -337,5 +356,6 @@ Row, | ||
RelationName, | ||
Relationships | ||
Relationships, | ||
Method | ||
> | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { GenericTable } from '../types' | ||
import { ClientServerOptions, GenericTable } from '../types' | ||
import { ContainsNull, GenericRelationship, PostgreSQLTypes } from './types' | ||
@@ -24,2 +24,5 @@ import { Ast, ParseQuery } from './parser' | ||
export type SpreadOnManyEnabled<PostgrestVersion extends string | undefined> = | ||
PostgrestVersion extends `13${string}` ? true : false | ||
/** | ||
@@ -39,3 +42,4 @@ * Main entry point for constructing the result type of a PostgREST query. | ||
Relationships, | ||
Query extends string | ||
Query extends string, | ||
ClientOptions extends ClientServerOptions | ||
> = IsAny<Schema> extends true | ||
@@ -59,3 +63,3 @@ ? ParseQuery<Query> extends infer ParsedQuery | ||
? Relationships extends GenericRelationship[] | ||
? ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery> | ||
? ProcessNodes<ClientOptions, Schema, Row, RelationName, Relationships, ParsedQuery> | ||
: SelectQueryError<'Invalid Relationships cannot infer result type'> | ||
@@ -179,2 +183,3 @@ : SelectQueryError<'Invalid RelationName cannot infer result type'> | ||
export type ProcessNodes< | ||
ClientOptions extends ClientServerOptions, | ||
Schema extends GenericSchema, | ||
@@ -190,5 +195,20 @@ Row extends Record<string, unknown>, | ||
? RestNodes extends Ast.Node[] | ||
? ProcessNode<Schema, Row, RelationName, Relationships, FirstNode> extends infer FieldResult | ||
? ProcessNode< | ||
ClientOptions, | ||
Schema, | ||
Row, | ||
RelationName, | ||
Relationships, | ||
FirstNode | ||
> extends infer FieldResult | ||
? FieldResult extends Record<string, unknown> | ||
? ProcessNodes<Schema, Row, RelationName, Relationships, RestNodes, Acc & FieldResult> | ||
? ProcessNodes< | ||
ClientOptions, | ||
Schema, | ||
Row, | ||
RelationName, | ||
Relationships, | ||
RestNodes, | ||
Acc & FieldResult | ||
> | ||
: FieldResult extends SelectQueryError<infer E> | ||
@@ -213,2 +233,3 @@ ? SelectQueryError<E> | ||
export type ProcessNode< | ||
ClientOptions extends ClientServerOptions, | ||
Schema extends GenericSchema, | ||
@@ -224,5 +245,19 @@ Row extends Record<string, unknown>, | ||
: NodeType['type'] extends Ast.SpreadNode['type'] // If the selection is a ...spread | ||
? ProcessSpreadNode<Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.SpreadNode>> | ||
? ProcessSpreadNode< | ||
ClientOptions, | ||
Schema, | ||
Row, | ||
RelationName, | ||
Relationships, | ||
Extract<NodeType, Ast.SpreadNode> | ||
> | ||
: NodeType['type'] extends Ast.FieldNode['type'] | ||
? ProcessFieldNode<Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.FieldNode>> | ||
? ProcessFieldNode< | ||
ClientOptions, | ||
Schema, | ||
Row, | ||
RelationName, | ||
Relationships, | ||
Extract<NodeType, Ast.FieldNode> | ||
> | ||
: SelectQueryError<'Unsupported node type.'> | ||
@@ -240,2 +275,3 @@ | ||
type ProcessFieldNode< | ||
ClientOptions extends ClientServerOptions, | ||
Schema extends GenericSchema, | ||
@@ -249,3 +285,3 @@ Row extends Record<string, unknown>, | ||
: IsNonEmptyArray<Field['children']> extends true // Has embedded resource? | ||
? ProcessEmbeddedResource<Schema, Relationships, Field, RelationName> | ||
? ProcessEmbeddedResource<ClientOptions, Schema, Relationships, Field, RelationName> | ||
: ProcessSimpleField<Row, RelationName, Field> | ||
@@ -315,2 +351,3 @@ | ||
export type ProcessEmbeddedResource< | ||
ClientOptions extends ClientServerOptions, | ||
Schema extends GenericSchema, | ||
@@ -326,3 +363,3 @@ Relationships extends GenericRelationship[], | ||
} | ||
? ProcessEmbeddedResourceResult<Schema, Resolved, Field, CurrentTableOrView> | ||
? ProcessEmbeddedResourceResult<ClientOptions, Schema, Resolved, Field, CurrentTableOrView> | ||
: // Otherwise the Resolved is a SelectQueryError return it | ||
@@ -339,2 +376,3 @@ { [K in GetFieldNodeResultName<Field>]: Resolved } | ||
type ProcessEmbeddedResourceResult< | ||
ClientOptions extends ClientServerOptions, | ||
Schema extends GenericSchema, | ||
@@ -349,2 +387,3 @@ Resolved extends { | ||
> = ProcessNodes< | ||
ClientOptions, | ||
Schema, | ||
@@ -408,2 +447,3 @@ Resolved['referencedTable']['Row'], | ||
type ProcessSpreadNode< | ||
ClientOptions extends ClientServerOptions, | ||
Schema extends GenericSchema, | ||
@@ -414,9 +454,18 @@ Row extends Record<string, unknown>, | ||
Spread extends Ast.SpreadNode | ||
> = ProcessNode<Schema, Row, RelationName, Relationships, Spread['target']> extends infer Result | ||
> = ProcessNode< | ||
ClientOptions, | ||
Schema, | ||
Row, | ||
RelationName, | ||
Relationships, | ||
Spread['target'] | ||
> extends infer Result | ||
? Result extends SelectQueryError<infer E> | ||
? SelectQueryError<E> | ||
: ExtractFirstProperty<Result> 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`> | ||
} | ||
? SpreadOnManyEnabled<ClientOptions['PostgrestVersion']> extends true // Spread over an many-to-many relationship, turn all the result fields into correlated arrays | ||
? ProcessManyToManySpreadNodeResult<Result> | ||
: { | ||
[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<Result> | ||
@@ -426,2 +475,17 @@ : never | ||
/** | ||
* Helper type to process the result of a many-to-many spread node. | ||
* Converts all fields in the spread object into arrays. | ||
*/ | ||
type ProcessManyToManySpreadNodeResult<Result> = Result extends Record< | ||
string, | ||
SelectQueryError<string> | null | ||
> | ||
? Result | ||
: ExtractFirstProperty<Result> extends infer SpreadedObject | ||
? SpreadedObject extends Array<Record<string, unknown>> | ||
? { [K in keyof SpreadedObject[number]]: Array<SpreadedObject[number][K]> } | ||
: SelectQueryError<'An error occurred spreading the many-to-many object'> | ||
: SelectQueryError<'An error occurred spreading the many-to-many object'> | ||
/** | ||
* Helper type to process the result of a spread node. | ||
@@ -428,0 +492,0 @@ */ |
import PostgrestError from './PostgrestError' | ||
import { ContainsNull } from './select-query-parser/types' | ||
import { SelectQueryError } from './select-query-parser/utils' | ||
import { IsAny, SelectQueryError } from './select-query-parser/utils' | ||
@@ -74,4 +74,30 @@ export type Fetch = typeof fetch | ||
export type ClientServerOptions = { | ||
PostgrestVersion?: string | ||
} | ||
export type DatabaseWithOptions<Database, Options extends ClientServerOptions> = { | ||
db: Database | ||
options: Options | ||
} | ||
const INTERNAL_SUPABASE_OPTIONS = '__InternalSupabase' | ||
export type GetGenericDatabaseWithOptions< | ||
Database, | ||
Opts extends ClientServerOptions = { PostgrestVersion: '12' } | ||
> = IsAny<Database> extends true | ||
? DatabaseWithOptions<Database, Opts> | ||
: typeof INTERNAL_SUPABASE_OPTIONS extends keyof Database | ||
? Database[typeof INTERNAL_SUPABASE_OPTIONS] extends ClientServerOptions | ||
? DatabaseWithOptions< | ||
Omit<Database, typeof INTERNAL_SUPABASE_OPTIONS>, | ||
Database[typeof INTERNAL_SUPABASE_OPTIONS] | ||
> | ||
: DatabaseWithOptions<Omit<Database, typeof INTERNAL_SUPABASE_OPTIONS>, Opts> | ||
: DatabaseWithOptions<Database, Opts> | ||
// https://twitter.com/mattpocockuk/status/1622730173446557697 | ||
export type Prettify<T> = { [K in keyof T]: T[K] } & {} | ||
// https://github.com/sindresorhus/type-fest | ||
@@ -78,0 +104,0 @@ export type SimplifyDeep<Type, ExcludeType = never> = ConditionalSimplifyDeep< |
@@ -1,1 +0,1 @@ | ||
export const version = '1.19.4' | ||
export const version = '1.20.0' |
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
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
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
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
365003
3.08%6299
3.82%2
100%