1
- import { ClientServerOptions , GenericTable } from '../types'
2
- import { ContainsNull , GenericRelationship , PostgreSQLTypes } from './types'
3
1
import { Ast , ParseQuery } from './parser'
4
2
import {
5
3
AggregateFunctions ,
@@ -9,6 +7,11 @@ import {
9
7
Prettify ,
10
8
TablesAndViews ,
11
9
TypeScriptTypes ,
10
+ ContainsNull ,
11
+ GenericRelationship ,
12
+ PostgreSQLTypes ,
13
+ GenericTable ,
14
+ ClientServerOptions ,
12
15
} from './types'
13
16
import {
14
17
CheckDuplicateEmbededReference ,
@@ -366,7 +369,7 @@ export type ProcessEmbeddedResource<
366
369
ResolveRelationship < Schema , Relationships , Field , CurrentTableOrView > extends infer Resolved
367
370
? Resolved extends {
368
371
referencedTable : Pick < GenericTable , 'Row' | 'Relationships' >
369
- relation : GenericRelationship & { match : 'refrel' | 'col' | 'fkname' }
372
+ relation : GenericRelationship & { match : 'refrel' | 'col' | 'fkname' | 'func' }
370
373
direction : string
371
374
}
372
375
? ProcessEmbeddedResourceResult < ClientOptions , Schema , Resolved , Field , CurrentTableOrView >
@@ -385,7 +388,12 @@ type ProcessEmbeddedResourceResult<
385
388
Schema extends GenericSchema ,
386
389
Resolved extends {
387
390
referencedTable : Pick < GenericTable , 'Row' | 'Relationships' >
388
- relation : GenericRelationship & { match : 'refrel' | 'col' | 'fkname' }
391
+ relation : GenericRelationship & {
392
+ match : 'refrel' | 'col' | 'fkname' | 'func'
393
+ isNotNullable ?: boolean
394
+ referencedRelation : string
395
+ isSetofReturn ?: boolean
396
+ }
389
397
direction : string
390
398
} ,
391
399
Field extends Ast . FieldNode ,
@@ -395,7 +403,11 @@ type ProcessEmbeddedResourceResult<
395
403
ClientOptions ,
396
404
Schema ,
397
405
Resolved [ 'referencedTable' ] [ 'Row' ] ,
398
- Field [ 'name' ] ,
406
+ // For embeded function selection, the source of truth is the 'referencedRelation'
407
+ // coming from the SetofOptions.to parameter
408
+ Resolved [ 'relation' ] [ 'match' ] extends 'func'
409
+ ? Resolved [ 'relation' ] [ 'referencedRelation' ]
410
+ : Field [ 'name' ] ,
399
411
Resolved [ 'referencedTable' ] [ 'Relationships' ] ,
400
412
Field [ 'children' ] extends undefined
401
413
? [ ]
@@ -410,7 +422,18 @@ type ProcessEmbeddedResourceResult<
410
422
? ProcessedChildren
411
423
: ProcessedChildren [ ]
412
424
: Resolved [ 'relation' ] [ 'isOneToOne' ] extends true
413
- ? ProcessedChildren | null
425
+ ? Resolved [ 'relation' ] [ 'match' ] extends 'func'
426
+ ? Resolved [ 'relation' ] [ 'isNotNullable' ] extends true
427
+ ? Resolved [ 'relation' ] [ 'isSetofReturn' ] extends true
428
+ ? ProcessedChildren
429
+ : // TODO: This shouldn't be necessary but is due in an inconsitency in PostgREST v12/13 where if a function
430
+ // is declared with RETURNS <table-name> instead of RETURNS SETOF <table-name> ROWS 1
431
+ // In case where there is no object matching the relations, the object will be returned with all the properties within it
432
+ // set to null, we mimic this buggy behavior for type safety an issue is opened on postgREST here:
433
+ // https://github.com/PostgREST/postgrest/issues/4234
434
+ { [ P in keyof ProcessedChildren ] : ProcessedChildren [ P ] | null }
435
+ : ProcessedChildren | null
436
+ : ProcessedChildren | null
414
437
: ProcessedChildren [ ]
415
438
: // If the relation is a self-reference it'll always be considered as reverse relationship
416
439
Resolved [ 'relation' ] [ 'referencedRelation' ] extends CurrentTableOrView
0 commit comments