UNPKG

7.32 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.DefaultHasOneRepository = void 0;
8const errors_1 = require("../../errors");
9const repositories_1 = require("../../repositories");
10class DefaultHasOneRepository {
11 /**
12 * Constructor of DefaultHasOneEntityCrudRepository
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 create(targetModelData, options) {
37 let polymorphicTypeName = options === null || options === void 0 ? void 0 : options.polymorphicType;
38 if (polymorphicTypeName) {
39 if (!this.getTargetRepositoryDict[polymorphicTypeName]) {
40 throw new errors_1.InvalidPolymorphismError(polymorphicTypeName);
41 }
42 }
43 else {
44 if (Object.keys(this.getTargetRepositoryDict).length > 1) {
45 console.warn('It is highly recommended to specify the polymorphicType param when using polymorphic types.');
46 }
47 polymorphicTypeName = this.targetResolver().name;
48 if (!this.getTargetRepositoryDict[polymorphicTypeName]) {
49 throw new errors_1.InvalidPolymorphismError(polymorphicTypeName);
50 }
51 }
52 const targetRepository = await this.getTargetRepositoryDict[polymorphicTypeName]();
53 return targetRepository.create((0, repositories_1.constrainDataObject)(targetModelData, this.constraint), options);
54 }
55 async get(filter, options) {
56 let polymorphicTypes = options === null || options === void 0 ? void 0 : options.polymorphicType;
57 let allKeys;
58 if (Object.keys(this.getTargetRepositoryDict).length <= 1) {
59 allKeys = Object.keys(this.getTargetRepositoryDict);
60 }
61 else if (!polymorphicTypes || polymorphicTypes.length === 0) {
62 console.warn('It is highly recommended to specify the polymorphicType param when using polymorphic types.');
63 allKeys = Object.keys(this.getTargetRepositoryDict);
64 }
65 else {
66 if (typeof polymorphicTypes === 'string') {
67 polymorphicTypes = [polymorphicTypes];
68 }
69 allKeys = [];
70 new Set(polymorphicTypes).forEach(element => {
71 if (Object.keys(this.getTargetRepositoryDict).includes(element)) {
72 allKeys.push(element);
73 }
74 });
75 }
76 for (const key of allKeys) {
77 const targetRepository = await this.getTargetRepositoryDict[key]();
78 const found = await targetRepository.find(Object.assign({ limit: 1 }, (0, repositories_1.constrainFilter)(filter, this.constraint)), { ...options, polymorphicType: key });
79 if (found.length >= 1) {
80 return found[0];
81 }
82 }
83 // We don't have a direct access to the foreign key value here :(
84 const id = 'constraint ' + JSON.stringify(this.constraint);
85 throw new errors_1.EntityNotFoundError(this.targetResolver().name, id);
86 }
87 async delete(options) {
88 var _a, _b;
89 let polymorphicTypes = options === null || options === void 0 ? void 0 : options.polymorphicType;
90 let allKeys;
91 if (Object.keys(this.getTargetRepositoryDict).length <= 1) {
92 allKeys = Object.keys(this.getTargetRepositoryDict);
93 }
94 else if (!polymorphicTypes || polymorphicTypes.length === 0) {
95 console.warn('It is highly recommended to specify the polymorphicType param when using polymorphic types.');
96 allKeys = Object.keys(this.getTargetRepositoryDict);
97 }
98 else {
99 if (typeof polymorphicTypes === 'string') {
100 polymorphicTypes = [polymorphicTypes];
101 }
102 allKeys = [];
103 new Set(polymorphicTypes).forEach(element => {
104 if (Object.keys(this.getTargetRepositoryDict).includes(element)) {
105 allKeys.push(element);
106 }
107 });
108 }
109 let total = 0;
110 for (const key of allKeys) {
111 const targetRepository = await this.getTargetRepositoryDict[key]();
112 total +=
113 (_b = (_a = (await targetRepository.deleteAll((0, repositories_1.constrainWhere)({}, this.constraint), options))) === null || _a === void 0 ? void 0 : _a.count) !== null && _b !== void 0 ? _b : 0;
114 }
115 return { count: total };
116 }
117 async patch(dataObject, options) {
118 var _a, _b;
119 const isMultipleTypes = options === null || options === void 0 ? void 0 : options.isPolymorphic;
120 let allKeys;
121 if (!isMultipleTypes) {
122 if (Object.keys(this.getTargetRepositoryDict).length > 1) {
123 console.warn('It is highly recommended to specify the isPolymorphic param and pass in a dictionary of dataobjects when using polymorphic types.');
124 }
125 allKeys = Object.keys(this.getTargetRepositoryDict);
126 }
127 else {
128 allKeys = [];
129 new Set(Object.keys(dataObject)).forEach(element => {
130 if (Object.keys(this.getTargetRepositoryDict).includes(element)) {
131 allKeys.push(element);
132 }
133 });
134 }
135 let total = 0;
136 for (const key of allKeys) {
137 const targetRepository = await this.getTargetRepositoryDict[key]();
138 total +=
139 (_b = (_a = (await targetRepository.updateAll((0, repositories_1.constrainDataObject)(isMultipleTypes
140 ? dataObject[key]
141 : dataObject, this.constraint), (0, repositories_1.constrainWhere)({}, this.constraint), options))) === null || _a === void 0 ? void 0 : _a.count) !== null && _b !== void 0 ? _b : 0;
142 }
143 return { count: total };
144 }
145}
146exports.DefaultHasOneRepository = DefaultHasOneRepository;
147//# sourceMappingURL=has-one.repository.js.map
\No newline at end of file