@@ -266,6 +266,7 @@ type ResolveReverseRelationship<
266266 FoundRelation extends { hint : string } | { match : 'col' }
267267 ? {
268268 referencedTable : TablesAndViews < Schema > [ RelatedRelationName ]
269+ relationName : RelatedRelationName
269270 relation : FoundRelation
270271 direction : 'reverse'
271272 from : CurrentTableOrView
@@ -275,6 +276,7 @@ type ResolveReverseRelationship<
275276 ? SelectQueryError < `Could not embed because more than one relationship was found for '${RelatedRelationName } ' and '${CurrentTableOrView } ' you need to hint the column with ${RelatedRelationName } !<columnName> ?`>
276277 : {
277278 referencedTable : TablesAndViews < Schema > [ RelatedRelationName ]
279+ relationName : RelatedRelationName
278280 relation : FoundRelation
279281 direction : 'reverse'
280282 from : CurrentTableOrView
@@ -428,6 +430,7 @@ export type ResolveForwardRelationship<
428430 ? FoundByName extends GenericRelationship
429431 ? {
430432 referencedTable : TablesAndViews < Schema > [ Field [ 'name' ] ]
433+ relationName : Field [ 'name' ]
431434 relation : FoundByName
432435 direction : 'forward'
433436 from : Field [ 'name' ]
@@ -443,6 +446,7 @@ export type ResolveForwardRelationship<
443446 }
444447 ? {
445448 referencedTable : TablesAndViews < Schema > [ FoundByMatch [ 'from' ] ]
449+ relationName : FoundByMatch [ 'from' ] & string
446450 relation : FoundByMatch
447451 direction : 'forward'
448452 from : CurrentTableOrView
@@ -456,6 +460,7 @@ export type ResolveForwardRelationship<
456460 ? FoundByJoinTable extends GenericRelationship
457461 ? {
458462 referencedTable : TablesAndViews < Schema > [ FoundByJoinTable [ 'referencedRelation' ] ]
463+ relationName : FoundByJoinTable [ 'referencedRelation' ]
459464 relation : FoundByJoinTable & { match : 'refrel' }
460465 direction : 'forward'
461466 from : CurrentTableOrView
@@ -491,6 +496,7 @@ export type ResolveForwardRelationship<
491496 scalarType : FoundEmbededFunctionJoinTableRelation [ 'Returns' ]
492497 direction : 'forward'
493498 from : CurrentTableOrView
499+ relationName : ''
494500 type : 'found-by-embeded-scalar-function'
495501 }
496502 : // Table-valued function: `to` names the target table/view.
@@ -515,6 +521,7 @@ export type ResolveForwardRelationship<
515521 }
516522 direction : 'forward'
517523 from : CurrentTableOrView
524+ relationName : FoundEmbededFunctionJoinTableRelation [ 'SetofOptions' ] [ 'to' ]
518525 type : 'found-by-embeded-function'
519526 }
520527 : SelectQueryError < `could not find the relation between ${CurrentTableOrView } and ${Field [ 'name' ] } `>
@@ -681,26 +688,73 @@ type FindMatchingFunctionBySetofFrom<
681688 ? MatchingFunctionBySetofFrom < Fn , TableName >
682689 : false
683690
691+ type IsUnion < T , U = T > = [ T ] extends [ never ]
692+ ? false
693+ : T extends U
694+ ? [ U ] extends [ T ]
695+ ? false
696+ : true
697+ : never
698+
699+ type SameKeys < A , B > = [ keyof A ] extends [ keyof B ]
700+ ? [ keyof B ] extends [ keyof A ]
701+ ? true
702+ : false
703+ : false
704+
705+ // The type of a function's only argument when it has exactly one and it is required,
706+ // whatever the argument is called. `Args: never` (a zero-argument function in older
707+ // generated types), `Args: Record<PropertyKey, never>`, an optional argument and several
708+ // arguments all resolve to `never`.
709+ type SoleRequiredArgument < Args > = [ Args ] extends [ never ]
710+ ? never
711+ : IsUnion < keyof Args > extends true
712+ ? never
713+ : { } extends Args
714+ ? never
715+ : Args [ keyof Args ]
716+
717+ // Whether one signature of a function (one member of an overload union, as `Fn` is a
718+ // naked type parameter and distributes) is a computed field of a relation: PostgREST
719+ // treats a function whose only argument is the relation's row type as one. The argument
720+ // is matched on its keys rather than its exact type, so that a `Row` whose column types
721+ // were overridden (`MergeDeep`) still matches the generated `Row` the argument references.
722+ type IsComputedFieldFunction < Fn , Row > = Fn extends { Args : infer Args }
723+ ? [ SoleRequiredArgument < Args > ] extends [ never ]
724+ ? false
725+ : SameKeys < SoleRequiredArgument < Args > , Row >
726+ : false
727+
684728type ComputedField <
685729 Schema extends GenericSchema ,
686730 RelationName extends keyof TablesAndViews < Schema > ,
687731 FieldName extends keyof TablesAndViews < Schema > [ RelationName ] [ 'Row' ] ,
688732> = FieldName extends keyof Schema [ 'Functions' ]
689- ? [ Schema [ 'Functions' ] [ FieldName ] [ 'Args' ] ] extends [ never ]
690- ? never
691- : Schema [ 'Functions' ] [ FieldName ] extends {
692- Args : { '' : TablesAndViews < Schema > [ RelationName ] [ 'Row' ] }
693- Returns : any
694- }
695- ? FieldName
696- : never
733+ ? true extends IsComputedFieldFunction <
734+ Schema [ 'Functions' ] [ FieldName ] ,
735+ TablesAndViews < Schema > [ RelationName ] [ 'Row' ]
736+ >
737+ ? FieldName
738+ : never
697739 : never
698740
699- // Given a relation name (Table or View) extract all the "computed fields" based on the Row
700- // object, and the schema functions definitions
701- export type GetComputedFields <
741+ // Computed fields of a relation inferred from its `Row` keys and the schema functions, for
742+ // types generated before the relation declared them in `ComputedFields`.
743+ type InferredComputedFields <
702744 Schema extends GenericSchema ,
703745 RelationName extends keyof TablesAndViews < Schema > ,
704746> = {
705747 [ K in keyof TablesAndViews < Schema > [ RelationName ] [ 'Row' ] ] : ComputedField < Schema , RelationName , K >
706748} [ keyof TablesAndViews < Schema > [ RelationName ] [ 'Row' ] ]
749+
750+ // Given a relation name (Table or View) extract all the "computed fields", which PostgREST
751+ // leaves out of a `*` selection: the names the relation declares in `ComputedFields`, or
752+ // otherwise the ones inferred from the schema functions definitions.
753+ export type GetComputedFields <
754+ Schema extends GenericSchema ,
755+ RelationName extends keyof TablesAndViews < Schema > ,
756+ > = 'ComputedFields' extends keyof TablesAndViews < Schema > [ RelationName ]
757+ ? TablesAndViews < Schema > [ RelationName ] extends { ComputedFields ?: infer Fields extends string }
758+ ? Fields
759+ : never
760+ : InferredComputedFields < Schema , RelationName >
0 commit comments