@opra/sqb 1.21.0 → 1.22.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -28
- package/{esm/sqb-collection-service.js → sqb-collection-service.js} +4 -0
- package/{esm/sqb-entity-service.js → sqb-entity-service.js} +29 -2
- package/{esm/sqb-service-base.js → sqb-service-base.js} +4 -0
- package/{esm/sqb-singleton-service.js → sqb-singleton-service.js} +5 -0
- package/cjs/adapter-utils/prepare-filter.js +0 -108
- package/cjs/augmentation/datatype-factory.augmentation.js +0 -90
- package/cjs/augmentation/mapped-type.augmentation.js +0 -14
- package/cjs/augmentation/mixin-type.augmentation.js +0 -11
- package/cjs/index.js +0 -11
- package/cjs/package.json +0 -3
- package/cjs/sqb-adapter.js +0 -130
- package/cjs/sqb-collection-service.js +0 -355
- package/cjs/sqb-entity-service.js +0 -642
- package/cjs/sqb-service-base.js +0 -88
- package/cjs/sqb-singleton-service.js +0 -172
- package/esm/package.json +0 -3
- package/types/index.d.cts +0 -8
- /package/{types/adapter-utils → adapter-utils}/prepare-filter.d.ts +0 -0
- /package/{esm/adapter-utils → adapter-utils}/prepare-filter.js +0 -0
- /package/{types/augmentation → augmentation}/datatype-factory.augmentation.d.ts +0 -0
- /package/{esm/augmentation → augmentation}/datatype-factory.augmentation.js +0 -0
- /package/{types/augmentation → augmentation}/mapped-type.augmentation.d.ts +0 -0
- /package/{esm/augmentation → augmentation}/mapped-type.augmentation.js +0 -0
- /package/{types/augmentation → augmentation}/mixin-type.augmentation.d.ts +0 -0
- /package/{esm/augmentation → augmentation}/mixin-type.augmentation.js +0 -0
- /package/{types/index.d.ts → index.d.ts} +0 -0
- /package/{esm/index.js → index.js} +0 -0
- /package/{types/sqb-adapter.d.ts → sqb-adapter.d.ts} +0 -0
- /package/{esm/sqb-adapter.js → sqb-adapter.js} +0 -0
- /package/{types/sqb-collection-service.d.ts → sqb-collection-service.d.ts} +0 -0
- /package/{types/sqb-entity-service.d.ts → sqb-entity-service.d.ts} +0 -0
- /package/{types/sqb-service-base.d.ts → sqb-service-base.d.ts} +0 -0
- /package/{types/sqb-singleton-service.d.ts → sqb-singleton-service.d.ts} +0 -0
|
@@ -1,642 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SqbEntityService = void 0;
|
|
4
|
-
const common_1 = require("@opra/common");
|
|
5
|
-
const builder_1 = require("@sqb/builder");
|
|
6
|
-
const connect_1 = require("@sqb/connect");
|
|
7
|
-
const valgen_1 = require("valgen");
|
|
8
|
-
const sqb_adapter_js_1 = require("./sqb-adapter.js");
|
|
9
|
-
const sqb_service_base_js_1 = require("./sqb-service-base.js");
|
|
10
|
-
/**
|
|
11
|
-
* @class SqbEntityService
|
|
12
|
-
* @template T - The data type class type of the resource
|
|
13
|
-
*/
|
|
14
|
-
class SqbEntityService extends sqb_service_base_js_1.SqbServiceBase {
|
|
15
|
-
/**
|
|
16
|
-
* Constructs a new instance
|
|
17
|
-
*
|
|
18
|
-
* @param dataType - The data type of the returning results
|
|
19
|
-
* @param [options] - The options for the service.
|
|
20
|
-
* @constructor
|
|
21
|
-
*/
|
|
22
|
-
constructor(dataType, options) {
|
|
23
|
-
super(options);
|
|
24
|
-
this._inputCodecs = {};
|
|
25
|
-
this._outputCodecs = {};
|
|
26
|
-
this._dataType_ = dataType;
|
|
27
|
-
this.resourceName = options?.resourceName;
|
|
28
|
-
this.commonFilter = options?.commonFilter;
|
|
29
|
-
this.interceptor = options?.interceptor;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Retrieves the OPRA data type
|
|
33
|
-
*
|
|
34
|
-
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
35
|
-
*/
|
|
36
|
-
get dataType() {
|
|
37
|
-
if (this._dataType && this._dataTypeScope !== this.scope)
|
|
38
|
-
this._dataType = undefined;
|
|
39
|
-
if (!this._dataType)
|
|
40
|
-
this._dataType = this.context.__docNode.getComplexType(this._dataType_);
|
|
41
|
-
this._dataTypeScope = this.scope;
|
|
42
|
-
return this._dataType;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Retrieves the Class of the data type
|
|
46
|
-
*
|
|
47
|
-
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
48
|
-
*/
|
|
49
|
-
get dataTypeClass() {
|
|
50
|
-
if (!this._dataTypeClass)
|
|
51
|
-
this._dataTypeClass = this.entityMetadata.ctor;
|
|
52
|
-
return this._dataTypeClass;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Retrieves the SQB entity metadata object
|
|
56
|
-
*
|
|
57
|
-
* @throws {TypeError} If metadata is not available
|
|
58
|
-
*/
|
|
59
|
-
get entityMetadata() {
|
|
60
|
-
if (!this._entityMetadata) {
|
|
61
|
-
const t = this.dataType.ctor;
|
|
62
|
-
const metadata = connect_1.EntityMetadata.get(t);
|
|
63
|
-
if (!metadata)
|
|
64
|
-
throw new TypeError(`Class (${t}) is not decorated with $Entity() decorator`);
|
|
65
|
-
this._entityMetadata = metadata;
|
|
66
|
-
}
|
|
67
|
-
return this._entityMetadata;
|
|
68
|
-
}
|
|
69
|
-
for(context, overwriteProperties, overwriteContext) {
|
|
70
|
-
if (overwriteProperties?.commonFilter && this.commonFilter) {
|
|
71
|
-
overwriteProperties.commonFilter = [
|
|
72
|
-
...(Array.isArray(this.commonFilter)
|
|
73
|
-
? this.commonFilter
|
|
74
|
-
: [this.commonFilter]),
|
|
75
|
-
...(Array.isArray(overwriteProperties?.commonFilter)
|
|
76
|
-
? overwriteProperties.commonFilter
|
|
77
|
-
: [overwriteProperties.commonFilter]),
|
|
78
|
-
];
|
|
79
|
-
}
|
|
80
|
-
return super.for(context, overwriteProperties, overwriteContext);
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Retrieves the resource name.
|
|
84
|
-
*
|
|
85
|
-
* @returns {string} The resource name.
|
|
86
|
-
* @throws {Error} If the collection name is not defined.
|
|
87
|
-
*/
|
|
88
|
-
getResourceName() {
|
|
89
|
-
const out = typeof this.resourceName === 'function'
|
|
90
|
-
? this.resourceName(this)
|
|
91
|
-
: this.resourceName || this.dataType.name;
|
|
92
|
-
if (out)
|
|
93
|
-
return out;
|
|
94
|
-
throw new Error('resourceName is not defined');
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Retrieves the codec for the specified operation.
|
|
98
|
-
*
|
|
99
|
-
* @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
|
|
100
|
-
*/
|
|
101
|
-
getInputCodec(operation) {
|
|
102
|
-
const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
|
|
103
|
-
let validator = this._inputCodecs[cacheKey];
|
|
104
|
-
if (validator)
|
|
105
|
-
return validator;
|
|
106
|
-
const options = {
|
|
107
|
-
projection: '*',
|
|
108
|
-
scope: this._dataTypeScope,
|
|
109
|
-
};
|
|
110
|
-
if (operation === 'update')
|
|
111
|
-
options.partial = 'deep';
|
|
112
|
-
const dataType = this.dataType;
|
|
113
|
-
validator = dataType.generateCodec('decode', options);
|
|
114
|
-
this._inputCodecs[cacheKey] = validator;
|
|
115
|
-
return validator;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Retrieves the codec.
|
|
119
|
-
*/
|
|
120
|
-
getOutputCodec(operation) {
|
|
121
|
-
const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
|
|
122
|
-
let validator = this._outputCodecs[cacheKey];
|
|
123
|
-
if (validator)
|
|
124
|
-
return validator;
|
|
125
|
-
const options = {
|
|
126
|
-
projection: '*',
|
|
127
|
-
partial: 'deep',
|
|
128
|
-
scope: this._dataTypeScope,
|
|
129
|
-
};
|
|
130
|
-
const dataType = this.dataType;
|
|
131
|
-
validator = dataType.generateCodec('decode', options);
|
|
132
|
-
this._outputCodecs[cacheKey] = validator;
|
|
133
|
-
return validator;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Insert a new record into database
|
|
137
|
-
*
|
|
138
|
-
* @param command
|
|
139
|
-
* @returns - A promise that resolves to the created resource
|
|
140
|
-
* @protected
|
|
141
|
-
*/
|
|
142
|
-
async _create(command) {
|
|
143
|
-
const { input, options } = command;
|
|
144
|
-
(0, valgen_1.isNotNullish)(command.input, { label: 'input' });
|
|
145
|
-
const inputCodec = this.getInputCodec('create');
|
|
146
|
-
const outputCodec = this.getOutputCodec('create');
|
|
147
|
-
const data = inputCodec(input);
|
|
148
|
-
const conn = await this.getConnection();
|
|
149
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
150
|
-
const out = await repo.create(data, options);
|
|
151
|
-
if (out)
|
|
152
|
-
return outputCodec(out);
|
|
153
|
-
throw new common_1.InternalServerError(`Unknown error while creating document for "${this.getResourceName()}"`);
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Insert a new record into database
|
|
157
|
-
*
|
|
158
|
-
* @param command
|
|
159
|
-
* @returns - A promise that resolves to the created resource
|
|
160
|
-
* @protected
|
|
161
|
-
*/
|
|
162
|
-
async _createOnly(command) {
|
|
163
|
-
const { input, options } = command;
|
|
164
|
-
(0, valgen_1.isNotNullish)(command.input, { label: 'input' });
|
|
165
|
-
const inputCodec = this.getInputCodec('create');
|
|
166
|
-
const data = inputCodec(input);
|
|
167
|
-
const conn = await this.getConnection();
|
|
168
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
169
|
-
return await repo.createOnly(data, options);
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Returns the count of records based on the provided options
|
|
173
|
-
*
|
|
174
|
-
* @param command
|
|
175
|
-
* @return - A promise that resolves to the count of records
|
|
176
|
-
* @protected
|
|
177
|
-
*/
|
|
178
|
-
async _count(command) {
|
|
179
|
-
const filter = command.options?.filter
|
|
180
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
181
|
-
: undefined;
|
|
182
|
-
return this._dbCount({ ...command.options, filter });
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Deletes a record from the collection.
|
|
186
|
-
*
|
|
187
|
-
* @param command
|
|
188
|
-
* @return - A Promise that resolves to the number of documents deleted.
|
|
189
|
-
* @protected
|
|
190
|
-
*/
|
|
191
|
-
async _delete(command) {
|
|
192
|
-
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
193
|
-
const filter = command.options?.filter
|
|
194
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
195
|
-
: undefined;
|
|
196
|
-
return this._dbDelete(command.documentId, { ...command.options, filter });
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
200
|
-
*
|
|
201
|
-
* @param command
|
|
202
|
-
* @return - A promise that resolves to the number of documents deleted.
|
|
203
|
-
* @protected
|
|
204
|
-
*/
|
|
205
|
-
async _deleteMany(command) {
|
|
206
|
-
const filter = command.options?.filter
|
|
207
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
208
|
-
: undefined;
|
|
209
|
-
return await this._dbDeleteMany({ ...command.options, filter });
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Checks if a record with the given id exists.
|
|
213
|
-
*
|
|
214
|
-
* @param command
|
|
215
|
-
* @protected
|
|
216
|
-
*/
|
|
217
|
-
async _exists(command) {
|
|
218
|
-
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
219
|
-
const filter = command.options?.filter
|
|
220
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
221
|
-
: undefined;
|
|
222
|
-
return await this._dbExists(command.documentId, {
|
|
223
|
-
...command.options,
|
|
224
|
-
filter,
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Checks if a record with the given arguments exists.
|
|
229
|
-
*
|
|
230
|
-
* @param command
|
|
231
|
-
* @return - A Promise that resolves to a boolean indicating whether the record exists or not.
|
|
232
|
-
* @protected
|
|
233
|
-
*/
|
|
234
|
-
async _existsOne(command) {
|
|
235
|
-
const filter = command.options?.filter
|
|
236
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
237
|
-
: undefined;
|
|
238
|
-
return await this._dbExistsOne({ ...command.options, filter });
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Finds a record by ID.
|
|
242
|
-
*
|
|
243
|
-
* @param command
|
|
244
|
-
* @return - A promise resolving to the found document, or undefined if not found.
|
|
245
|
-
* @protected
|
|
246
|
-
*/
|
|
247
|
-
async _findById(command) {
|
|
248
|
-
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
249
|
-
const decode = this.getOutputCodec('find');
|
|
250
|
-
const filter = command.options?.filter
|
|
251
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
252
|
-
: undefined;
|
|
253
|
-
const out = await this._dbFindById(command.documentId, {
|
|
254
|
-
...command.options,
|
|
255
|
-
filter,
|
|
256
|
-
});
|
|
257
|
-
return out ? decode(out) : undefined;
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Finds a record in the collection that matches the specified options.
|
|
261
|
-
*
|
|
262
|
-
* @param command
|
|
263
|
-
* @return - A promise that resolves with the found document or undefined if no document is found.
|
|
264
|
-
* @protected
|
|
265
|
-
*/
|
|
266
|
-
async _findOne(command) {
|
|
267
|
-
const decode = this.getOutputCodec('find');
|
|
268
|
-
const filter = command.options?.filter
|
|
269
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
270
|
-
: undefined;
|
|
271
|
-
const out = await this._dbFindOne({ ...command.options, filter });
|
|
272
|
-
return out ? decode(out) : undefined;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Finds multiple records in collection.
|
|
276
|
-
*
|
|
277
|
-
* @param command
|
|
278
|
-
* @return - A Promise that resolves to an array of partial outputs of type T.
|
|
279
|
-
* @protected
|
|
280
|
-
*/
|
|
281
|
-
async _findMany(command) {
|
|
282
|
-
const decode = this.getOutputCodec('find');
|
|
283
|
-
const filter = command.options?.filter
|
|
284
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
285
|
-
: undefined;
|
|
286
|
-
const out = await this._dbFindMany({ ...command.options, filter });
|
|
287
|
-
if (out?.length) {
|
|
288
|
-
return out.map(x => decode(x));
|
|
289
|
-
}
|
|
290
|
-
return out;
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* Updates a record with the given id in the collection.
|
|
294
|
-
*
|
|
295
|
-
* @param command
|
|
296
|
-
* @returns A promise that resolves to the updated document or undefined if the document was not found.
|
|
297
|
-
* @protected
|
|
298
|
-
*/
|
|
299
|
-
async _update(command) {
|
|
300
|
-
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
301
|
-
(0, valgen_1.isNotNullish)(command.input, { label: 'input' });
|
|
302
|
-
const { documentId, input, options } = command;
|
|
303
|
-
const inputCodec = this.getInputCodec('update');
|
|
304
|
-
const data = inputCodec(input);
|
|
305
|
-
const filter = command.options?.filter
|
|
306
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
307
|
-
: undefined;
|
|
308
|
-
const out = await this._dbUpdate(documentId, data, { ...options, filter });
|
|
309
|
-
const outputCodec = this.getOutputCodec('update');
|
|
310
|
-
if (out)
|
|
311
|
-
return outputCodec(out);
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Updates a record in the collection with the specified ID and returns updated record count
|
|
315
|
-
*
|
|
316
|
-
* @param command
|
|
317
|
-
* @returns - A promise that resolves to the number of documents modified.
|
|
318
|
-
* @protected
|
|
319
|
-
*/
|
|
320
|
-
async _updateOnly(command) {
|
|
321
|
-
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
322
|
-
(0, valgen_1.isNotNullish)(command.input, { label: 'input' });
|
|
323
|
-
const { documentId, input, options } = command;
|
|
324
|
-
const inputCodec = this.getInputCodec('update');
|
|
325
|
-
const data = inputCodec(input);
|
|
326
|
-
const filter = command.options?.filter
|
|
327
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
328
|
-
: undefined;
|
|
329
|
-
return await this._dbUpdateOnly(documentId, data, { ...options, filter });
|
|
330
|
-
}
|
|
331
|
-
/**
|
|
332
|
-
* Updates multiple records in the collection based on the specified input and options.
|
|
333
|
-
*
|
|
334
|
-
* @param command
|
|
335
|
-
* @return - A promise that resolves to the number of documents matched and modified.
|
|
336
|
-
* @protected
|
|
337
|
-
*/
|
|
338
|
-
async _updateMany(command) {
|
|
339
|
-
(0, valgen_1.isNotNullish)(command.input, { label: 'input' });
|
|
340
|
-
const inputCodec = this.getInputCodec('update');
|
|
341
|
-
const data = inputCodec(command.input);
|
|
342
|
-
const filter = command.options?.filter
|
|
343
|
-
? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter)
|
|
344
|
-
: undefined;
|
|
345
|
-
return await this._dbUpdateMany(data, { ...command.options, filter });
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* Acquires a connection and performs Repository.create operation
|
|
349
|
-
*
|
|
350
|
-
* @param input - The document to insert
|
|
351
|
-
* @param options - Optional settings for the command
|
|
352
|
-
* @protected
|
|
353
|
-
*/
|
|
354
|
-
async _dbCreate(input, options) {
|
|
355
|
-
const conn = await this.getConnection();
|
|
356
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
357
|
-
return await repo.create(input, options);
|
|
358
|
-
}
|
|
359
|
-
/**
|
|
360
|
-
* Acquires a connection and performs Repository.count operation
|
|
361
|
-
*
|
|
362
|
-
* @param options - The options for counting documents.
|
|
363
|
-
* @protected
|
|
364
|
-
*/
|
|
365
|
-
async _dbCount(options) {
|
|
366
|
-
const conn = await this.getConnection();
|
|
367
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
368
|
-
if (options?.filter)
|
|
369
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
370
|
-
return await repo.count(options);
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Acquires a connection and performs Repository.delete operation
|
|
374
|
-
*
|
|
375
|
-
* @param id - Value of the key field used to select the record
|
|
376
|
-
* @param options - Optional settings for the command
|
|
377
|
-
* @protected
|
|
378
|
-
*/
|
|
379
|
-
async _dbDelete(id, options) {
|
|
380
|
-
const conn = await this.getConnection();
|
|
381
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
382
|
-
if (options?.filter)
|
|
383
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
384
|
-
return (await repo.delete(id, options)) ? 1 : 0;
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Acquires a connection and performs Repository.deleteMany operation
|
|
388
|
-
*
|
|
389
|
-
* @param options - Optional settings for the command
|
|
390
|
-
* @protected
|
|
391
|
-
*/
|
|
392
|
-
async _dbDeleteMany(options) {
|
|
393
|
-
const conn = await this.getConnection();
|
|
394
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
395
|
-
if (options?.filter)
|
|
396
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
397
|
-
return await repo.deleteMany(options);
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Acquires a connection and performs Repository.exists operation
|
|
401
|
-
*
|
|
402
|
-
* @param id - Value of the key field used to select the record
|
|
403
|
-
* @param options - Optional settings for the command
|
|
404
|
-
* @protected
|
|
405
|
-
*/
|
|
406
|
-
async _dbExists(id, options) {
|
|
407
|
-
const conn = await this.getConnection();
|
|
408
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
409
|
-
if (options?.filter)
|
|
410
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
411
|
-
return await repo.exists(id, options);
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* Acquires a connection and performs Repository.existsOne operation
|
|
415
|
-
*
|
|
416
|
-
* @param options - Optional settings for the command
|
|
417
|
-
* @protected
|
|
418
|
-
*/
|
|
419
|
-
async _dbExistsOne(options) {
|
|
420
|
-
const conn = await this.getConnection();
|
|
421
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
422
|
-
if (options?.filter)
|
|
423
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
424
|
-
return await repo.existsOne(options);
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* Acquires a connection and performs Repository.findById operation
|
|
428
|
-
*
|
|
429
|
-
* @param id - Value of the key field used to select the record
|
|
430
|
-
* @param options - Optional settings for the command
|
|
431
|
-
* @protected
|
|
432
|
-
*/
|
|
433
|
-
async _dbFindById(id, options) {
|
|
434
|
-
const conn = await this.getConnection();
|
|
435
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
436
|
-
if (options?.filter)
|
|
437
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
438
|
-
return await repo.findById(id, options);
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* Acquires a connection and performs Repository.findOne operation
|
|
442
|
-
*
|
|
443
|
-
* @param options - Optional settings for the command
|
|
444
|
-
* @protected
|
|
445
|
-
*/
|
|
446
|
-
async _dbFindOne(options) {
|
|
447
|
-
const conn = await this.getConnection();
|
|
448
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
449
|
-
if (options?.filter)
|
|
450
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
451
|
-
return await repo.findOne({ ...options, offset: options?.skip });
|
|
452
|
-
}
|
|
453
|
-
/**
|
|
454
|
-
* Acquires a connection and performs Repository.findMany operation
|
|
455
|
-
*
|
|
456
|
-
* @param options - Optional settings for the command
|
|
457
|
-
* @protected
|
|
458
|
-
*/
|
|
459
|
-
async _dbFindMany(options) {
|
|
460
|
-
const conn = await this.getConnection();
|
|
461
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
462
|
-
if (options?.filter)
|
|
463
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
464
|
-
return await repo.findMany({ ...options, offset: options?.skip });
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* Acquires a connection and performs Repository.update operation
|
|
468
|
-
*
|
|
469
|
-
* @param id - Value of the key field used to select the record
|
|
470
|
-
* @param data - The update values to be applied to the document
|
|
471
|
-
* @param options - Optional settings for the command
|
|
472
|
-
* @protected
|
|
473
|
-
*/
|
|
474
|
-
async _dbUpdate(id, data, options) {
|
|
475
|
-
const conn = await this.getConnection();
|
|
476
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
477
|
-
if (options?.filter)
|
|
478
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
479
|
-
return await repo.update(id, data, options);
|
|
480
|
-
}
|
|
481
|
-
/**
|
|
482
|
-
* Acquires a connection and performs Repository.updateOnly operation
|
|
483
|
-
*
|
|
484
|
-
* @param id - Value of the key field used to select the record
|
|
485
|
-
* @param data - The update values to be applied to the document
|
|
486
|
-
* @param options - Optional settings for the command
|
|
487
|
-
* @protected
|
|
488
|
-
*/
|
|
489
|
-
async _dbUpdateOnly(id, data, options) {
|
|
490
|
-
const conn = await this.getConnection();
|
|
491
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
492
|
-
if (options?.filter)
|
|
493
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
494
|
-
return (await repo.updateOnly(id, data, options)) ? 1 : 0;
|
|
495
|
-
}
|
|
496
|
-
/**
|
|
497
|
-
* Acquires a connection and performs Repository.updateMany operation
|
|
498
|
-
*
|
|
499
|
-
* @param data - The update values to be applied to the document
|
|
500
|
-
* @param options - Optional settings for the command
|
|
501
|
-
* @protected
|
|
502
|
-
*/
|
|
503
|
-
async _dbUpdateMany(data, options) {
|
|
504
|
-
const conn = await this.getConnection();
|
|
505
|
-
const repo = conn.getRepository(this.dataTypeClass);
|
|
506
|
-
if (options?.filter)
|
|
507
|
-
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
508
|
-
return await repo.updateMany(data, options);
|
|
509
|
-
}
|
|
510
|
-
/**
|
|
511
|
-
* Retrieves the common filter used for querying documents.
|
|
512
|
-
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
513
|
-
*
|
|
514
|
-
* @protected
|
|
515
|
-
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
516
|
-
* that resolves to the common filter, or undefined if not available.
|
|
517
|
-
*/
|
|
518
|
-
_getCommonFilter(command) {
|
|
519
|
-
const commonFilter = Array.isArray(this.commonFilter)
|
|
520
|
-
? this.commonFilter
|
|
521
|
-
: [this.commonFilter];
|
|
522
|
-
const mapped = commonFilter.map(f => typeof f === 'function' ? f(command, this) : f);
|
|
523
|
-
return mapped.length > 1 ? builder_1.op.and(...mapped) : mapped[0];
|
|
524
|
-
}
|
|
525
|
-
async _executeCommand(command, commandFn) {
|
|
526
|
-
let proto;
|
|
527
|
-
const next = async () => {
|
|
528
|
-
proto = proto ? Object.getPrototypeOf(proto) : this;
|
|
529
|
-
while (proto) {
|
|
530
|
-
if (proto.interceptor &&
|
|
531
|
-
Object.prototype.hasOwnProperty.call(proto, 'interceptor')) {
|
|
532
|
-
return await proto.interceptor.call(this, next, command, this);
|
|
533
|
-
}
|
|
534
|
-
proto = Object.getPrototypeOf(proto);
|
|
535
|
-
if (!(proto instanceof SqbEntityService))
|
|
536
|
-
break;
|
|
537
|
-
}
|
|
538
|
-
/** Call before[X] hooks */
|
|
539
|
-
if (command.crud === 'create')
|
|
540
|
-
await this._beforeCreate(command);
|
|
541
|
-
else if (command.crud === 'update' && command.byId) {
|
|
542
|
-
await this._beforeUpdate(command);
|
|
543
|
-
}
|
|
544
|
-
else if (command.crud === 'update' && !command.byId) {
|
|
545
|
-
await this._beforeUpdateMany(command);
|
|
546
|
-
}
|
|
547
|
-
else if (command.crud === 'delete' && command.byId) {
|
|
548
|
-
await this._beforeDelete(command);
|
|
549
|
-
}
|
|
550
|
-
else if (command.crud === 'delete' && !command.byId) {
|
|
551
|
-
await this._beforeDeleteMany(command);
|
|
552
|
-
}
|
|
553
|
-
/** Call command function */
|
|
554
|
-
return commandFn();
|
|
555
|
-
};
|
|
556
|
-
try {
|
|
557
|
-
const result = await next();
|
|
558
|
-
/** Call after[X] hooks */
|
|
559
|
-
if (command.crud === 'create')
|
|
560
|
-
await this._afterCreate(command, result);
|
|
561
|
-
else if (command.crud === 'update' && command.byId) {
|
|
562
|
-
await this._afterUpdate(command, result);
|
|
563
|
-
}
|
|
564
|
-
else if (command.crud === 'update' && !command.byId) {
|
|
565
|
-
await this._afterUpdateMany(command, result);
|
|
566
|
-
}
|
|
567
|
-
else if (command.crud === 'delete' && command.byId) {
|
|
568
|
-
await this._afterDelete(command, result);
|
|
569
|
-
}
|
|
570
|
-
else if (command.crud === 'delete' && !command.byId) {
|
|
571
|
-
await this._afterDeleteMany(command, result);
|
|
572
|
-
}
|
|
573
|
-
return result;
|
|
574
|
-
}
|
|
575
|
-
catch (e) {
|
|
576
|
-
Error.captureStackTrace(e, this._executeCommand);
|
|
577
|
-
await this.onError?.(e, this);
|
|
578
|
-
throw e;
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
async _beforeCreate(
|
|
582
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
583
|
-
command) {
|
|
584
|
-
// Do nothing
|
|
585
|
-
}
|
|
586
|
-
async _beforeUpdate(
|
|
587
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
588
|
-
command) {
|
|
589
|
-
// Do nothing
|
|
590
|
-
}
|
|
591
|
-
async _beforeUpdateMany(
|
|
592
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
593
|
-
command) {
|
|
594
|
-
// Do nothing
|
|
595
|
-
}
|
|
596
|
-
async _beforeDelete(
|
|
597
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
598
|
-
command) {
|
|
599
|
-
// Do nothing
|
|
600
|
-
}
|
|
601
|
-
async _beforeDeleteMany(
|
|
602
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
603
|
-
command) {
|
|
604
|
-
// Do nothing
|
|
605
|
-
}
|
|
606
|
-
async _afterCreate(
|
|
607
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
608
|
-
command,
|
|
609
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
610
|
-
result) {
|
|
611
|
-
// Do nothing
|
|
612
|
-
}
|
|
613
|
-
async _afterUpdate(
|
|
614
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
615
|
-
command,
|
|
616
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
617
|
-
result) {
|
|
618
|
-
// Do nothing
|
|
619
|
-
}
|
|
620
|
-
async _afterUpdateMany(
|
|
621
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
622
|
-
command,
|
|
623
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
624
|
-
affected) {
|
|
625
|
-
// Do nothing
|
|
626
|
-
}
|
|
627
|
-
async _afterDelete(
|
|
628
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
629
|
-
command,
|
|
630
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
631
|
-
affected) {
|
|
632
|
-
// Do nothing
|
|
633
|
-
}
|
|
634
|
-
async _afterDeleteMany(
|
|
635
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
636
|
-
command,
|
|
637
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
638
|
-
affected) {
|
|
639
|
-
// Do nothing
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
exports.SqbEntityService = SqbEntityService;
|