@supabase/postgrest-js 1.19.4 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/cjs/PostgrestBuilder.d.ts +17 -7
  2. package/dist/cjs/PostgrestBuilder.d.ts.map +1 -1
  3. package/dist/cjs/PostgrestBuilder.js +17 -16
  4. package/dist/cjs/PostgrestBuilder.js.map +1 -1
  5. package/dist/cjs/PostgrestClient.d.ts +10 -8
  6. package/dist/cjs/PostgrestClient.d.ts.map +1 -1
  7. package/dist/cjs/PostgrestClient.js +6 -7
  8. package/dist/cjs/PostgrestClient.js.map +1 -1
  9. package/dist/cjs/PostgrestFilterBuilder.d.ts +5 -2
  10. package/dist/cjs/PostgrestFilterBuilder.d.ts.map +1 -1
  11. package/dist/cjs/PostgrestFilterBuilder.js.map +1 -1
  12. package/dist/cjs/PostgrestQueryBuilder.d.ts +12 -12
  13. package/dist/cjs/PostgrestQueryBuilder.d.ts.map +1 -1
  14. package/dist/cjs/PostgrestQueryBuilder.js +17 -37
  15. package/dist/cjs/PostgrestQueryBuilder.js.map +1 -1
  16. package/dist/cjs/PostgrestTransformBuilder.d.ts +17 -9
  17. package/dist/cjs/PostgrestTransformBuilder.d.ts.map +1 -1
  18. package/dist/cjs/PostgrestTransformBuilder.js +24 -20
  19. package/dist/cjs/PostgrestTransformBuilder.js.map +1 -1
  20. package/dist/cjs/index.d.ts +1 -1
  21. package/dist/cjs/index.d.ts.map +1 -1
  22. package/dist/cjs/select-query-parser/result.d.ts +18 -10
  23. package/dist/cjs/select-query-parser/result.d.ts.map +1 -1
  24. package/dist/cjs/types.d.ts +13 -1
  25. package/dist/cjs/types.d.ts.map +1 -1
  26. package/dist/cjs/types.js +1 -0
  27. package/dist/cjs/types.js.map +1 -1
  28. package/package.json +5 -3
  29. package/src/PostgrestBuilder.ts +40 -19
  30. package/src/PostgrestClient.ts +26 -22
  31. package/src/PostgrestFilterBuilder.ts +15 -3
  32. package/src/PostgrestQueryBuilder.ts +112 -56
  33. package/src/PostgrestTransformBuilder.ts +80 -39
  34. package/src/index.ts +2 -0
  35. package/src/select-query-parser/result.ts +77 -13
  36. package/src/types.ts +30 -1
  37. package/src/version.ts +1 -1
@@ -1,4 +1,4 @@
1
- import { GenericTable } from '../types'
1
+ import { ClientServerOptions, GenericTable } from '../types'
2
2
  import { ContainsNull, GenericRelationship, PostgreSQLTypes } from './types'
3
3
  import { Ast, ParseQuery } from './parser'
4
4
  import {
@@ -21,6 +21,9 @@ import {
21
21
  SelectQueryError,
22
22
  } from './utils'
23
23
 
24
+ export type SpreadOnManyEnabled<PostgrestVersion extends string | undefined> =
25
+ PostgrestVersion extends `13${string}` ? true : false
26
+
24
27
  /**
25
28
  * Main entry point for constructing the result type of a PostgREST query.
26
29
  *
@@ -35,7 +38,8 @@ export type GetResult<
35
38
  Row extends Record<string, unknown>,
36
39
  RelationName,
37
40
  Relationships,
38
- Query extends string
41
+ Query extends string,
42
+ ClientOptions extends ClientServerOptions
39
43
  > = IsAny<Schema> extends true
40
44
  ? ParseQuery<Query> extends infer ParsedQuery
41
45
  ? ParsedQuery extends Ast.Node[]
@@ -54,7 +58,7 @@ export type GetResult<
54
58
  ? ParsedQuery extends Ast.Node[]
55
59
  ? RelationName extends string
56
60
  ? Relationships extends GenericRelationship[]
57
- ? ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery>
61
+ ? ProcessNodes<ClientOptions, Schema, Row, RelationName, Relationships, ParsedQuery>
58
62
  : SelectQueryError<'Invalid Relationships cannot infer result type'>
59
63
  : SelectQueryError<'Invalid RelationName cannot infer result type'>
60
64
  : ParsedQuery
@@ -173,6 +177,7 @@ export type RPCCallNodes<
173
177
  * @param Acc - Accumulator for the constructed type.
174
178
  */
175
179
  export type ProcessNodes<
180
+ ClientOptions extends ClientServerOptions,
176
181
  Schema extends GenericSchema,
177
182
  Row extends Record<string, unknown>,
178
183
  RelationName extends string,
@@ -183,9 +188,24 @@ export type ProcessNodes<
183
188
  ? Nodes extends [infer FirstNode, ...infer RestNodes]
184
189
  ? FirstNode extends Ast.Node
185
190
  ? RestNodes extends Ast.Node[]
186
- ? ProcessNode<Schema, Row, RelationName, Relationships, FirstNode> extends infer FieldResult
191
+ ? ProcessNode<
192
+ ClientOptions,
193
+ Schema,
194
+ Row,
195
+ RelationName,
196
+ Relationships,
197
+ FirstNode
198
+ > extends infer FieldResult
187
199
  ? FieldResult extends Record<string, unknown>
188
- ? ProcessNodes<Schema, Row, RelationName, Relationships, RestNodes, Acc & FieldResult>
200
+ ? ProcessNodes<
201
+ ClientOptions,
202
+ Schema,
203
+ Row,
204
+ RelationName,
205
+ Relationships,
206
+ RestNodes,
207
+ Acc & FieldResult
208
+ >
189
209
  : FieldResult extends SelectQueryError<infer E>
190
210
  ? SelectQueryError<E>
191
211
  : SelectQueryError<'Could not retrieve a valid record or error value'>
@@ -205,6 +225,7 @@ export type ProcessNodes<
205
225
  * @param NodeType - The Node to process.
206
226
  */
207
227
  export type ProcessNode<
228
+ ClientOptions extends ClientServerOptions,
208
229
  Schema extends GenericSchema,
209
230
  Row extends Record<string, unknown>,
210
231
  RelationName extends string,
@@ -215,9 +236,23 @@ export type ProcessNode<
215
236
  NodeType['type'] extends Ast.StarNode['type'] // If the selection is *
216
237
  ? Row
217
238
  : NodeType['type'] extends Ast.SpreadNode['type'] // If the selection is a ...spread
218
- ? ProcessSpreadNode<Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.SpreadNode>>
239
+ ? ProcessSpreadNode<
240
+ ClientOptions,
241
+ Schema,
242
+ Row,
243
+ RelationName,
244
+ Relationships,
245
+ Extract<NodeType, Ast.SpreadNode>
246
+ >
219
247
  : NodeType['type'] extends Ast.FieldNode['type']
220
- ? ProcessFieldNode<Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.FieldNode>>
248
+ ? ProcessFieldNode<
249
+ ClientOptions,
250
+ Schema,
251
+ Row,
252
+ RelationName,
253
+ Relationships,
254
+ Extract<NodeType, Ast.FieldNode>
255
+ >
221
256
  : SelectQueryError<'Unsupported node type.'>
222
257
 
223
258
  /**
@@ -230,6 +265,7 @@ export type ProcessNode<
230
265
  * @param Field - The FieldNode to process.
231
266
  */
232
267
  type ProcessFieldNode<
268
+ ClientOptions extends ClientServerOptions,
233
269
  Schema extends GenericSchema,
234
270
  Row extends Record<string, unknown>,
235
271
  RelationName extends string,
@@ -238,7 +274,7 @@ type ProcessFieldNode<
238
274
  > = Field['children'] extends []
239
275
  ? {}
240
276
  : IsNonEmptyArray<Field['children']> extends true // Has embedded resource?
241
- ? ProcessEmbeddedResource<Schema, Relationships, Field, RelationName>
277
+ ? ProcessEmbeddedResource<ClientOptions, Schema, Relationships, Field, RelationName>
242
278
  : ProcessSimpleField<Row, RelationName, Field>
243
279
 
244
280
  type ResolveJsonPathType<
@@ -303,6 +339,7 @@ type ProcessSimpleField<
303
339
  * @param Field - The FieldNode to process.
304
340
  */
305
341
  export type ProcessEmbeddedResource<
342
+ ClientOptions extends ClientServerOptions,
306
343
  Schema extends GenericSchema,
307
344
  Relationships extends GenericRelationship[],
308
345
  Field extends Ast.FieldNode,
@@ -313,7 +350,7 @@ export type ProcessEmbeddedResource<
313
350
  relation: GenericRelationship & { match: 'refrel' | 'col' | 'fkname' }
314
351
  direction: string
315
352
  }
316
- ? ProcessEmbeddedResourceResult<Schema, Resolved, Field, CurrentTableOrView>
353
+ ? ProcessEmbeddedResourceResult<ClientOptions, Schema, Resolved, Field, CurrentTableOrView>
317
354
  : // Otherwise the Resolved is a SelectQueryError return it
318
355
  { [K in GetFieldNodeResultName<Field>]: Resolved }
319
356
  : {
@@ -325,6 +362,7 @@ export type ProcessEmbeddedResource<
325
362
  * Helper type to process the result of an embedded resource.
326
363
  */
327
364
  type ProcessEmbeddedResourceResult<
365
+ ClientOptions extends ClientServerOptions,
328
366
  Schema extends GenericSchema,
329
367
  Resolved extends {
330
368
  referencedTable: Pick<GenericTable, 'Row' | 'Relationships'>
@@ -334,6 +372,7 @@ type ProcessEmbeddedResourceResult<
334
372
  Field extends Ast.FieldNode,
335
373
  CurrentTableOrView extends keyof TablesAndViews<Schema>
336
374
  > = ProcessNodes<
375
+ ClientOptions,
337
376
  Schema,
338
377
  Resolved['referencedTable']['Row'],
339
378
  Field['name'],
@@ -392,21 +431,46 @@ type ProcessEmbeddedResourceResult<
392
431
  * @param Spread - The SpreadNode to process.
393
432
  */
394
433
  type ProcessSpreadNode<
434
+ ClientOptions extends ClientServerOptions,
395
435
  Schema extends GenericSchema,
396
436
  Row extends Record<string, unknown>,
397
437
  RelationName extends string,
398
438
  Relationships extends GenericRelationship[],
399
439
  Spread extends Ast.SpreadNode
400
- > = ProcessNode<Schema, Row, RelationName, Relationships, Spread['target']> extends infer Result
440
+ > = ProcessNode<
441
+ ClientOptions,
442
+ Schema,
443
+ Row,
444
+ RelationName,
445
+ Relationships,
446
+ Spread['target']
447
+ > extends infer Result
401
448
  ? Result extends SelectQueryError<infer E>
402
449
  ? SelectQueryError<E>
403
450
  : ExtractFirstProperty<Result> extends unknown[]
404
- ? {
405
- [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`>
406
- }
451
+ ? SpreadOnManyEnabled<ClientOptions['PostgrestVersion']> extends true // Spread over an many-to-many relationship, turn all the result fields into correlated arrays
452
+ ? ProcessManyToManySpreadNodeResult<Result>
453
+ : {
454
+ [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`>
455
+ }
407
456
  : ProcessSpreadNodeResult<Result>
408
457
  : never
409
458
 
459
+ /**
460
+ * Helper type to process the result of a many-to-many spread node.
461
+ * Converts all fields in the spread object into arrays.
462
+ */
463
+ type ProcessManyToManySpreadNodeResult<Result> = Result extends Record<
464
+ string,
465
+ SelectQueryError<string> | null
466
+ >
467
+ ? Result
468
+ : ExtractFirstProperty<Result> extends infer SpreadedObject
469
+ ? SpreadedObject extends Array<Record<string, unknown>>
470
+ ? { [K in keyof SpreadedObject[number]]: Array<SpreadedObject[number][K]> }
471
+ : SelectQueryError<'An error occurred spreading the many-to-many object'>
472
+ : SelectQueryError<'An error occurred spreading the many-to-many object'>
473
+
410
474
  /**
411
475
  * Helper type to process the result of a spread node.
412
476
  */
package/src/types.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import PostgrestError from './PostgrestError'
2
2
  import { ContainsNull } from './select-query-parser/types'
3
- import { SelectQueryError } from './select-query-parser/utils'
3
+ import { IsAny, SelectQueryError } from './select-query-parser/utils'
4
4
 
5
5
  export type Fetch = typeof fetch
6
6
 
@@ -71,8 +71,37 @@ export type GenericSchema = {
71
71
  Functions: Record<string, GenericFunction>
72
72
  }
73
73
 
74
+ export type ClientServerOptions = {
75
+ PostgrestVersion?: string
76
+ }
77
+
78
+ export type DatabaseWithOptions<Database, Options extends ClientServerOptions> = {
79
+ db: Database
80
+ options: Options
81
+ }
82
+
83
+ const INTERNAL_SUPABASE_OPTIONS = '__InternalSupabase'
84
+
85
+ export type GetGenericDatabaseWithOptions<
86
+ Database,
87
+ Opts extends ClientServerOptions = { PostgrestVersion: '12' }
88
+ > = IsAny<Database> extends true
89
+ ? DatabaseWithOptions<Database, Opts>
90
+ : typeof INTERNAL_SUPABASE_OPTIONS extends keyof Database
91
+ ? Database[typeof INTERNAL_SUPABASE_OPTIONS] extends ClientServerOptions
92
+ ? DatabaseWithOptions<
93
+ Omit<Database, typeof INTERNAL_SUPABASE_OPTIONS>,
94
+ Database[typeof INTERNAL_SUPABASE_OPTIONS]
95
+ >
96
+ : DatabaseWithOptions<Omit<Database, typeof INTERNAL_SUPABASE_OPTIONS>, Opts>
97
+ : DatabaseWithOptions<Database, Opts>
98
+
99
+ export type MaxAffectedEnabled<PostgrestVersion extends string | undefined> =
100
+ PostgrestVersion extends `13${string}` ? true : false
101
+
74
102
  // https://twitter.com/mattpocockuk/status/1622730173446557697
75
103
  export type Prettify<T> = { [K in keyof T]: T[K] } & {}
104
+
76
105
  // https://github.com/sindresorhus/type-fest
77
106
  export type SimplifyDeep<Type, ExcludeType = never> = ConditionalSimplifyDeep<
78
107
  Type,
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.19.4'
1
+ export const version = '1.21.0'