1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.createHasManyInclusionResolver = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
10 | const relation_helpers_1 = require("../relation.helpers");
|
11 | const has_many_helpers_1 = require("./has-many.helpers");
|
12 | const debug = (0, debug_1.default)('loopback:repository:relations:has-many:inclusion-resolver');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function createHasManyInclusionResolver(meta, getTargetRepo) {
|
24 | const relationMeta = (0, has_many_helpers_1.resolveHasManyMetadata)(meta);
|
25 | return async function fetchHasManyModels(entities, inclusion, options) {
|
26 | if (!entities.length)
|
27 | return [];
|
28 | debug('Fetching target models for entities:', entities);
|
29 | debug('Relation metadata:', relationMeta);
|
30 | const sourceKey = relationMeta.keyFrom;
|
31 | const sourceIds = entities.map(e => e[sourceKey]);
|
32 | const targetKey = relationMeta.keyTo;
|
33 | debug('Parameters:', { sourceKey, sourceIds, targetKey });
|
34 | debug('sourceId types', sourceIds.map(i => typeof i));
|
35 | const scope = typeof inclusion === 'string' ? {} : inclusion.scope;
|
36 | const targetRepo = await getTargetRepo();
|
37 | const targetsFound = await (0, relation_helpers_1.findByForeignKeys)(targetRepo, targetKey, sourceIds, scope, options);
|
38 | debug('Targets found:', targetsFound);
|
39 | const result = (0, relation_helpers_1.flattenTargetsOfOneToManyRelation)(sourceIds, targetsFound, targetKey);
|
40 | debug('fetchHasManyModels result', result);
|
41 | return result;
|
42 | };
|
43 | }
|
44 | exports.createHasManyInclusionResolver = createHasManyInclusionResolver;
|
45 |
|
\ | No newline at end of file |