1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {Filter, InclusionFilter} from '@loopback/filter';
|
7 | import {includeFieldIfNot, InvalidPolymorphismError} from '../../';
|
8 | import {AnyObject, Options} from '../../common-types';
|
9 | import {Entity} from '../../model';
|
10 | import {EntityCrudRepository} from '../../repositories';
|
11 | import {
|
12 | deduplicate,
|
13 | findByForeignKeys,
|
14 | flattenTargetsOfOneToOneRelation,
|
15 | StringKeyOf,
|
16 | } from '../relation.helpers';
|
17 | import {
|
18 | BelongsToDefinition,
|
19 | Getter,
|
20 | InclusionResolver,
|
21 | } from '../relation.types';
|
22 | import {resolveBelongsToMetadata} from './belongs-to.helpers';
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | export function createBelongsToInclusionResolver<
|
36 | Target extends Entity,
|
37 | TargetID,
|
38 | TargetRelations extends object,
|
39 | >(
|
40 | meta: BelongsToDefinition,
|
41 | getTargetRepoDict: {
|
42 | [repoType: string]: Getter<
|
43 | EntityCrudRepository<Target, TargetID, TargetRelations>
|
44 | >;
|
45 | },
|
46 | ): InclusionResolver<Entity, Target> {
|
47 | const relationMeta = resolveBelongsToMetadata(meta);
|
48 |
|
49 | return async function fetchBelongsToModel(
|
50 | entities: Entity[],
|
51 | inclusion: InclusionFilter,
|
52 | options?: Options,
|
53 | ): Promise<((Target & object) | undefined)[]> {
|
54 | if (!entities.length) return [];
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | const sourceKey = relationMeta.keyFrom;
|
60 | const targetKey = relationMeta.keyTo as StringKeyOf<Target>;
|
61 | const targetDiscriminator: keyof Entity | undefined =
|
62 | relationMeta.polymorphic
|
63 | ? (relationMeta.polymorphic.discriminator as keyof Entity)
|
64 | : undefined;
|
65 |
|
66 | const scope =
|
67 | typeof inclusion === 'string' ? {} : (inclusion.scope as Filter<Target>);
|
68 |
|
69 |
|
70 | const sourceIdsCategorized: {
|
71 | [concreteItemType: string]: Target[StringKeyOf<Target>][];
|
72 | } = {};
|
73 | if (targetDiscriminator) {
|
74 | entities.forEach((value, index, allEntites) => {
|
75 | const concreteType = String(value[targetDiscriminator]);
|
76 | if (!getTargetRepoDict[concreteType]) {
|
77 | throw new InvalidPolymorphismError(concreteType, targetDiscriminator);
|
78 | }
|
79 | if (!sourceIdsCategorized[concreteType]) {
|
80 | sourceIdsCategorized[concreteType] = [];
|
81 | }
|
82 | sourceIdsCategorized[concreteType].push(
|
83 | (value as AnyObject)[sourceKey],
|
84 | );
|
85 | });
|
86 | } else {
|
87 | const concreteType = relationMeta.target().name;
|
88 | if (!getTargetRepoDict[concreteType]) {
|
89 | throw new InvalidPolymorphismError(concreteType);
|
90 | }
|
91 | entities.forEach((value, index, allEntites) => {
|
92 | if (!sourceIdsCategorized[concreteType]) {
|
93 | sourceIdsCategorized[concreteType] = [];
|
94 | }
|
95 | sourceIdsCategorized[concreteType].push(
|
96 | (value as AnyObject)[sourceKey],
|
97 | );
|
98 | });
|
99 | }
|
100 |
|
101 |
|
102 | const changedTargetKeyField = includeFieldIfNot(scope?.fields, targetKey);
|
103 | let needToRemoveTargetKeyFieldLater = false;
|
104 | if (changedTargetKeyField !== false) {
|
105 | scope.fields = changedTargetKeyField;
|
106 | needToRemoveTargetKeyFieldLater = true;
|
107 | }
|
108 |
|
109 | const targetCategorized: {
|
110 | [concreteItemType: string]: ((Target & TargetRelations) | undefined)[];
|
111 | } = {};
|
112 | for (const k of Object.keys(sourceIdsCategorized)) {
|
113 | const targetRepo = await getTargetRepoDict[k]();
|
114 | const targetsFound = await findByForeignKeys(
|
115 | targetRepo,
|
116 | targetKey,
|
117 | deduplicate(sourceIdsCategorized[k]).filter(e => e),
|
118 | scope,
|
119 | options,
|
120 | );
|
121 | targetCategorized[k] = flattenTargetsOfOneToOneRelation(
|
122 | sourceIdsCategorized[k],
|
123 | targetsFound,
|
124 | targetKey,
|
125 | );
|
126 |
|
127 |
|
128 | if (needToRemoveTargetKeyFieldLater) {
|
129 | targetCategorized[k] = targetCategorized[k].map(e => {
|
130 | if (e) {
|
131 | delete e[targetKey];
|
132 | }
|
133 | return e;
|
134 | });
|
135 | }
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | if (targetDiscriminator) {
|
151 | const allTargets: ((Target & TargetRelations) | undefined)[] = [];
|
152 | entities.forEach((value, index, allEntites) => {
|
153 | allTargets.push(
|
154 | targetCategorized[String(value[targetDiscriminator])].shift(),
|
155 | );
|
156 | });
|
157 | return allTargets;
|
158 | } else {
|
159 | return targetCategorized[relationMeta.target().name];
|
160 | }
|
161 | };
|
162 | }
|