1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.createReferencesManyAccessor = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
10 | const references_many_helpers_1 = require("./references-many.helpers");
|
11 | const references_many_inclusion_resolver_1 = require("./references-many.inclusion-resolver");
|
12 | const references_many_repository_1 = require("./references-many.repository");
|
13 | const debug = (0, debug_1.default)('loopback:repository:relations:references-many:accessor');
|
14 |
|
15 |
|
16 |
|
17 | function createReferencesManyAccessor(referencesManyMetadata, targetRepoGetter, sourceRepository) {
|
18 | const meta = (0, references_many_helpers_1.resolveReferencesManyMetadata)(referencesManyMetadata);
|
19 | debug('Resolved ReferencesMany relation metadata: %o', meta);
|
20 | const result = async function getTargetInstancesOfReferencesMany(sourceId) {
|
21 | const foreignKey = meta.keyFrom;
|
22 | const primaryKey = meta.keyTo;
|
23 | const sourceModel = await sourceRepository.findById(sourceId);
|
24 | const foreignKeyValue = sourceModel[foreignKey];
|
25 |
|
26 |
|
27 |
|
28 | if (!foreignKeyValue) {
|
29 | return undefined;
|
30 | }
|
31 |
|
32 | const constraint = { [primaryKey]: foreignKeyValue };
|
33 | const constrainedRepo = new references_many_repository_1.DefaultReferencesManyRepository(targetRepoGetter, constraint);
|
34 | return constrainedRepo.get();
|
35 | };
|
36 | result.inclusionResolver = (0, references_many_inclusion_resolver_1.createReferencesManyInclusionResolver)(meta, targetRepoGetter);
|
37 | return result;
|
38 | }
|
39 | exports.createReferencesManyAccessor = createReferencesManyAccessor;
|
40 |
|
\ | No newline at end of file |