1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.resolveHasManyThroughMetadata = exports.createThroughConstraintFromTarget = exports.getTargetIdsFromTargetModels = exports.createThroughConstraintFromSource = exports.getTargetKeysFromThroughModels = exports.createTargetConstraintFromThrough = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
10 | const lodash_1 = require("lodash");
|
11 | const __1 = require("../..");
|
12 | const has_many_helpers_1 = require("./has-many.helpers");
|
13 | const debug = (0, debug_1.default)('loopback:repository:relations:has-many-through:helpers');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function createTargetConstraintFromThrough(relationMeta, throughInstances) {
|
53 | const fkValues = getTargetKeysFromThroughModels(relationMeta, throughInstances);
|
54 | const targetPrimaryKey = relationMeta.keyTo;
|
55 |
|
56 | const constraint = {
|
57 | [targetPrimaryKey]: fkValues.length === 1 ? fkValues[0] : { inq: fkValues },
|
58 | };
|
59 | return constraint;
|
60 | }
|
61 | exports.createTargetConstraintFromThrough = createTargetConstraintFromThrough;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function getTargetKeysFromThroughModels(relationMeta, throughInstances) {
|
100 | const targetFkName = relationMeta.through.keyTo;
|
101 |
|
102 | let fkValues = throughInstances.map((throughInstance) => throughInstance[targetFkName]);
|
103 | fkValues = (0, __1.deduplicate)(fkValues);
|
104 | return fkValues;
|
105 | }
|
106 | exports.getTargetKeysFromThroughModels = getTargetKeysFromThroughModels;
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | function createThroughConstraintFromSource(relationMeta, fkValue) {
|
132 | const sourceFkName = relationMeta.through.keyFrom;
|
133 |
|
134 | const constraint = { [sourceFkName]: fkValue };
|
135 | return constraint;
|
136 | }
|
137 | exports.createThroughConstraintFromSource = createThroughConstraintFromSource;
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | function getTargetIdsFromTargetModels(relationMeta, targetInstances) {
|
173 | const targetId = relationMeta.keyTo;
|
174 |
|
175 | let ids = [];
|
176 | ids = targetInstances.map((targetInstance) => targetInstance[targetId]);
|
177 | ids = (0, __1.deduplicate)(ids);
|
178 | return ids;
|
179 | }
|
180 | exports.getTargetIdsFromTargetModels = getTargetIdsFromTargetModels;
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | function createThroughConstraintFromTarget(relationMeta, fkValues) {
|
209 | if (fkValues === undefined || fkValues.length === 0) {
|
210 | throw new Error('"fkValue" must be provided');
|
211 | }
|
212 | const targetFkName = relationMeta.through.keyTo;
|
213 |
|
214 | const constraint = fkValues.length === 1
|
215 | ? { [targetFkName]: fkValues[0] }
|
216 | : { [targetFkName]: { inq: fkValues } };
|
217 | return constraint;
|
218 | }
|
219 | exports.createThroughConstraintFromTarget = createThroughConstraintFromTarget;
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | function resolveHasManyThroughMetadata(relationMeta) {
|
228 | var _a, _b, _c, _d, _e, _f;
|
229 |
|
230 | relationMeta = (0, has_many_helpers_1.resolveHasManyMetaHelper)(relationMeta);
|
231 | if (!relationMeta.through) {
|
232 | const reason = 'through must be specified';
|
233 | throw new __1.InvalidRelationError(reason, relationMeta);
|
234 | }
|
235 | if (!(0, __1.isTypeResolver)((_a = relationMeta.through) === null || _a === void 0 ? void 0 : _a.model)) {
|
236 | const reason = 'through.model must be a type resolver';
|
237 | throw new __1.InvalidRelationError(reason, relationMeta);
|
238 | }
|
239 | const throughModel = relationMeta.through.model();
|
240 | const throughModelProperties = (_b = throughModel.definition) === null || _b === void 0 ? void 0 : _b.properties;
|
241 | const targetModel = relationMeta.target();
|
242 | const targetModelProperties = (_c = targetModel.definition) === null || _c === void 0 ? void 0 : _c.properties;
|
243 |
|
244 | if (relationMeta.through.keyTo &&
|
245 | throughModelProperties[relationMeta.through.keyTo] &&
|
246 | relationMeta.through.keyFrom &&
|
247 | throughModelProperties[relationMeta.through.keyFrom] &&
|
248 | relationMeta.keyTo &&
|
249 | targetModelProperties[relationMeta.keyTo] &&
|
250 | (relationMeta.through.polymorphic === false ||
|
251 | (typeof relationMeta.through.polymorphic === 'object' &&
|
252 | relationMeta.through.polymorphic.discriminator.length > 0))) {
|
253 |
|
254 | return relationMeta;
|
255 | }
|
256 | const sourceModel = relationMeta.source;
|
257 | debug('Resolved model %s from given metadata: %o', targetModel.modelName, targetModel);
|
258 | debug('Resolved model %s from given metadata: %o', throughModel.modelName, throughModel);
|
259 | const sourceFkName = (_d = relationMeta.through.keyFrom) !== null && _d !== void 0 ? _d : (0, lodash_1.camelCase)(sourceModel.modelName + '_id');
|
260 | if (!throughModelProperties[sourceFkName]) {
|
261 | const reason = `through model ${throughModel.name} is missing definition of source foreign key`;
|
262 | throw new __1.InvalidRelationError(reason, relationMeta);
|
263 | }
|
264 | const targetFkName = (_e = relationMeta.through.keyTo) !== null && _e !== void 0 ? _e : (0, lodash_1.camelCase)(targetModel.modelName + '_id');
|
265 | if (!throughModelProperties[targetFkName]) {
|
266 | const reason = `through model ${throughModel.name} is missing definition of target foreign key`;
|
267 | throw new __1.InvalidRelationError(reason, relationMeta);
|
268 | }
|
269 | const targetPrimaryKey = (_f = relationMeta.keyTo) !== null && _f !== void 0 ? _f : targetModel.definition.idProperties()[0];
|
270 | if (!targetPrimaryKey || !targetModelProperties[targetPrimaryKey]) {
|
271 | const reason = `target model ${targetModel.modelName} does not have any primary key (id property)`;
|
272 | throw new __1.InvalidRelationError(reason, relationMeta);
|
273 | }
|
274 | let throughPolymorphic;
|
275 | if (relationMeta.through.polymorphic === undefined ||
|
276 | relationMeta.through.polymorphic === false ||
|
277 | !relationMeta.through.polymorphic) {
|
278 | const polymorphicFalse = false;
|
279 | throughPolymorphic = polymorphicFalse;
|
280 | }
|
281 | else {
|
282 | if (relationMeta.through.polymorphic === true) {
|
283 | const polymorphicObject = {
|
284 | discriminator: (0, lodash_1.camelCase)(relationMeta.target().name + '_type'),
|
285 | };
|
286 | throughPolymorphic = polymorphicObject;
|
287 | }
|
288 | else {
|
289 | const polymorphicObject = relationMeta.through
|
290 | .polymorphic;
|
291 | throughPolymorphic = polymorphicObject;
|
292 | }
|
293 | }
|
294 | return Object.assign(relationMeta, {
|
295 | keyTo: targetPrimaryKey,
|
296 | keyFrom: relationMeta.keyFrom,
|
297 | through: {
|
298 | ...relationMeta.through,
|
299 | keyTo: targetFkName,
|
300 | keyFrom: sourceFkName,
|
301 | polymorphic: throughPolymorphic,
|
302 | },
|
303 | });
|
304 | }
|
305 | exports.resolveHasManyThroughMetadata = resolveHasManyThroughMetadata;
|
306 |
|
\ | No newline at end of file |