Skip to content

Commit 1bea938

Browse files
committed
feat: pass all tests
1 parent 19c9f3a commit 1bea938

File tree

9 files changed

+435
-91
lines changed

9 files changed

+435
-91
lines changed

src/PostgrestClient.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ export type GetRpcFunctionFilterBuilderByArgs<
5454
: Fn['Returns'] extends Record<string, unknown>
5555
? Fn['Returns']
5656
: never
57-
Result: Fn['Returns']
57+
Result: Fn['SetofOptions'] extends GenericSetofOption
58+
? Fn['SetofOptions']['isSetofReturn'] extends true
59+
? Fn['SetofOptions']['isOneToOne'] extends true
60+
? Fn['Returns'][]
61+
: Fn['Returns']
62+
: Fn['Returns']
63+
: Fn['Returns']
5864
RelationName: Fn['SetofOptions'] extends GenericSetofOption
5965
? Fn['SetofOptions']['to']
6066
: FnName

src/PostgrestTransformBuilder.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ export default class PostgrestTransformBuilder<
3535
ClientOptions,
3636
Schema,
3737
Row,
38-
NewResultOne[],
38+
Method extends 'RPC'
39+
? Result extends unknown[]
40+
? NewResultOne[]
41+
: NewResultOne
42+
: NewResultOne[],
3943
RelationName,
4044
Relationships,
4145
Method
@@ -60,7 +64,11 @@ export default class PostgrestTransformBuilder<
6064
ClientOptions,
6165
Schema,
6266
Row,
63-
NewResultOne[],
67+
Method extends 'RPC'
68+
? Result extends unknown[]
69+
? NewResultOne[]
70+
: NewResultOne
71+
: NewResultOne[],
6472
RelationName,
6573
Relationships,
6674
Method

src/select-query-parser/result.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ type ProcessEmbeddedResourceResult<
377377
match: 'refrel' | 'col' | 'fkname' | 'func'
378378
isNotNullable?: boolean
379379
referencedRelation: string
380+
isSetofReturn?: boolean
380381
}
381382
direction: string
382383
},
@@ -407,7 +408,14 @@ type ProcessEmbeddedResourceResult<
407408
: Resolved['relation']['isOneToOne'] extends true
408409
? Resolved['relation']['match'] extends 'func'
409410
? Resolved['relation']['isNotNullable'] extends true
410-
? ProcessedChildren
411+
? Resolved['relation']['isSetofReturn'] extends true
412+
? ProcessedChildren
413+
: // TODO: This shouldn't be necessary but is due in an inconsitency in PostgREST v12/13 where if a function
414+
// is declared with RETURNS <table-name> instead of RETURNS SETOF <table-name> ROWS 1
415+
// In case where there is no object matching the relations, the object will be returned with all the properties within it
416+
// set to null, we mimic this buggy behavior for type safety an issue is opened on postgREST here:
417+
// https://github.com/PostgREST/postgrest/issues/4234
418+
{ [P in keyof ProcessedChildren]: ProcessedChildren[P] | null }
411419
: ProcessedChildren | null
412420
: ProcessedChildren | null
413421
: ProcessedChildren[]

src/select-query-parser/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,10 @@ export type ResolveForwardRelationship<
473473
match: 'func'
474474
isNotNullable: FoundEmbededFunctionJoinTableRelation['isNotNullable'] extends true
475475
? true
476-
: false
476+
: FoundEmbededFunctionJoinTableRelation['isSetofReturn'] extends true
477+
? false
478+
: true
479+
isSetofReturn: FoundEmbededFunctionJoinTableRelation['isSetofReturn']
477480
}
478481
direction: 'forward'
479482
from: CurrentTableOrView

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export type GenericNonUpdatableView = {
6161
export type GenericView = GenericUpdatableView | GenericNonUpdatableView
6262

6363
export type GenericSetofOption = {
64+
isSetofReturn?: boolean | undefined
6465
isOneToOne?: boolean | undefined
6566
isNotNullable?: boolean | undefined
6667
to: string

0 commit comments

Comments
 (0)