( tables: Tables, relationsMap: RelationsMap<Tables> )
| 771 | } |
| 772 | |
| 773 | function setRelations<Tables extends Record<string, AnyTable>>( |
| 774 | tables: Tables, |
| 775 | relationsMap: RelationsMap<Tables> |
| 776 | ) { |
| 777 | const impliedRelations: { |
| 778 | relationName: string; |
| 779 | relation: ImplicitRelationInit<RelationType, Tables, keyof Tables>; |
| 780 | }[] = []; |
| 781 | const explicitRelations: { |
| 782 | implicitRelationName?: string; |
| 783 | relation: ExplicitRelation; |
| 784 | }[] = []; |
| 785 | |
| 786 | for (const k in relationsMap) { |
| 787 | const relationFn = relationsMap[k]; |
| 788 | if (!relationFn) continue; |
| 789 | const table = tables[k]; |
| 790 | |
| 791 | const relations = relationFn(relationBuilder(tables, k)); |
| 792 | for (const name in relations) { |
| 793 | const relation = relations[name]; |
| 794 | if (!relation) continue; |
| 795 | |
| 796 | if (relation instanceof ImplicitRelationInit) { |
| 797 | impliedRelations.push({ |
| 798 | relationName: name, |
| 799 | relation, |
| 800 | }); |
| 801 | continue; |
| 802 | } |
| 803 | |
| 804 | if (relation instanceof ExplicitRelationInit) { |
| 805 | const output = relation.init(name); |
| 806 | |
| 807 | explicitRelations.push({ |
| 808 | relation: output, |
| 809 | implicitRelationName: relation.implyingRelationName, |
| 810 | }); |
| 811 | |
| 812 | table.relations[name] = output; |
| 813 | if (output.foreignKey) table.foreignKeys.push(output.foreignKey); |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | for (const { relation, relationName } of impliedRelations) { |
| 819 | const referencer = relation.referencer; |
| 820 | const explicits = explicitRelations.filter((item) => { |
| 821 | if (item.implicitRelationName) { |
| 822 | return item.implicitRelationName === relationName; |
| 823 | } |
| 824 | |
| 825 | return ( |
| 826 | item.relation.table === referencer && |
| 827 | item.relation.referencer === relation.referencedTable |
| 828 | ); |
| 829 | }); |
| 830 |
no test coverage detected