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 | entities = (0, lodash_1.cloneDeep)(entities);
|
95 | if (options === null || options === void 0 ? void 0 : options.polymorphicType) {
|
96 | include = include === null || include === void 0 ? void 0 : include.filter(inclusionFilter => {
|
97 | if (typeof inclusionFilter === 'string') {
|
98 | return true;
|
99 | }
|
100 | else {
|
101 | if (inclusionFilter.targetType === undefined ||
|
102 | inclusionFilter.targetType === (options === null || options === void 0 ? void 0 : options.polymorphicType)) {
|
103 | return true;
|
104 | }
|
105 | }
|
106 | });
|
107 | }
|
108 | else {
|
109 | include = (0, lodash_1.cloneDeep)(include);
|
110 | }
|
111 | const result = entities;
|
112 | if (!include)
|
113 | return result;
|
114 | const invalidInclusions = include.filter(inclusionFilter => !isInclusionAllowed(targetRepository, inclusionFilter));
|
115 | if (invalidInclusions.length) {
|
116 | const msg = 'Invalid "filter.include" entries: ' +
|
117 | invalidInclusions
|
118 | .map(inclusionFilter => JSON.stringify(inclusionFilter))
|
119 | .join('; ');
|
120 | const err = new Error(msg);
|
121 | Object.assign(err, {
|
122 | code: 'INVALID_INCLUSION_FILTER',
|
123 | statusCode: 400,
|
124 | });
|
125 | throw err;
|
126 | }
|
127 | const resolveTasks = include.map(async (inclusionFilter) => {
|
128 | const relationName = typeof inclusionFilter === 'string'
|
129 | ? inclusionFilter
|
130 | : inclusionFilter.relation;
|
131 | const resolver = targetRepository.inclusionResolvers.get(relationName);
|
132 | const targets = await resolver(entities, inclusionFilter, options);
|
133 | result.forEach((entity, ix) => {
|
134 | const src = entity;
|
135 | src[relationName] = targets[ix];
|
136 | });
|
137 | });
|
138 | await Promise.all(resolveTasks);
|
139 | return result;
|
140 | }
|
141 | exports.includeRelatedModels = includeRelatedModels;
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | function isInclusionAllowed(targetRepository, include) {
|
150 | const relationName = typeof include === 'string' ? include : include.relation;
|
151 | if (!relationName) {
|
152 | debug('isInclusionAllowed for %j? No: missing relation name', include);
|
153 | return false;
|
154 | }
|
155 | const allowed = targetRepository.inclusionResolvers.has(relationName);
|
156 | debug('isInclusionAllowed for %j (relation %s)? %s', include, allowed);
|
157 | return allowed;
|
158 | }
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function flattenTargetsOfOneToOneRelation(sourceIds, targetEntities, targetKey) {
|
169 | const lookup = buildLookupMap(targetEntities, targetKey, reduceAsSingleItem);
|
170 | return flattenMapByKeys(sourceIds, lookup);
|
171 | }
|
172 | exports.flattenTargetsOfOneToOneRelation = flattenTargetsOfOneToOneRelation;
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | function flattenTargetsOfOneToManyRelation(sourceIds, targetEntities, targetKey) {
|
184 | debug('flattenTargetsOfOneToManyRelation');
|
185 | debug('sourceIds', sourceIds);
|
186 | debug('sourceId types', sourceIds.map(i => typeof i));
|
187 | debug('targetEntities', targetEntities);
|
188 | debug('targetKey', targetKey);
|
189 | const lookup = buildLookupMap(targetEntities, targetKey, reduceAsArray);
|
190 | debug('lookup map', lookup);
|
191 | return flattenMapByKeys(sourceIds, lookup);
|
192 | }
|
193 | exports.flattenTargetsOfOneToManyRelation = flattenTargetsOfOneToManyRelation;
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | function flattenMapByKeys(sourceIds, targetMap) {
|
202 | const result = new Array(sourceIds.length);
|
203 |
|
204 |
|
205 | sourceIds.forEach((id, index) => {
|
206 | const key = normalizeKey(id);
|
207 | const target = targetMap.get(key);
|
208 | result[index] = target;
|
209 | });
|
210 | return result;
|
211 | }
|
212 | exports.flattenMapByKeys = flattenMapByKeys;
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | function buildLookupMap(list, keyName, reducer) {
|
222 | const lookup = new Map();
|
223 | for (const entity of list) {
|
224 |
|
225 | const key = getKeyValue(entity, keyName);
|
226 |
|
227 | const original = lookup.get(key);
|
228 | const reduced = reducer(original, entity);
|
229 | lookup.set(key, reduced);
|
230 | }
|
231 | return lookup;
|
232 | }
|
233 | exports.buildLookupMap = buildLookupMap;
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | function getKeyValue(model, keyName) {
|
241 | return normalizeKey(model[keyName]);
|
242 | }
|
243 | exports.getKeyValue = getKeyValue;
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | function normalizeKey(rawKey) {
|
251 | if (isBsonType(rawKey)) {
|
252 | return rawKey.toString();
|
253 | }
|
254 | return rawKey;
|
255 | }
|
256 | exports.normalizeKey = normalizeKey;
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | function reduceAsArray(acc, it) {
|
264 | if (acc)
|
265 | acc.push(it);
|
266 | else
|
267 | acc = [it];
|
268 | return acc;
|
269 | }
|
270 | exports.reduceAsArray = reduceAsArray;
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | function reduceAsSingleItem(_acc, it) {
|
278 | return it;
|
279 | }
|
280 | exports.reduceAsSingleItem = reduceAsSingleItem;
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | function deduplicate(input) {
|
287 | const uniqArray = [];
|
288 | if (!input) {
|
289 | return uniqArray;
|
290 | }
|
291 | (0, assert_1.default)(Array.isArray(input), 'array argument is required');
|
292 | const comparableArray = input.map(item => normalizeKey(item));
|
293 | for (let i = 0, n = comparableArray.length; i < n; i++) {
|
294 | if (comparableArray.indexOf(comparableArray[i]) === i) {
|
295 | uniqArray.push(input[i]);
|
296 | }
|
297 | }
|
298 | return uniqArray;
|
299 | }
|
300 | exports.deduplicate = deduplicate;
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | function isBsonType(value) {
|
310 | if (typeof value !== 'object' || !value)
|
311 | return false;
|
312 |
|
313 | return check(value) || check(value.constructor.prototype);
|
314 | function check(target) {
|
315 | return Object.prototype.hasOwnProperty.call(target, '_bsontype');
|
316 | }
|
317 | }
|
318 | exports.isBsonType = isBsonType;
|
319 |
|
\ | No newline at end of file |