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