UNPKG

2.22 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/repository
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.createHasManyRepositoryFactory = void 0;
8const tslib_1 = require("tslib");
9const debug_1 = tslib_1.__importDefault(require("debug"));
10const has_many_helpers_1 = require("./has-many.helpers");
11const has_many_inclusion_resolver_1 = require("./has-many.inclusion-resolver");
12const has_many_repository_1 = require("./has-many.repository");
13const debug = (0, debug_1.default)('loopback:repository:relations:has-many:repository-factory');
14/**
15 * Enforces a constraint on a repository based on a relationship contract
16 * between models. For example, if a Customer model is related to an Order model
17 * via a HasMany relation, then, the relational repository returned by the
18 * factory function would be constrained by a Customer model instance's id(s).
19 *
20 * @param relationMetadata - The relation metadata used to describe the
21 * relationship and determine how to apply the constraint.
22 * @param targetRepositoryGetter - The repository which represents the target model of a
23 * relation attached to a datasource.
24 * @returns The factory function which accepts a foreign key value to constrain
25 * the given target repository
26 */
27function createHasManyRepositoryFactory(relationMetadata, targetRepositoryGetter) {
28 const meta = (0, has_many_helpers_1.resolveHasManyMetadata)(relationMetadata);
29 debug('Resolved HasMany relation metadata: %o', meta);
30 const result = function (fkValue) {
31 // eslint-disable-next-line @typescript-eslint/no-explicit-any
32 const constraint = { [meta.keyTo]: fkValue };
33 return new has_many_repository_1.DefaultHasManyRepository(targetRepositoryGetter, constraint);
34 };
35 result.inclusionResolver = (0, has_many_inclusion_resolver_1.createHasManyInclusionResolver)(meta, targetRepositoryGetter);
36 return result;
37}
38exports.createHasManyRepositoryFactory = createHasManyRepositoryFactory;
39//# sourceMappingURL=has-many.repository-factory.js.map
\No newline at end of file