@supabase/postgrest-js 1.17.1 → 1.17.3

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.
@@ -38,27 +38,24 @@ type ResolveRelationships<
38
38
  Relationships extends GenericRelationship[],
39
39
  Nodes extends Ast.FieldNode[]
40
40
  > = UnionToArray<{
41
- [K in keyof Nodes]: ResolveRelationship<
42
- Schema,
43
- Relationships,
44
- Nodes[K],
45
- RelationName
46
- > extends infer Relation
47
- ? Relation extends {
48
- relation: {
49
- referencedRelation: any
50
- foreignKeyName: any
51
- match: any
52
- }
53
- from: any
54
- }
55
- ? {
56
- referencedTable: Relation['relation']['referencedRelation']
57
- fkName: Relation['relation']['foreignKeyName']
58
- from: Relation['from']
59
- match: Relation['relation']['match']
60
- fieldName: GetFieldNodeResultName<Nodes[K]>
41
+ [K in keyof Nodes]: Nodes[K] extends Ast.FieldNode
42
+ ? ResolveRelationship<Schema, Relationships, Nodes[K], RelationName> extends infer Relation
43
+ ? Relation extends {
44
+ relation: {
45
+ referencedRelation: string
46
+ foreignKeyName: string
47
+ match: string
48
+ }
49
+ from: string
61
50
  }
51
+ ? {
52
+ referencedTable: Relation['relation']['referencedRelation']
53
+ fkName: Relation['relation']['foreignKeyName']
54
+ from: Relation['from']
55
+ match: Relation['relation']['match']
56
+ fieldName: GetFieldNodeResultName<Nodes[K]>
57
+ }
58
+ : Relation
62
59
  : never
63
60
  : never
64
61
  }>[0]
@@ -69,10 +66,12 @@ type ResolveRelationships<
69
66
  type IsDoubleReference<T, U> = T extends {
70
67
  referencedTable: infer RT
71
68
  fieldName: infer FN
72
- match: infer M extends 'col' | 'refrel'
69
+ match: infer M
73
70
  }
74
- ? U extends { referencedTable: RT; fieldName: FN; match: M }
75
- ? true
71
+ ? M extends 'col' | 'refrel'
72
+ ? U extends { referencedTable: RT; fieldName: FN; match: M }
73
+ ? true
74
+ : false
76
75
  : false
77
76
  : false
78
77
 
@@ -97,21 +96,25 @@ export type CheckDuplicateEmbededReference<
97
96
  RelationName extends string,
98
97
  Relationships extends GenericRelationship[],
99
98
  Nodes extends Ast.Node[]
100
- > = FilterRelationNodes<Nodes> extends infer RelationsNodes extends Ast.FieldNode[]
101
- ? ResolveRelationships<
102
- Schema,
103
- RelationName,
104
- Relationships,
105
- RelationsNodes
106
- > extends infer ResolvedRels
107
- ? ResolvedRels extends unknown[]
108
- ? FindDuplicates<ResolvedRels> extends infer Duplicates
109
- ? Duplicates extends never
110
- ? false
111
- : Duplicates extends { fieldName: infer FieldName extends string }
112
- ? {
113
- [K in FieldName]: SelectQueryError<`table "${RelationName}" specified more than once use hinting for desambiguation`>
114
- }
99
+ > = FilterRelationNodes<Nodes> extends infer RelationsNodes
100
+ ? RelationsNodes extends Ast.FieldNode[]
101
+ ? ResolveRelationships<
102
+ Schema,
103
+ RelationName,
104
+ Relationships,
105
+ RelationsNodes
106
+ > extends infer ResolvedRels
107
+ ? ResolvedRels extends unknown[]
108
+ ? FindDuplicates<ResolvedRels> extends infer Duplicates
109
+ ? Duplicates extends never
110
+ ? false
111
+ : Duplicates extends { fieldName: infer FieldName }
112
+ ? FieldName extends string
113
+ ? {
114
+ [K in FieldName]: SelectQueryError<`table "${RelationName}" specified more than once use hinting for desambiguation`>
115
+ }
116
+ : false
117
+ : false
115
118
  : false
116
119
  : false
117
120
  : false
@@ -155,33 +158,38 @@ type CheckRelationshipError<
155
158
  : // If the relation is a reverse relation with no hint (matching by name)
156
159
  FoundRelation extends {
157
160
  relation: {
158
- referencedRelation: infer RelatedRelationName extends string
161
+ referencedRelation: infer RelatedRelationName
159
162
  name: string
160
163
  }
161
164
  direction: 'reverse'
162
165
  }
163
- ? // We check if there is possible confusion with other relations with this table
164
- HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
165
- ? // If there is, postgrest will fail at runtime, and require desambiguation via hinting
166
- SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`>
167
- : FoundRelation
166
+ ? RelatedRelationName extends string
167
+ ? // We check if there is possible confusion with other relations with this table
168
+ HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
169
+ ? // If there is, postgrest will fail at runtime, and require desambiguation via hinting
170
+ SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`>
171
+ : FoundRelation
172
+ : never
168
173
  : // Same check for forward relationships, but we must gather the relationships from the found relation
169
174
  FoundRelation extends {
170
175
  relation: {
171
- referencedRelation: infer RelatedRelationName extends string
176
+ referencedRelation: infer RelatedRelationName
172
177
  name: string
173
178
  }
174
179
  direction: 'forward'
175
- from: infer From extends keyof TablesAndViews<Schema> & string
180
+ from: infer From
176
181
  }
177
- ? HasMultipleFKeysToFRel<
178
- RelatedRelationName,
179
- TablesAndViews<Schema>[From]['Relationships']
180
- > extends true
181
- ? SelectQueryError<`Could not embed because more than one relationship was found for '${From}' and '${RelatedRelationName}' you need to hint the column with ${From}!<columnName> ?`>
182
- : FoundRelation
182
+ ? RelatedRelationName extends string
183
+ ? From extends keyof TablesAndViews<Schema> & string
184
+ ? HasMultipleFKeysToFRel<
185
+ RelatedRelationName,
186
+ TablesAndViews<Schema>[From]['Relationships']
187
+ > extends true
188
+ ? SelectQueryError<`Could not embed because more than one relationship was found for '${From}' and '${RelatedRelationName}' you need to hint the column with ${From}!<columnName> ?`>
189
+ : FoundRelation
190
+ : never
191
+ : never
183
192
  : FoundRelation
184
-
185
193
  /**
186
194
  * Resolves relationships for embedded resources and retrieves the referenced Table
187
195
  */
@@ -217,26 +225,28 @@ type ResolveReverseRelationship<
217
225
  > = FindFieldMatchingRelationships<Schema, Relationships, Field> extends infer FoundRelation
218
226
  ? FoundRelation extends never
219
227
  ? false
220
- : FoundRelation extends { referencedRelation: infer RelatedRelationName extends string }
221
- ? RelatedRelationName extends keyof TablesAndViews<Schema>
222
- ? // If the relation was found via hinting we just return it without any more checks
223
- FoundRelation extends { hint: string }
224
- ? {
225
- referencedTable: TablesAndViews<Schema>[RelatedRelationName]
226
- relation: FoundRelation
227
- direction: 'reverse'
228
- from: CurrentTableOrView
229
- }
230
- : // If the relation was found via implicit relation naming, we must ensure there is no conflicting matches
231
- HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
232
- ? SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`>
233
- : {
234
- referencedTable: TablesAndViews<Schema>[RelatedRelationName]
235
- relation: FoundRelation
236
- direction: 'reverse'
237
- from: CurrentTableOrView
238
- }
239
- : SelectQueryError<`Relation '${RelatedRelationName}' not found in schema.`>
228
+ : FoundRelation extends { referencedRelation: infer RelatedRelationName }
229
+ ? RelatedRelationName extends string
230
+ ? RelatedRelationName extends keyof TablesAndViews<Schema>
231
+ ? // If the relation was found via hinting we just return it without any more checks
232
+ FoundRelation extends { hint: string }
233
+ ? {
234
+ referencedTable: TablesAndViews<Schema>[RelatedRelationName]
235
+ relation: FoundRelation
236
+ direction: 'reverse'
237
+ from: CurrentTableOrView
238
+ }
239
+ : // If the relation was found via implicit relation naming, we must ensure there is no conflicting matches
240
+ HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
241
+ ? SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`>
242
+ : {
243
+ referencedTable: TablesAndViews<Schema>[RelatedRelationName]
244
+ relation: FoundRelation
245
+ direction: 'reverse'
246
+ from: CurrentTableOrView
247
+ }
248
+ : SelectQueryError<`Relation '${RelatedRelationName}' not found in schema.`>
249
+ : false
240
250
  : false
241
251
  : false
242
252
 
@@ -244,17 +254,19 @@ export type FindMatchingTableRelationships<
244
254
  Schema extends GenericSchema,
245
255
  Relationships extends GenericRelationship[],
246
256
  value extends string
247
- > = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]]
248
- ? R extends { referencedRelation: infer ReferencedRelation }
249
- ? ReferencedRelation extends keyof Schema['Tables']
250
- ? R extends { foreignKeyName: value }
251
- ? R & { match: 'fkname' }
252
- : R extends { referencedRelation: value }
253
- ? R & { match: 'refrel' }
254
- : R extends { columns: [value] }
255
- ? R & { match: 'col' }
257
+ > = Relationships extends [infer R, ...infer Rest]
258
+ ? Rest extends GenericRelationship[]
259
+ ? R extends { referencedRelation: infer ReferencedRelation }
260
+ ? ReferencedRelation extends keyof Schema['Tables']
261
+ ? R extends { foreignKeyName: value }
262
+ ? R & { match: 'fkname' }
263
+ : R extends { referencedRelation: value }
264
+ ? R & { match: 'refrel' }
265
+ : R extends { columns: [value] }
266
+ ? R & { match: 'col' }
267
+ : FindMatchingTableRelationships<Schema, Rest, value>
256
268
  : FindMatchingTableRelationships<Schema, Rest, value>
257
- : FindMatchingTableRelationships<Schema, Rest, value>
269
+ : false
258
270
  : false
259
271
  : false
260
272
 
@@ -262,17 +274,19 @@ export type FindMatchingViewRelationships<
262
274
  Schema extends GenericSchema,
263
275
  Relationships extends GenericRelationship[],
264
276
  value extends string
265
- > = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]]
266
- ? R extends { referencedRelation: infer ReferencedRelation }
267
- ? ReferencedRelation extends keyof Schema['Views']
268
- ? R extends { foreignKeyName: value }
269
- ? R & { match: 'fkname' }
270
- : R extends { referencedRelation: value }
271
- ? R & { match: 'refrel' }
272
- : R extends { columns: [value] }
273
- ? R & { match: 'col' }
277
+ > = Relationships extends [infer R, ...infer Rest]
278
+ ? Rest extends GenericRelationship[]
279
+ ? R extends { referencedRelation: infer ReferencedRelation }
280
+ ? ReferencedRelation extends keyof Schema['Views']
281
+ ? R extends { foreignKeyName: value }
282
+ ? R & { match: 'fkname' }
283
+ : R extends { referencedRelation: value }
284
+ ? R & { match: 'refrel' }
285
+ : R extends { columns: [value] }
286
+ ? R & { match: 'col' }
287
+ : FindMatchingViewRelationships<Schema, Rest, value>
274
288
  : FindMatchingViewRelationships<Schema, Rest, value>
275
- : FindMatchingViewRelationships<Schema, Rest, value>
289
+ : false
276
290
  : false
277
291
  : false
278
292
 
@@ -281,36 +295,39 @@ export type FindMatchingHintTableRelationships<
281
295
  Relationships extends GenericRelationship[],
282
296
  hint extends string,
283
297
  name extends string
284
- > = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]]
285
- ? R extends { referencedRelation: infer ReferencedRelation }
286
- ? ReferencedRelation extends name
287
- ? R extends { foreignKeyName: hint }
288
- ? R & { match: 'fkname' }
289
- : R extends { referencedRelation: hint }
290
- ? R & { match: 'refrel' }
291
- : R extends { columns: [hint] }
292
- ? R & { match: 'col' }
298
+ > = Relationships extends [infer R, ...infer Rest]
299
+ ? Rest extends GenericRelationship[]
300
+ ? R extends { referencedRelation: infer ReferencedRelation }
301
+ ? ReferencedRelation extends name
302
+ ? R extends { foreignKeyName: hint }
303
+ ? R & { match: 'fkname' }
304
+ : R extends { referencedRelation: hint }
305
+ ? R & { match: 'refrel' }
306
+ : R extends { columns: [hint] }
307
+ ? R & { match: 'col' }
308
+ : FindMatchingHintTableRelationships<Schema, Rest, hint, name>
293
309
  : FindMatchingHintTableRelationships<Schema, Rest, hint, name>
294
- : FindMatchingHintTableRelationships<Schema, Rest, hint, name>
310
+ : false
295
311
  : false
296
312
  : false
297
-
298
313
  export type FindMatchingHintViewRelationships<
299
314
  Schema extends GenericSchema,
300
315
  Relationships extends GenericRelationship[],
301
316
  hint extends string,
302
317
  name extends string
303
- > = Relationships extends [infer R, ...infer Rest extends GenericRelationship[]]
304
- ? R extends { referencedRelation: infer ReferencedRelation }
305
- ? ReferencedRelation extends name
306
- ? R extends { foreignKeyName: hint }
307
- ? R & { match: 'fkname' }
308
- : R extends { referencedRelation: hint }
309
- ? R & { match: 'refrel' }
310
- : R extends { columns: [hint] }
311
- ? R & { match: 'col' }
318
+ > = Relationships extends [infer R, ...infer Rest]
319
+ ? Rest extends GenericRelationship[]
320
+ ? R extends { referencedRelation: infer ReferencedRelation }
321
+ ? ReferencedRelation extends name
322
+ ? R extends { foreignKeyName: hint }
323
+ ? R & { match: 'fkname' }
324
+ : R extends { referencedRelation: hint }
325
+ ? R & { match: 'refrel' }
326
+ : R extends { columns: [hint] }
327
+ ? R & { match: 'col' }
328
+ : FindMatchingHintViewRelationships<Schema, Rest, hint, name>
312
329
  : FindMatchingHintViewRelationships<Schema, Rest, hint, name>
313
- : FindMatchingHintViewRelationships<Schema, Rest, hint, name>
330
+ : false
314
331
  : false
315
332
  : false
316
333
 
@@ -337,8 +354,10 @@ type TableForwardRelationships<
337
354
  > = TName extends keyof TablesAndViews<Schema>
338
355
  ? UnionToArray<
339
356
  RecursivelyFindRelationships<Schema, TName, keyof TablesAndViews<Schema>>
340
- > extends infer R extends (GenericRelationship & { from: keyof TablesAndViews<Schema> })[]
341
- ? R
357
+ > extends infer R
358
+ ? R extends (GenericRelationship & { from: keyof TablesAndViews<Schema> })[]
359
+ ? R
360
+ : []
342
361
  : []
343
362
  : []
344
363
 
@@ -362,8 +381,7 @@ type FilterRelationships<R, TName, From> = R extends readonly (infer Rel)[]
362
381
  : never
363
382
  : never
364
383
 
365
- // Find a relationship from the parent to the childrens
366
- type ResolveForwardRelationship<
384
+ export type ResolveForwardRelationship<
367
385
  Schema extends GenericSchema,
368
386
  Field extends Ast.FieldNode,
369
387
  CurrentTableOrView extends keyof TablesAndViews<Schema> & string
@@ -371,46 +389,46 @@ type ResolveForwardRelationship<
371
389
  Schema,
372
390
  TablesAndViews<Schema>[Field['name']]['Relationships'],
373
391
  Ast.FieldNode & { name: CurrentTableOrView; hint: Field['hint'] }
374
- > extends infer FoundByName extends GenericRelationship
375
- ? {
376
- referencedTable: TablesAndViews<Schema>[Field['name']]
377
- relation: FoundByName
378
- direction: 'forward'
379
- from: Field['name']
380
- type: 'found-by-name'
381
- }
382
- : // The Field['name'] can sometimes be a reference to the related foreign key
383
- // In that case, we can't use the Field['name'] to get back the relations, instead, we will find all relations pointing
384
- // to our current table or view, and search if we can find a match in it
385
- FindFieldMatchingRelationships<
386
- Schema,
387
- TableForwardRelationships<Schema, CurrentTableOrView>,
388
- Field
389
- > extends infer FoundByMatch extends GenericRelationship & {
390
- from: keyof TablesAndViews<Schema>
391
- }
392
- ? {
393
- referencedTable: TablesAndViews<Schema>[FoundByMatch['from']]
394
- relation: FoundByMatch
395
- direction: 'forward'
396
- from: CurrentTableOrView
397
- type: 'found-by-match'
398
- }
399
- : // Forward relations can also alias other tables via tables joins relationships
400
- // in such cases we crawl all the tables looking for a join table between our current table
401
- // and the Field['name'] desired desitnation
402
- FindJoinTableRelationship<
403
- Schema,
404
- CurrentTableOrView,
405
- Field['name']
406
- > extends infer FoundByJoinTable extends GenericRelationship
407
- ? {
408
- referencedTable: TablesAndViews<Schema>[FoundByJoinTable['referencedRelation']]
409
- relation: FoundByJoinTable & { match: 'refrel' }
410
- direction: 'forward'
411
- from: CurrentTableOrView
412
- type: 'found-by-join-table'
413
- }
392
+ > extends infer FoundByName
393
+ ? FoundByName extends GenericRelationship
394
+ ? {
395
+ referencedTable: TablesAndViews<Schema>[Field['name']]
396
+ relation: FoundByName
397
+ direction: 'forward'
398
+ from: Field['name']
399
+ type: 'found-by-name'
400
+ }
401
+ : FindFieldMatchingRelationships<
402
+ Schema,
403
+ TableForwardRelationships<Schema, CurrentTableOrView>,
404
+ Field
405
+ > extends infer FoundByMatch
406
+ ? FoundByMatch extends GenericRelationship & {
407
+ from: keyof TablesAndViews<Schema>
408
+ }
409
+ ? {
410
+ referencedTable: TablesAndViews<Schema>[FoundByMatch['from']]
411
+ relation: FoundByMatch
412
+ direction: 'forward'
413
+ from: CurrentTableOrView
414
+ type: 'found-by-match'
415
+ }
416
+ : FindJoinTableRelationship<
417
+ Schema,
418
+ CurrentTableOrView,
419
+ Field['name']
420
+ > extends infer FoundByJoinTable
421
+ ? FoundByJoinTable extends GenericRelationship
422
+ ? {
423
+ referencedTable: TablesAndViews<Schema>[FoundByJoinTable['referencedRelation']]
424
+ relation: FoundByJoinTable & { match: 'refrel' }
425
+ direction: 'forward'
426
+ from: CurrentTableOrView
427
+ type: 'found-by-join-table'
428
+ }
429
+ : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`>
430
+ : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`>
431
+ : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`>
414
432
  : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`>
415
433
 
416
434
  /**
@@ -431,7 +449,7 @@ type ResolveForwardRelationship<
431
449
  * referencedColumns: ["id"]
432
450
  * }
433
451
  */
434
- export type FindJoinTableRelationship<
452
+ type ResolveJoinTableRelationship<
435
453
  Schema extends GenericSchema,
436
454
  CurrentTableOrView extends keyof TablesAndViews<Schema> & string,
437
455
  FieldName extends string
@@ -447,6 +465,15 @@ export type FindJoinTableRelationship<
447
465
  : never
448
466
  }[keyof TablesAndViews<Schema>]
449
467
 
468
+ export type FindJoinTableRelationship<
469
+ Schema extends GenericSchema,
470
+ CurrentTableOrView extends keyof TablesAndViews<Schema> & string,
471
+ FieldName extends string
472
+ > = ResolveJoinTableRelationship<Schema, CurrentTableOrView, FieldName> extends infer Result
473
+ ? [Result] extends [never]
474
+ ? false
475
+ : Result
476
+ : never
450
477
  /**
451
478
  * Finds a matching relationship based on the FieldNode's name and optional hint.
452
479
  */
@@ -454,43 +481,35 @@ export type FindFieldMatchingRelationships<
454
481
  Schema extends GenericSchema,
455
482
  Relationships extends GenericRelationship[],
456
483
  Field extends Ast.FieldNode
457
- > = Field extends { hint: infer Hint extends string }
484
+ > = Field extends { hint: string }
458
485
  ? FindMatchingHintTableRelationships<
459
486
  Schema,
460
487
  Relationships,
461
- Hint,
488
+ Field['hint'],
462
489
  Field['name']
463
- > extends infer TableRelationViaHint extends GenericRelationship
464
- ? TableRelationViaHint & {
490
+ > extends GenericRelationship
491
+ ? FindMatchingHintTableRelationships<Schema, Relationships, Field['hint'], Field['name']> & {
465
492
  branch: 'found-in-table-via-hint'
466
493
  hint: Field['hint']
467
494
  }
468
495
  : FindMatchingHintViewRelationships<
469
496
  Schema,
470
497
  Relationships,
471
- Hint,
498
+ Field['hint'],
472
499
  Field['name']
473
- > extends infer TableViewViaHint extends GenericRelationship
474
- ? TableViewViaHint & {
500
+ > extends GenericRelationship
501
+ ? FindMatchingHintViewRelationships<Schema, Relationships, Field['hint'], Field['name']> & {
475
502
  branch: 'found-in-view-via-hint'
476
503
  hint: Field['hint']
477
504
  }
478
505
  : SelectQueryError<'Failed to find matching relation via hint'>
479
- : FindMatchingTableRelationships<
480
- Schema,
481
- Relationships,
482
- Field['name']
483
- > extends infer TableRelationViaName extends GenericRelationship
484
- ? TableRelationViaName & {
506
+ : FindMatchingTableRelationships<Schema, Relationships, Field['name']> extends GenericRelationship
507
+ ? FindMatchingTableRelationships<Schema, Relationships, Field['name']> & {
485
508
  branch: 'found-in-table-via-name'
486
509
  name: Field['name']
487
510
  }
488
- : FindMatchingViewRelationships<
489
- Schema,
490
- Relationships,
491
- Field['name']
492
- > extends infer ViewRelationViaName extends GenericRelationship
493
- ? ViewRelationViaName & {
511
+ : FindMatchingViewRelationships<Schema, Relationships, Field['name']> extends GenericRelationship
512
+ ? FindMatchingViewRelationships<Schema, Relationships, Field['name']> & {
494
513
  branch: 'found-in-view-via-name'
495
514
  name: Field['name']
496
515
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.17.1'
1
+ export const version = '1.17.3'