1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.isBsonType = exports.deduplicate = exports.reduceAsSingleItem = exports.reduceAsArray = exports.normalizeKey = exports.getKeyValue = exports.buildLookupMap = exports.flattenMapByKeys = exports.flattenTargetsOfOneToManyRelation = exports.flattenTargetsOfOneToOneRelation = exports.includeRelatedModels = exports.findByForeignKeys = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const assert_1 = tslib_1.__importDefault(require("assert"));
|
10 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
11 | const lodash_1 = tslib_1.__importStar(require("lodash"));
|
12 | const __1 = require("..");
|
13 | const debug = (0, debug_1.default)('loopback:repository:relation-helpers');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | async function findByForeignKeys(targetRepository, fkName, fkValues, scope, options) {
|
24 | let value;
|
25 | scope = (0, lodash_1.cloneDeep)(scope);
|
26 | if (Array.isArray(fkValues)) {
|
27 | if (fkValues.length === 0)
|
28 | return [];
|
29 | value = fkValues.length === 1 ? fkValues[0] : { inq: fkValues };
|
30 | }
|
31 | else {
|
32 | value = fkValues;
|
33 | }
|
34 | let useScopeFilterGlobally = false;
|
35 |
|
36 |
|
37 |
|
38 | if (options) {
|
39 | useScopeFilterGlobally = options.isThroughModelInclude;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 | if (!(scope === null || scope === void 0 ? void 0 : scope.limit)) {
|
45 | useScopeFilterGlobally = true;
|
46 | }
|
47 |
|
48 |
|
49 | if (scope === null || scope === void 0 ? void 0 : scope.totalLimit) {
|
50 | scope.limit = scope.totalLimit;
|
51 | useScopeFilterGlobally = true;
|
52 | delete scope.totalLimit;
|
53 | }
|
54 | const isScopeSet = scope && !lodash_1.default.isEmpty(scope);
|
55 | if (isScopeSet && Array.isArray(fkValues) && !useScopeFilterGlobally) {
|
56 |
|
57 |
|
58 |
|
59 | const findPromises = fkValues.map(fk => {
|
60 | const where = { [fkName]: fk };
|
61 | let localScope = (0, lodash_1.cloneDeep)(scope);
|
62 |
|
63 | localScope = new __1.FilterBuilder(localScope).impose({ where }).filter;
|
64 | return targetRepository.find(localScope, options);
|
65 | });
|
66 | return Promise.all(findPromises).then(findResults => {
|
67 |
|
68 | return lodash_1.default.flatten(findResults);
|
69 | });
|
70 | }
|
71 | else {
|
72 | const where = { [fkName]: value };
|
73 | if (isScopeSet) {
|
74 |
|
75 | scope = new __1.FilterBuilder(scope).impose({ where }).filter;
|
76 | }
|
77 | else {
|
78 | scope = { where };
|
79 | }
|
80 | return targetRepository.find(scope, options);
|
81 | }
|
82 | }
|
83 | exports.findByForeignKeys = findByForeignKeys;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | async function includeRelatedModels(targetRepository, entities, include, options) {
|
94 | if (options === null || options === void 0 ? void 0 : options.polymorphicType) {
|
95 | include = include === null || include === void 0 ? void 0 : include.filter(inclusionFilter => {
|
96 | if (typeof inclusionFilter === 'string') {
|
97 | return true;
|
98 | }
|
99 | else {
|
100 | if (inclusionFilter.targetType === undefined ||
|
101 | inclusionFilter.targetType === (options === null || options === void 0 ? void 0 : options.polymorphicType)) {
|
102 | return true;
|
103 | }
|
104 | }
|
105 | });
|
106 | }
|
107 | else {
|
108 | include = (0, lodash_1.cloneDeep)(include);
|
109 | }
|
110 | if (include) {
|
111 | entities = (0, lodash_1.cloneDeep)(entities);
|
112 | }
|
113 | const result = entities;
|
114 | if (!include)
|
115 | return result;
|
116 | const invalidInclusions = include.filter(inclusionFilter => !isInclusionAllowed(targetRepository, inclusionFilter));
|
117 | if (invalidInclusions.length) {
|
118 | const msg = 'Invalid "filter.include" entries: ' +
|
119 | invalidInclusions
|
120 | .map(inclusionFilter => JSON.stringify(inclusionFilter))
|
121 | .join('; ');
|
122 | const err = new Error(msg);
|
123 | Object.assign(err, {
|
124 | code: 'INVALID_INCLUSION_FILTER',
|
125 | statusCode: 400,
|
126 | });
|
127 | throw err;
|
128 | }
|
129 | const resolveTasks = include.map(async (inclusionFilter) => {
|
130 | const relationName = typeof inclusionFilter === 'string'
|
131 | ? inclusionFilter
|
132 | : inclusionFilter.relation;
|
133 | const resolver = targetRepository.inclusionResolvers.get(relationName);
|
134 | const targets = await resolver(entities, inclusionFilter, options);
|
135 | result.forEach((entity, ix) => {
|
136 | const src = entity;
|
137 | src[relationName] = targets[ix];
|
138 | });
|
139 | });
|
140 | await Promise.all(resolveTasks);
|
141 | return result;
|
142 | }
|
143 | exports.includeRelatedModels = includeRelatedModels;
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | function isInclusionAllowed(targetRepository, include) {
|
152 | const relationName = typeof include === 'string' ? include : include.relation;
|
153 | if (!relationName) {
|
154 | debug('isInclusionAllowed for %j? No: missing relation name', include);
|
155 | return false;
|
156 | }
|
157 | const allowed = targetRepository.inclusionResolvers.has(relationName);
|
158 | debug('isInclusionAllowed for %j (relation %s)? %s', include, allowed);
|
159 | return allowed;
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | function flattenTargetsOfOneToOneRelation(sourceIds, targetEntities, targetKey) {
|
171 | const lookup = buildLookupMap(targetEntities, targetKey, reduceAsSingleItem);
|
172 | return flattenMapByKeys(sourceIds, lookup);
|
173 | }
|
174 | exports.flattenTargetsOfOneToOneRelation = flattenTargetsOfOneToOneRelation;
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | function flattenTargetsOfOneToManyRelation(sourceIds, targetEntities, targetKey) {
|
186 | debug('flattenTargetsOfOneToManyRelation');
|
187 | debug('sourceIds', sourceIds);
|
188 | debug('sourceId types', sourceIds.map(i => typeof i));
|
189 | debug('targetEntities', targetEntities);
|
190 | debug('targetKey', targetKey);
|
191 | const lookup = buildLookupMap(targetEntities, targetKey, reduceAsArray);
|
192 | debug('lookup map', lookup);
|
193 | return flattenMapByKeys(sourceIds, lookup);
|
194 | }
|
195 | exports.flattenTargetsOfOneToManyRelation = flattenTargetsOfOneToManyRelation;
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | function flattenMapByKeys(sourceIds, targetMap) {
|
204 | const result = new Array(sourceIds.length);
|
205 |
|
206 |
|
207 | sourceIds.forEach((id, index) => {
|
208 | const key = normalizeKey(id);
|
209 | const target = targetMap.get(key);
|
210 | result[index] = target;
|
211 | });
|
212 | return result;
|
213 | }
|
214 | exports.flattenMapByKeys = flattenMapByKeys;
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | function buildLookupMap(list, keyName, reducer) {
|
224 | const lookup = new Map();
|
225 | for (const entity of list) {
|
226 |
|
227 | const key = getKeyValue(entity, keyName);
|
228 |
|
229 | const original = lookup.get(key);
|
230 | const reduced = reducer(original, entity);
|
231 | lookup.set(key, reduced);
|
232 | }
|
233 | return lookup;
|
234 | }
|
235 | exports.buildLookupMap = buildLookupMap;
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | function getKeyValue(model, keyName) {
|
243 | return normalizeKey(model[keyName]);
|
244 | }
|
245 | exports.getKeyValue = getKeyValue;
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | function normalizeKey(rawKey) {
|
253 | if (isBsonType(rawKey)) {
|
254 | return rawKey.toString();
|
255 | }
|
256 | return rawKey;
|
257 | }
|
258 | exports.normalizeKey = normalizeKey;
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | function reduceAsArray(acc, it) {
|
266 | if (acc)
|
267 | acc.push(it);
|
268 | else
|
269 | acc = [it];
|
270 | return acc;
|
271 | }
|
272 | exports.reduceAsArray = reduceAsArray;
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | function reduceAsSingleItem(_acc, it) {
|
280 | return it;
|
281 | }
|
282 | exports.reduceAsSingleItem = reduceAsSingleItem;
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | function deduplicate(input) {
|
289 | const uniqArray = [];
|
290 | if (!input) {
|
291 | return uniqArray;
|
292 | }
|
293 | (0, assert_1.default)(Array.isArray(input), 'array argument is required');
|
294 | const comparableArray = input.map(item => normalizeKey(item));
|
295 | for (let i = 0, n = comparableArray.length; i < n; i++) {
|
296 | if (comparableArray.indexOf(comparableArray[i]) === i) {
|
297 | uniqArray.push(input[i]);
|
298 | }
|
299 | }
|
300 | return uniqArray;
|
301 | }
|
302 | exports.deduplicate = deduplicate;
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | function isBsonType(value) {
|
312 | if (typeof value !== 'object' || !value)
|
313 | return false;
|
314 |
|
315 | return check(value) || check(value.constructor.prototype);
|
316 | function check(target) {
|
317 | return Object.prototype.hasOwnProperty.call(target, '_bsontype');
|
318 | }
|
319 | }
|
320 | exports.isBsonType = isBsonType;
|
321 |
|
\ | No newline at end of file |