|
| 1 | +import { GenericFunction, GenericSetofOption } from '../types' |
1 | 2 | import { Ast } from './parser'
|
2 | 3 | import {
|
3 | 4 | AggregateFunctions,
|
@@ -452,6 +453,36 @@ export type ResolveForwardRelationship<
|
452 | 453 | from: CurrentTableOrView
|
453 | 454 | type: 'found-by-join-table'
|
454 | 455 | }
|
| 456 | + : ResolveEmbededFunctionJoinTableRelationship< |
| 457 | + Schema, |
| 458 | + CurrentTableOrView, |
| 459 | + Field['name'] |
| 460 | + > extends infer FoundEmbededFunctionJoinTableRelation |
| 461 | + ? FoundEmbededFunctionJoinTableRelation extends GenericSetofOption |
| 462 | + ? { |
| 463 | + referencedTable: TablesAndViews<Schema>[FoundEmbededFunctionJoinTableRelation['to']] |
| 464 | + relation: { |
| 465 | + foreignKeyName: `${Field['name']}_${CurrentTableOrView}_${FoundEmbededFunctionJoinTableRelation['to']}_forward` |
| 466 | + columns: [] |
| 467 | + isOneToOne: FoundEmbededFunctionJoinTableRelation['isOneToOne'] extends true |
| 468 | + ? true |
| 469 | + : false |
| 470 | + referencedColumns: [] |
| 471 | + referencedRelation: FoundEmbededFunctionJoinTableRelation['to'] |
| 472 | + } & { |
| 473 | + match: 'func' |
| 474 | + isNotNullable: FoundEmbededFunctionJoinTableRelation['isNotNullable'] extends true |
| 475 | + ? true |
| 476 | + : FoundEmbededFunctionJoinTableRelation['isSetofReturn'] extends true |
| 477 | + ? false |
| 478 | + : true |
| 479 | + isSetofReturn: FoundEmbededFunctionJoinTableRelation['isSetofReturn'] |
| 480 | + } |
| 481 | + direction: 'forward' |
| 482 | + from: CurrentTableOrView |
| 483 | + type: 'found-by-embeded-function' |
| 484 | + } |
| 485 | + : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> |
455 | 486 | : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`>
|
456 | 487 | : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`>
|
457 | 488 | : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`>
|
@@ -495,6 +526,19 @@ type ResolveJoinTableRelationship<
|
495 | 526 | : never
|
496 | 527 | }[keyof TablesAndViews<Schema>]
|
497 | 528 |
|
| 529 | +type ResolveEmbededFunctionJoinTableRelationship< |
| 530 | + Schema extends GenericSchema, |
| 531 | + CurrentTableOrView extends keyof TablesAndViews<Schema> & string, |
| 532 | + FieldName extends string |
| 533 | +> = FindMatchingFunctionBySetofFrom< |
| 534 | + Schema['Functions'][FieldName], |
| 535 | + CurrentTableOrView |
| 536 | +> extends infer Fn |
| 537 | + ? Fn extends GenericFunction |
| 538 | + ? Fn['SetofOptions'] |
| 539 | + : false |
| 540 | + : false |
| 541 | + |
498 | 542 | export type FindJoinTableRelationship<
|
499 | 543 | Schema extends GenericSchema,
|
500 | 544 | CurrentTableOrView extends keyof TablesAndViews<Schema> & string,
|
@@ -579,6 +623,50 @@ export type IsStringUnion<T> = string extends T
|
579 | 623 | : true
|
580 | 624 | : false
|
581 | 625 |
|
| 626 | +// Functions matching utils |
| 627 | +export type IsMatchingArgs< |
| 628 | + FnArgs extends GenericFunction['Args'], |
| 629 | + PassedArgs extends GenericFunction['Args'] |
| 630 | +> = [FnArgs] extends [Record<PropertyKey, never>] |
| 631 | + ? PassedArgs extends Record<PropertyKey, never> |
| 632 | + ? true |
| 633 | + : false |
| 634 | + : keyof PassedArgs extends keyof FnArgs |
| 635 | + ? PassedArgs extends FnArgs |
| 636 | + ? true |
| 637 | + : false |
| 638 | + : false |
| 639 | + |
| 640 | +export type MatchingFunctionArgs< |
| 641 | + Fn extends GenericFunction, |
| 642 | + Args extends GenericFunction['Args'] |
| 643 | +> = Fn extends { Args: infer A extends GenericFunction['Args'] } |
| 644 | + ? IsMatchingArgs<A, Args> extends true |
| 645 | + ? Fn |
| 646 | + : never |
| 647 | + : never |
| 648 | + |
| 649 | +export type FindMatchingFunctionByArgs< |
| 650 | + FnUnion, |
| 651 | + Args extends GenericFunction['Args'] |
| 652 | +> = FnUnion extends infer Fn extends GenericFunction ? MatchingFunctionArgs<Fn, Args> : never |
| 653 | + |
| 654 | +type MatchingFunctionBySetofFrom< |
| 655 | + Fn extends GenericFunction, |
| 656 | + TableName extends string |
| 657 | +> = Fn['SetofOptions'] extends GenericSetofOption |
| 658 | + ? TableName extends Fn['SetofOptions']['from'] |
| 659 | + ? Fn |
| 660 | + : never |
| 661 | + : never |
| 662 | + |
| 663 | +type FindMatchingFunctionBySetofFrom< |
| 664 | + FnUnion, |
| 665 | + TableName extends string |
| 666 | +> = FnUnion extends infer Fn extends GenericFunction |
| 667 | + ? MatchingFunctionBySetofFrom<Fn, TableName> |
| 668 | + : false |
| 669 | + |
582 | 670 | type ComputedField<
|
583 | 671 | Schema extends GenericSchema,
|
584 | 672 | RelationName extends keyof TablesAndViews<Schema>,
|
|
0 commit comments