UNPKG

3.48 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.DefaultBelongsToRepository = void 0;
8const __1 = require("../../");
9const repositories_1 = require("../../repositories");
10class DefaultBelongsToRepository {
11 /**
12 * Constructor of DefaultBelongsToEntityCrudRepository
13 * @param getTargetRepository - either a dictionary of target model type - target repository instance
14 * or a single target repository instance
15 * e.g. if the target is of a non-polymorphic type "Student", put the studentRepositoryGetterInstance
16 * if the target is of a polymorphic type "Person" which can be either a "Student" or a "Teacher"
17 * then put "{Student: studentRepositoryGetterInstance, Teacher: teacherRepositoryGetterInstance}"
18 * @param constraint - the key value pair representing foreign key name to constrain
19 * the target repository instance
20 * @param targetResolver - () => Target to resolve the target class
21 * e.g. if the target is of type "Student", then put "() => Student"
22 */
23 constructor(getTargetRepository, constraint, targetResolver) {
24 this.getTargetRepository = getTargetRepository;
25 this.constraint = constraint;
26 this.targetResolver = targetResolver;
27 if (typeof getTargetRepository === 'function') {
28 this.getTargetRepositoryDict = {
29 [targetResolver().name]: getTargetRepository,
30 };
31 }
32 else {
33 this.getTargetRepositoryDict = getTargetRepository;
34 }
35 }
36 async get(options) {
37 let polymorphicTypes = options === null || options === void 0 ? void 0 : options.polymorphicType;
38 let allKeys;
39 if (Object.keys(this.getTargetRepositoryDict).length <= 1) {
40 allKeys = Object.keys(this.getTargetRepositoryDict);
41 }
42 else if (!polymorphicTypes || polymorphicTypes.length === 0) {
43 console.warn('It is highly recommended to specify the polymorphicTypes param when using polymorphic types.');
44 allKeys = Object.keys(this.getTargetRepositoryDict);
45 }
46 else {
47 if (typeof polymorphicTypes === 'string') {
48 polymorphicTypes = [polymorphicTypes];
49 }
50 allKeys = [];
51 new Set(polymorphicTypes).forEach(element => {
52 if (Object.keys(this.getTargetRepositoryDict).includes(element)) {
53 allKeys.push(element);
54 }
55 });
56 }
57 let result = [];
58 for (const key of allKeys) {
59 const targetRepository = await this.getTargetRepositoryDict[key]();
60 result = result.concat(await targetRepository.find((0, repositories_1.constrainFilter)(undefined, this.constraint), { ...options, polymorphicType: key }));
61 if (result.length >= 1) {
62 return result[0];
63 }
64 }
65 // We don't have a direct access to the foreign key value here :(
66 const id = 'constraint ' + JSON.stringify(this.constraint);
67 throw new __1.EntityNotFoundError(this.targetResolver().name, id);
68 }
69}
70exports.DefaultBelongsToRepository = DefaultBelongsToRepository;
71//# sourceMappingURL=belongs-to.repository.js.map
\No newline at end of file