UNPKG

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