@opra/sqb 1.26.2 → 1.26.4
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/README.md +24 -1
- package/adapter-utils/prepare-filter.d.ts +3 -4
- package/adapter-utils/prepare-filter.js +3 -4
- package/augmentation/datatype-factory.augmentation.js +1 -1
- package/package.json +3 -3
- package/sqb-adapter.d.ts +41 -1
- package/sqb-adapter.js +21 -6
- package/sqb-collection-service.d.ts +141 -159
- package/sqb-collection-service.js +52 -99
- package/sqb-entity-service.d.ts +131 -153
- package/sqb-entity-service.js +123 -127
- package/sqb-service-base.d.ts +18 -14
- package/sqb-service-base.js +15 -17
- package/sqb-singleton-service.d.ts +78 -88
- package/sqb-singleton-service.js +26 -26
package/sqb-entity-service.js
CHANGED
|
@@ -5,8 +5,9 @@ import { isNotNullish } from 'valgen';
|
|
|
5
5
|
import { SQBAdapter } from './sqb-adapter.js';
|
|
6
6
|
import { SqbServiceBase } from './sqb-service-base.js';
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Base service providing CRUD operations over an SQB entity.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T - The entity type managed by this service
|
|
10
11
|
*/
|
|
11
12
|
export class SqbEntityService extends SqbServiceBase {
|
|
12
13
|
_dataTypeScope;
|
|
@@ -17,33 +18,31 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
17
18
|
_inputCodecs = {};
|
|
18
19
|
_outputCodecs = {};
|
|
19
20
|
/**
|
|
20
|
-
*
|
|
21
|
+
* Comma-delimited scopes used to filter the API document.
|
|
21
22
|
*/
|
|
22
23
|
scope;
|
|
23
24
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
25
|
+
* Override for the resource name exposed in error messages and API metadata.
|
|
26
|
+
* Accepts a static string or a function that returns one.
|
|
26
27
|
*/
|
|
27
28
|
resourceName;
|
|
28
29
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* @type {SqbEntityService.Filter | Function}
|
|
30
|
+
* Filter(s) automatically applied to every query for this service.
|
|
31
|
+
* Useful for multi-tenant isolation or other cross-cutting constraints.
|
|
32
32
|
*/
|
|
33
33
|
commonFilter;
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
35
|
+
* Called whenever a command throws. Useful for logging or transforming errors.
|
|
36
36
|
*
|
|
37
|
-
* @param
|
|
38
|
-
* @param
|
|
37
|
+
* @param error - The thrown error.
|
|
38
|
+
* @param _this - The service instance.
|
|
39
39
|
*/
|
|
40
40
|
onError;
|
|
41
41
|
/**
|
|
42
|
-
* Constructs a new instance
|
|
42
|
+
* Constructs a new instance.
|
|
43
43
|
*
|
|
44
|
-
* @param dataType - The
|
|
45
|
-
* @param
|
|
46
|
-
* @constructor
|
|
44
|
+
* @param dataType - The entity class or its registered name.
|
|
45
|
+
* @param options - Options for the service.
|
|
47
46
|
*/
|
|
48
47
|
constructor(dataType, options) {
|
|
49
48
|
super(options);
|
|
@@ -53,9 +52,9 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
53
52
|
this.interceptor = options?.interceptor;
|
|
54
53
|
}
|
|
55
54
|
/**
|
|
56
|
-
*
|
|
55
|
+
* Returns the resolved OPRA `ComplexType` for this service's entity.
|
|
57
56
|
*
|
|
58
|
-
* @throws
|
|
57
|
+
* @throws If the data type is not registered as a `ComplexType`.
|
|
59
58
|
*/
|
|
60
59
|
get dataType() {
|
|
61
60
|
if (this._dataType && this._dataTypeScope !== this.scope)
|
|
@@ -66,9 +65,9 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
66
65
|
return this._dataType;
|
|
67
66
|
}
|
|
68
67
|
/**
|
|
69
|
-
*
|
|
68
|
+
* Returns the constructor class of the entity data type.
|
|
70
69
|
*
|
|
71
|
-
* @throws
|
|
70
|
+
* @throws If the data type is not registered as a `ComplexType`.
|
|
72
71
|
*/
|
|
73
72
|
get dataTypeClass() {
|
|
74
73
|
if (!this._dataTypeClass)
|
|
@@ -76,9 +75,9 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
76
75
|
return this._dataTypeClass;
|
|
77
76
|
}
|
|
78
77
|
/**
|
|
79
|
-
*
|
|
78
|
+
* Returns the SQB `EntityMetadata` for the entity class.
|
|
80
79
|
*
|
|
81
|
-
* @throws
|
|
80
|
+
* @throws If the class is not decorated with `@Entity()`.
|
|
82
81
|
*/
|
|
83
82
|
get entityMetadata() {
|
|
84
83
|
if (!this._entityMetadata) {
|
|
@@ -104,10 +103,9 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
104
103
|
return super.for(context, overwriteProperties, overwriteContext);
|
|
105
104
|
}
|
|
106
105
|
/**
|
|
107
|
-
*
|
|
106
|
+
* Returns the resource name used in error messages and API metadata.
|
|
108
107
|
*
|
|
109
|
-
* @
|
|
110
|
-
* @throws {Error} If the collection name is not defined.
|
|
108
|
+
* @throws If neither `resourceName` nor the data type name is available.
|
|
111
109
|
*/
|
|
112
110
|
getResourceName() {
|
|
113
111
|
const out = typeof this.resourceName === 'function'
|
|
@@ -118,9 +116,9 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
118
116
|
throw new Error('resourceName is not defined');
|
|
119
117
|
}
|
|
120
118
|
/**
|
|
121
|
-
*
|
|
119
|
+
* Returns the input codec for the given operation (e.g. `'create'`, `'update'`).
|
|
122
120
|
*
|
|
123
|
-
* @param operation - The operation
|
|
121
|
+
* @param operation - The operation name.
|
|
124
122
|
*/
|
|
125
123
|
getInputCodec(operation) {
|
|
126
124
|
const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
|
|
@@ -139,7 +137,9 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
139
137
|
return validator;
|
|
140
138
|
}
|
|
141
139
|
/**
|
|
142
|
-
*
|
|
140
|
+
* Returns the output codec for the given operation.
|
|
141
|
+
*
|
|
142
|
+
* @param operation - The operation name.
|
|
143
143
|
*/
|
|
144
144
|
getOutputCodec(operation) {
|
|
145
145
|
const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
|
|
@@ -157,10 +157,10 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
157
157
|
return validator;
|
|
158
158
|
}
|
|
159
159
|
/**
|
|
160
|
-
*
|
|
160
|
+
* Inserts a new record into the database and returns the created document.
|
|
161
161
|
*
|
|
162
|
-
* @param command
|
|
163
|
-
* @returns
|
|
162
|
+
* @param command - The create command.
|
|
163
|
+
* @returns The created document.
|
|
164
164
|
* @protected
|
|
165
165
|
*/
|
|
166
166
|
async _create(command) {
|
|
@@ -177,10 +177,9 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
177
177
|
throw new InternalServerError(`Unknown error while creating document for "${this.getResourceName()}"`);
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
180
|
-
*
|
|
180
|
+
* Inserts a new record into the database without returning it.
|
|
181
181
|
*
|
|
182
|
-
* @param command
|
|
183
|
-
* @returns - A promise that resolves to the created resource
|
|
182
|
+
* @param command - The create command.
|
|
184
183
|
* @protected
|
|
185
184
|
*/
|
|
186
185
|
async _createOnly(command) {
|
|
@@ -193,55 +192,54 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
193
192
|
return await repo.createOnly(data, options);
|
|
194
193
|
}
|
|
195
194
|
/**
|
|
196
|
-
* Returns the count of records
|
|
195
|
+
* Returns the count of records matching the command options.
|
|
197
196
|
*
|
|
198
|
-
* @param command
|
|
199
|
-
* @return - A promise that resolves to the count of records
|
|
197
|
+
* @param command - The count command.
|
|
200
198
|
* @protected
|
|
201
199
|
*/
|
|
202
200
|
async _count(command) {
|
|
203
201
|
const filter = command.options?.filter
|
|
204
|
-
? SQBAdapter.
|
|
202
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
205
203
|
: undefined;
|
|
206
204
|
return this._dbCount({ ...command.options, filter });
|
|
207
205
|
}
|
|
208
206
|
/**
|
|
209
|
-
* Deletes
|
|
207
|
+
* Deletes the record identified by `command.documentId`.
|
|
210
208
|
*
|
|
211
|
-
* @param command
|
|
212
|
-
* @
|
|
209
|
+
* @param command - The delete command.
|
|
210
|
+
* @returns The number of records deleted.
|
|
213
211
|
* @protected
|
|
214
212
|
*/
|
|
215
213
|
async _delete(command) {
|
|
216
214
|
isNotNullish(command.documentId, { label: 'documentId' });
|
|
217
215
|
const filter = command.options?.filter
|
|
218
|
-
? SQBAdapter.
|
|
216
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
219
217
|
: undefined;
|
|
220
218
|
return this._dbDelete(command.documentId, { ...command.options, filter });
|
|
221
219
|
}
|
|
222
220
|
/**
|
|
223
|
-
* Deletes
|
|
221
|
+
* Deletes all records matching the command filter.
|
|
224
222
|
*
|
|
225
|
-
* @param command
|
|
226
|
-
* @
|
|
223
|
+
* @param command - The deleteMany command.
|
|
224
|
+
* @returns The number of records deleted.
|
|
227
225
|
* @protected
|
|
228
226
|
*/
|
|
229
227
|
async _deleteMany(command) {
|
|
230
228
|
const filter = command.options?.filter
|
|
231
|
-
? SQBAdapter.
|
|
229
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
232
230
|
: undefined;
|
|
233
231
|
return await this._dbDeleteMany({ ...command.options, filter });
|
|
234
232
|
}
|
|
235
233
|
/**
|
|
236
|
-
* Checks
|
|
234
|
+
* Checks whether the record identified by `command.documentId` exists.
|
|
237
235
|
*
|
|
238
|
-
* @param command
|
|
236
|
+
* @param command - The exists command.
|
|
239
237
|
* @protected
|
|
240
238
|
*/
|
|
241
239
|
async _exists(command) {
|
|
242
240
|
isNotNullish(command.documentId, { label: 'documentId' });
|
|
243
241
|
const filter = command.options?.filter
|
|
244
|
-
? SQBAdapter.
|
|
242
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
245
243
|
: undefined;
|
|
246
244
|
return await this._dbExists(command.documentId, {
|
|
247
245
|
...command.options,
|
|
@@ -249,30 +247,29 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
249
247
|
});
|
|
250
248
|
}
|
|
251
249
|
/**
|
|
252
|
-
* Checks
|
|
250
|
+
* Checks whether any record matching the command filter exists.
|
|
253
251
|
*
|
|
254
|
-
* @param command
|
|
255
|
-
* @return - A Promise that resolves to a boolean indicating whether the record exists or not.
|
|
252
|
+
* @param command - The existsOne command.
|
|
256
253
|
* @protected
|
|
257
254
|
*/
|
|
258
255
|
async _existsOne(command) {
|
|
259
256
|
const filter = command.options?.filter
|
|
260
|
-
? SQBAdapter.
|
|
257
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
261
258
|
: undefined;
|
|
262
259
|
return await this._dbExistsOne({ ...command.options, filter });
|
|
263
260
|
}
|
|
264
261
|
/**
|
|
265
|
-
* Finds
|
|
262
|
+
* Finds the record identified by `command.documentId`.
|
|
266
263
|
*
|
|
267
|
-
* @param command
|
|
268
|
-
* @
|
|
264
|
+
* @param command - The findById command.
|
|
265
|
+
* @returns The found record, or `undefined` if not found.
|
|
269
266
|
* @protected
|
|
270
267
|
*/
|
|
271
268
|
async _findById(command) {
|
|
272
269
|
isNotNullish(command.documentId, { label: 'documentId' });
|
|
273
270
|
const decode = this.getOutputCodec('find');
|
|
274
271
|
const filter = command.options?.filter
|
|
275
|
-
? SQBAdapter.
|
|
272
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
276
273
|
: undefined;
|
|
277
274
|
const out = await this._dbFindById(command.documentId, {
|
|
278
275
|
...command.options,
|
|
@@ -281,31 +278,31 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
281
278
|
return out ? decode(out) : undefined;
|
|
282
279
|
}
|
|
283
280
|
/**
|
|
284
|
-
* Finds
|
|
281
|
+
* Finds the first record matching the command filter.
|
|
285
282
|
*
|
|
286
|
-
* @param command
|
|
287
|
-
* @
|
|
283
|
+
* @param command - The findOne command.
|
|
284
|
+
* @returns The found record, or `undefined` if not found.
|
|
288
285
|
* @protected
|
|
289
286
|
*/
|
|
290
287
|
async _findOne(command) {
|
|
291
288
|
const decode = this.getOutputCodec('find');
|
|
292
289
|
const filter = command.options?.filter
|
|
293
|
-
? SQBAdapter.
|
|
290
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
294
291
|
: undefined;
|
|
295
292
|
const out = await this._dbFindOne({ ...command.options, filter });
|
|
296
293
|
return out ? decode(out) : undefined;
|
|
297
294
|
}
|
|
298
295
|
/**
|
|
299
|
-
* Finds
|
|
296
|
+
* Finds all records matching the command filter.
|
|
300
297
|
*
|
|
301
|
-
* @param command
|
|
302
|
-
* @
|
|
298
|
+
* @param command - The findMany command.
|
|
299
|
+
* @returns An array of matching records.
|
|
303
300
|
* @protected
|
|
304
301
|
*/
|
|
305
302
|
async _findMany(command) {
|
|
306
303
|
const decode = this.getOutputCodec('find');
|
|
307
304
|
const filter = command.options?.filter
|
|
308
|
-
? SQBAdapter.
|
|
305
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
309
306
|
: undefined;
|
|
310
307
|
const out = await this._dbFindMany({ ...command.options, filter });
|
|
311
308
|
if (out?.length) {
|
|
@@ -314,10 +311,10 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
314
311
|
return out;
|
|
315
312
|
}
|
|
316
313
|
/**
|
|
317
|
-
* Updates
|
|
314
|
+
* Updates the record identified by `command.documentId` and returns it.
|
|
318
315
|
*
|
|
319
|
-
* @param command
|
|
320
|
-
* @returns
|
|
316
|
+
* @param command - The update command.
|
|
317
|
+
* @returns The updated record, or `undefined` if not found.
|
|
321
318
|
* @protected
|
|
322
319
|
*/
|
|
323
320
|
async _update(command) {
|
|
@@ -327,7 +324,7 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
327
324
|
const inputCodec = this.getInputCodec('update');
|
|
328
325
|
const data = inputCodec(input);
|
|
329
326
|
const filter = command.options?.filter
|
|
330
|
-
? SQBAdapter.
|
|
327
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
331
328
|
: undefined;
|
|
332
329
|
const out = await this._dbUpdate(documentId, data, { ...options, filter });
|
|
333
330
|
const outputCodec = this.getOutputCodec('update');
|
|
@@ -335,10 +332,10 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
335
332
|
return outputCodec(out);
|
|
336
333
|
}
|
|
337
334
|
/**
|
|
338
|
-
* Updates
|
|
335
|
+
* Updates the record identified by `command.documentId` without returning it.
|
|
339
336
|
*
|
|
340
|
-
* @param command
|
|
341
|
-
* @returns
|
|
337
|
+
* @param command - The updateOnly command.
|
|
338
|
+
* @returns The number of records modified.
|
|
342
339
|
* @protected
|
|
343
340
|
*/
|
|
344
341
|
async _updateOnly(command) {
|
|
@@ -348,15 +345,15 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
348
345
|
const inputCodec = this.getInputCodec('update');
|
|
349
346
|
const data = inputCodec(input);
|
|
350
347
|
const filter = command.options?.filter
|
|
351
|
-
? SQBAdapter.
|
|
348
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
352
349
|
: undefined;
|
|
353
350
|
return await this._dbUpdateOnly(documentId, data, { ...options, filter });
|
|
354
351
|
}
|
|
355
352
|
/**
|
|
356
|
-
* Updates
|
|
353
|
+
* Updates all records matching the command filter.
|
|
357
354
|
*
|
|
358
|
-
* @param command
|
|
359
|
-
* @
|
|
355
|
+
* @param command - The updateMany command.
|
|
356
|
+
* @returns The number of records modified.
|
|
360
357
|
* @protected
|
|
361
358
|
*/
|
|
362
359
|
async _updateMany(command) {
|
|
@@ -364,15 +361,15 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
364
361
|
const inputCodec = this.getInputCodec('update');
|
|
365
362
|
const data = inputCodec(command.input);
|
|
366
363
|
const filter = command.options?.filter
|
|
367
|
-
? SQBAdapter.
|
|
364
|
+
? SQBAdapter.prepareFilter(command.options.filter)
|
|
368
365
|
: undefined;
|
|
369
366
|
return await this._dbUpdateMany(data, { ...command.options, filter });
|
|
370
367
|
}
|
|
371
368
|
/**
|
|
372
|
-
* Acquires a connection and performs Repository.create
|
|
369
|
+
* Acquires a connection and performs `Repository.create`.
|
|
373
370
|
*
|
|
374
|
-
* @param input - The document to insert
|
|
375
|
-
* @param options - Optional settings
|
|
371
|
+
* @param input - The document to insert.
|
|
372
|
+
* @param options - Optional settings.
|
|
376
373
|
* @protected
|
|
377
374
|
*/
|
|
378
375
|
async _dbCreate(input, options) {
|
|
@@ -381,163 +378,162 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
381
378
|
return await repo.create(input, options);
|
|
382
379
|
}
|
|
383
380
|
/**
|
|
384
|
-
* Acquires a connection and performs Repository.count
|
|
381
|
+
* Acquires a connection and performs `Repository.count`.
|
|
385
382
|
*
|
|
386
|
-
* @param options -
|
|
383
|
+
* @param options - Optional settings.
|
|
387
384
|
* @protected
|
|
388
385
|
*/
|
|
389
386
|
async _dbCount(options) {
|
|
390
387
|
const conn = await this.getConnection();
|
|
391
388
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
392
389
|
if (options?.filter)
|
|
393
|
-
options.filter = SQBAdapter.
|
|
390
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
394
391
|
return await repo.count(options);
|
|
395
392
|
}
|
|
396
393
|
/**
|
|
397
|
-
* Acquires a connection and performs Repository.delete
|
|
394
|
+
* Acquires a connection and performs `Repository.delete`.
|
|
398
395
|
*
|
|
399
|
-
* @param id -
|
|
400
|
-
* @param options - Optional settings
|
|
396
|
+
* @param id - The key field value identifying the record.
|
|
397
|
+
* @param options - Optional settings.
|
|
401
398
|
* @protected
|
|
402
399
|
*/
|
|
403
400
|
async _dbDelete(id, options) {
|
|
404
401
|
const conn = await this.getConnection();
|
|
405
402
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
406
403
|
if (options?.filter)
|
|
407
|
-
options.filter = SQBAdapter.
|
|
404
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
408
405
|
return (await repo.delete(id, options)) ? 1 : 0;
|
|
409
406
|
}
|
|
410
407
|
/**
|
|
411
|
-
* Acquires a connection and performs Repository.deleteMany
|
|
408
|
+
* Acquires a connection and performs `Repository.deleteMany`.
|
|
412
409
|
*
|
|
413
|
-
* @param options - Optional settings
|
|
410
|
+
* @param options - Optional settings.
|
|
414
411
|
* @protected
|
|
415
412
|
*/
|
|
416
413
|
async _dbDeleteMany(options) {
|
|
417
414
|
const conn = await this.getConnection();
|
|
418
415
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
419
416
|
if (options?.filter)
|
|
420
|
-
options.filter = SQBAdapter.
|
|
417
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
421
418
|
return await repo.deleteMany(options);
|
|
422
419
|
}
|
|
423
420
|
/**
|
|
424
|
-
* Acquires a connection and performs Repository.exists
|
|
421
|
+
* Acquires a connection and performs `Repository.exists`.
|
|
425
422
|
*
|
|
426
|
-
* @param id -
|
|
427
|
-
* @param options - Optional settings
|
|
423
|
+
* @param id - The key field value identifying the record.
|
|
424
|
+
* @param options - Optional settings.
|
|
428
425
|
* @protected
|
|
429
426
|
*/
|
|
430
427
|
async _dbExists(id, options) {
|
|
431
428
|
const conn = await this.getConnection();
|
|
432
429
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
433
430
|
if (options?.filter)
|
|
434
|
-
options.filter = SQBAdapter.
|
|
431
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
435
432
|
return await repo.exists(id, options);
|
|
436
433
|
}
|
|
437
434
|
/**
|
|
438
|
-
* Acquires a connection and performs Repository.existsOne
|
|
435
|
+
* Acquires a connection and performs `Repository.existsOne`.
|
|
439
436
|
*
|
|
440
|
-
* @param options - Optional settings
|
|
437
|
+
* @param options - Optional settings.
|
|
441
438
|
* @protected
|
|
442
439
|
*/
|
|
443
440
|
async _dbExistsOne(options) {
|
|
444
441
|
const conn = await this.getConnection();
|
|
445
442
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
446
443
|
if (options?.filter)
|
|
447
|
-
options.filter = SQBAdapter.
|
|
444
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
448
445
|
return await repo.existsOne(options);
|
|
449
446
|
}
|
|
450
447
|
/**
|
|
451
|
-
* Acquires a connection and performs Repository.findById
|
|
448
|
+
* Acquires a connection and performs `Repository.findById`.
|
|
452
449
|
*
|
|
453
|
-
* @param id -
|
|
454
|
-
* @param options - Optional settings
|
|
450
|
+
* @param id - The key field value identifying the record.
|
|
451
|
+
* @param options - Optional settings.
|
|
455
452
|
* @protected
|
|
456
453
|
*/
|
|
457
454
|
async _dbFindById(id, options) {
|
|
458
455
|
const conn = await this.getConnection();
|
|
459
456
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
460
457
|
if (options?.filter)
|
|
461
|
-
options.filter = SQBAdapter.
|
|
458
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
462
459
|
return await repo.findById(id, options);
|
|
463
460
|
}
|
|
464
461
|
/**
|
|
465
|
-
* Acquires a connection and performs Repository.findOne
|
|
462
|
+
* Acquires a connection and performs `Repository.findOne`.
|
|
466
463
|
*
|
|
467
|
-
* @param options - Optional settings
|
|
464
|
+
* @param options - Optional settings.
|
|
468
465
|
* @protected
|
|
469
466
|
*/
|
|
470
467
|
async _dbFindOne(options) {
|
|
471
468
|
const conn = await this.getConnection();
|
|
472
469
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
473
470
|
if (options?.filter)
|
|
474
|
-
options.filter = SQBAdapter.
|
|
471
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
475
472
|
return await repo.findOne({ ...options, offset: options?.skip });
|
|
476
473
|
}
|
|
477
474
|
/**
|
|
478
|
-
* Acquires a connection and performs Repository.findMany
|
|
475
|
+
* Acquires a connection and performs `Repository.findMany`.
|
|
479
476
|
*
|
|
480
|
-
* @param options - Optional settings
|
|
477
|
+
* @param options - Optional settings.
|
|
481
478
|
* @protected
|
|
482
479
|
*/
|
|
483
480
|
async _dbFindMany(options) {
|
|
484
481
|
const conn = await this.getConnection();
|
|
485
482
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
486
483
|
if (options?.filter)
|
|
487
|
-
options.filter = SQBAdapter.
|
|
484
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
488
485
|
return await repo.findMany({ ...options, offset: options?.skip });
|
|
489
486
|
}
|
|
490
487
|
/**
|
|
491
|
-
* Acquires a connection and performs Repository.update
|
|
488
|
+
* Acquires a connection and performs `Repository.update`.
|
|
492
489
|
*
|
|
493
|
-
* @param id -
|
|
494
|
-
* @param data - The update values
|
|
495
|
-
* @param options - Optional settings
|
|
490
|
+
* @param id - The key field value identifying the record.
|
|
491
|
+
* @param data - The update values.
|
|
492
|
+
* @param options - Optional settings.
|
|
496
493
|
* @protected
|
|
497
494
|
*/
|
|
498
495
|
async _dbUpdate(id, data, options) {
|
|
499
496
|
const conn = await this.getConnection();
|
|
500
497
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
501
498
|
if (options?.filter)
|
|
502
|
-
options.filter = SQBAdapter.
|
|
499
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
503
500
|
return await repo.update(id, data, options);
|
|
504
501
|
}
|
|
505
502
|
/**
|
|
506
|
-
* Acquires a connection and performs Repository.updateOnly
|
|
503
|
+
* Acquires a connection and performs `Repository.updateOnly`.
|
|
507
504
|
*
|
|
508
|
-
* @param id -
|
|
509
|
-
* @param data - The update values
|
|
510
|
-
* @param options - Optional settings
|
|
505
|
+
* @param id - The key field value identifying the record.
|
|
506
|
+
* @param data - The update values.
|
|
507
|
+
* @param options - Optional settings.
|
|
511
508
|
* @protected
|
|
512
509
|
*/
|
|
513
510
|
async _dbUpdateOnly(id, data, options) {
|
|
514
511
|
const conn = await this.getConnection();
|
|
515
512
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
516
513
|
if (options?.filter)
|
|
517
|
-
options.filter = SQBAdapter.
|
|
514
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
518
515
|
return (await repo.updateOnly(id, data, options)) ? 1 : 0;
|
|
519
516
|
}
|
|
520
517
|
/**
|
|
521
|
-
* Acquires a connection and performs Repository.updateMany
|
|
518
|
+
* Acquires a connection and performs `Repository.updateMany`.
|
|
522
519
|
*
|
|
523
|
-
* @param data - The update values
|
|
524
|
-
* @param options - Optional settings
|
|
520
|
+
* @param data - The update values.
|
|
521
|
+
* @param options - Optional settings.
|
|
525
522
|
* @protected
|
|
526
523
|
*/
|
|
527
524
|
async _dbUpdateMany(data, options) {
|
|
528
525
|
const conn = await this.getConnection();
|
|
529
526
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
530
527
|
if (options?.filter)
|
|
531
|
-
options.filter = SQBAdapter.
|
|
528
|
+
options.filter = SQBAdapter.prepareFilter(options.filter);
|
|
532
529
|
return await repo.updateMany(data, options);
|
|
533
530
|
}
|
|
534
531
|
/**
|
|
535
|
-
*
|
|
536
|
-
*
|
|
532
|
+
* Builds the common filter for the given command.
|
|
533
|
+
* Used primarily for multi-tenant isolation and similar cross-cutting concerns.
|
|
537
534
|
*
|
|
538
535
|
* @protected
|
|
539
|
-
* @returns
|
|
540
|
-
* that resolves to the common filter, or undefined if not available.
|
|
536
|
+
* @returns The resolved filter input, or `undefined` if none is configured.
|
|
541
537
|
*/
|
|
542
538
|
_getCommonFilter(command) {
|
|
543
539
|
const commonFilter = Array.isArray(this.commonFilter)
|
|
@@ -559,7 +555,7 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
559
555
|
if (!(proto instanceof SqbEntityService))
|
|
560
556
|
break;
|
|
561
557
|
}
|
|
562
|
-
|
|
558
|
+
/* Call before[X] hooks */
|
|
563
559
|
if (command.crud === 'create')
|
|
564
560
|
await this._beforeCreate(command);
|
|
565
561
|
else if (command.crud === 'update' && command.byId) {
|
|
@@ -574,12 +570,12 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
574
570
|
else if (command.crud === 'delete' && !command.byId) {
|
|
575
571
|
await this._beforeDeleteMany(command);
|
|
576
572
|
}
|
|
577
|
-
|
|
573
|
+
/* Call command function */
|
|
578
574
|
return commandFn();
|
|
579
575
|
};
|
|
580
576
|
try {
|
|
581
577
|
const result = await next();
|
|
582
|
-
|
|
578
|
+
/* Call after[X] hooks */
|
|
583
579
|
if (command.crud === 'create')
|
|
584
580
|
await this._afterCreate(command, result);
|
|
585
581
|
else if (command.crud === 'update' && command.byId) {
|
package/sqb-service-base.d.ts
CHANGED
|
@@ -1,41 +1,45 @@
|
|
|
1
1
|
import { ServiceBase } from '@opra/core';
|
|
2
2
|
import { SqbClient, SqbConnection } from '@sqb/connect';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Namespace for SqbServiceBase types and options.
|
|
5
5
|
*/
|
|
6
6
|
export declare namespace SqbServiceBase {
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Options for initializing SqbServiceBase.
|
|
9
|
+
*/
|
|
10
|
+
interface Options extends ServiceBase.Options {
|
|
11
|
+
/**
|
|
12
|
+
* The SQB client or connection used by this service.
|
|
13
|
+
*/
|
|
8
14
|
db?: SqbServiceBase['db'];
|
|
9
15
|
}
|
|
10
16
|
}
|
|
11
17
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @template T - The data type class type of the resource
|
|
18
|
+
* Base class for services using SQB (SQL Builder) for database operations.
|
|
14
19
|
*/
|
|
15
20
|
export declare class SqbServiceBase extends ServiceBase {
|
|
16
21
|
/**
|
|
17
|
-
*
|
|
22
|
+
* The SQB client or connection used by this service.
|
|
18
23
|
*/
|
|
19
24
|
db?: (SqbClient | SqbConnection) | ((_this: this) => SqbClient | SqbConnection);
|
|
20
25
|
/**
|
|
21
|
-
* Constructs a new instance
|
|
26
|
+
* Constructs a new instance.
|
|
22
27
|
*
|
|
23
|
-
* @param
|
|
24
|
-
* @constructor
|
|
28
|
+
* @param options - Options for the service.
|
|
25
29
|
*/
|
|
26
30
|
constructor(options?: SqbServiceBase.Options);
|
|
27
31
|
/**
|
|
28
|
-
* Executes the provided
|
|
32
|
+
* Executes the provided callback within a database transaction.
|
|
33
|
+
* If a transaction is already active in the current context, the callback
|
|
34
|
+
* joins it rather than starting a new one.
|
|
29
35
|
*
|
|
30
|
-
* @param callback - The function to
|
|
36
|
+
* @param callback - The function to execute within the transaction.
|
|
31
37
|
*/
|
|
32
38
|
withTransaction(callback: (connection: SqbConnection, _this: this) => any): Promise<any>;
|
|
33
39
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* @protected
|
|
40
|
+
* Returns the active database connection for the current context.
|
|
37
41
|
*
|
|
38
|
-
* @throws
|
|
42
|
+
* @throws If no database connection is configured.
|
|
39
43
|
*/
|
|
40
44
|
getConnection(): SqbConnection | SqbClient | Promise<SqbConnection | SqbClient>;
|
|
41
45
|
}
|