@opra/sqb 0.33.13 → 1.0.0-alpha.10

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.
Files changed (43) hide show
  1. package/cjs/{transform-filter.js → adapter-utils/parse-filter.js} +37 -12
  2. package/cjs/augmentation/datatype-factory.augmentation.js +79 -0
  3. package/cjs/augmentation/mixin-type.augmentation.js +5 -3
  4. package/cjs/index.js +3 -4
  5. package/cjs/sqb-adapter.js +71 -100
  6. package/cjs/sqb-collection-service.js +297 -0
  7. package/cjs/sqb-entity-service.js +446 -25
  8. package/cjs/sqb-singleton-service.js +181 -0
  9. package/esm/{transform-filter.js → adapter-utils/parse-filter.js} +36 -11
  10. package/esm/augmentation/datatype-factory.augmentation.js +77 -0
  11. package/esm/augmentation/mapped-type.augmentation.js +1 -1
  12. package/esm/augmentation/mixin-type.augmentation.js +6 -4
  13. package/esm/index.js +3 -4
  14. package/esm/sqb-adapter.js +71 -100
  15. package/esm/sqb-collection-service.js +293 -0
  16. package/esm/sqb-entity-service.js +446 -25
  17. package/esm/sqb-singleton-service.js +177 -0
  18. package/package.json +16 -11
  19. package/types/adapter-utils/parse-filter.d.ts +10 -0
  20. package/types/index.d.ts +3 -4
  21. package/types/sqb-adapter.d.ts +10 -8
  22. package/types/sqb-collection-service.d.ts +233 -0
  23. package/types/sqb-entity-service.d.ts +418 -18
  24. package/types/sqb-singleton-service.d.ts +137 -0
  25. package/cjs/augmentation/api-document-factory.augmentation.js +0 -20
  26. package/cjs/augmentation/type-document-factory.augmentation.js +0 -99
  27. package/cjs/sqb-collection.js +0 -80
  28. package/cjs/sqb-entity-service-base.js +0 -170
  29. package/cjs/sqb-singleton.js +0 -44
  30. package/cjs/transform-key-values.js +0 -14
  31. package/esm/augmentation/api-document-factory.augmentation.js +0 -18
  32. package/esm/augmentation/type-document-factory.augmentation.js +0 -97
  33. package/esm/sqb-collection.js +0 -76
  34. package/esm/sqb-entity-service-base.js +0 -166
  35. package/esm/sqb-singleton.js +0 -40
  36. package/esm/transform-key-values.js +0 -11
  37. package/types/augmentation/type-document-factory.augmentation.d.ts +0 -1
  38. package/types/sqb-collection.d.ts +0 -31
  39. package/types/sqb-entity-service-base.d.ts +0 -31
  40. package/types/sqb-singleton.d.ts +0 -18
  41. package/types/transform-filter.d.ts +0 -3
  42. package/types/transform-key-values.d.ts +0 -3
  43. /package/types/augmentation/{api-document-factory.augmentation.d.ts → datatype-factory.augmentation.d.ts} +0 -0
@@ -1,37 +1,458 @@
1
- import { SqbEntityServiceBase } from './sqb-entity-service-base.js';
2
- export class SqbEntityService extends SqbEntityServiceBase {
3
- constructor(typeClass, options) {
4
- super(typeClass, options);
5
- this.typeClass = typeClass;
1
+ import { InternalServerError } from '@opra/common';
2
+ import { ServiceBase } from '@opra/core';
3
+ import { EntityMetadata } from '@sqb/connect';
4
+ import { SQBAdapter } from './sqb-adapter.js';
5
+ /**
6
+ * @class SqbEntityService
7
+ * @template T - The data type class type of the resource
8
+ */
9
+ export class SqbEntityService extends ServiceBase {
10
+ /**
11
+ * Constructs a new instance
12
+ *
13
+ * @param dataType - The data type of the returning results
14
+ * @param [options] - The options for the service.
15
+ * @constructor
16
+ */
17
+ constructor(dataType, options) {
18
+ super();
19
+ this._inputCodecs = {};
20
+ this._outputCodecs = {};
21
+ this._dataType_ = dataType;
22
+ this.db = options?.db;
23
+ this.$resourceName = options?.resourceName;
24
+ this.$commonFilter = this.$commonFilter || options?.commonFilter;
25
+ this.$interceptor = this.$interceptor || options?.interceptor;
6
26
  }
7
- async count(options) {
8
- return super._count(options);
27
+ /**
28
+ * Retrieves the OPRA data type
29
+ *
30
+ * @throws {NotAcceptableError} If the data type is not a ComplexType.
31
+ */
32
+ get dataType() {
33
+ if (!this._dataType)
34
+ this._dataType = this.context.document.node.getComplexType(this._dataType_);
35
+ return this._dataType;
9
36
  }
10
- async create(data, options) {
11
- return super._create(data, options);
37
+ /**
38
+ * Retrieves the Class of the data type
39
+ *
40
+ * @throws {NotAcceptableError} If the data type is not a ComplexType.
41
+ */
42
+ get dataTypeClass() {
43
+ if (!this._dataTypeClass)
44
+ this._dataTypeClass = this.entityMetadata.ctor;
45
+ return this._dataTypeClass;
12
46
  }
13
- async delete(keyValue, options) {
14
- return super._delete(keyValue, options);
47
+ /**
48
+ * Retrieves the SQB entity metadata object
49
+ *
50
+ * @throws {TypeError} If metadata is not available
51
+ */
52
+ get entityMetadata() {
53
+ if (!this._entityMetadata) {
54
+ const t = this.dataType.ctor;
55
+ const metadata = EntityMetadata.get(t);
56
+ if (!metadata)
57
+ throw new TypeError(`Class (${t}) is not decorated with $Entity() decorator`);
58
+ this._entityMetadata = metadata;
59
+ }
60
+ return this._entityMetadata;
15
61
  }
16
- async deleteMany(options) {
17
- return super._deleteMany(options);
62
+ /**
63
+ * Retrieves the resource name.
64
+ *
65
+ * @returns {string} The resource name.
66
+ * @throws {Error} If the collection name is not defined.
67
+ */
68
+ getResourceName() {
69
+ const out = typeof this.$resourceName === 'function' ? this.$resourceName(this) : this.$resourceName || this.dataType.name;
70
+ if (out)
71
+ return out;
72
+ throw new Error('resourceName is not defined');
18
73
  }
19
- async find(keyValue, options) {
20
- return super._find(keyValue, options);
74
+ /**
75
+ * Retrieves the codec for the specified operation.
76
+ *
77
+ * @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
78
+ */
79
+ getInputCodec(operation) {
80
+ let validator = this._inputCodecs[operation];
81
+ if (validator)
82
+ return validator;
83
+ const options = { projection: '*' };
84
+ if (operation === 'update')
85
+ options.partial = 'deep';
86
+ const dataType = this.dataType;
87
+ validator = dataType.generateCodec('decode', options);
88
+ this._inputCodecs[operation] = validator;
89
+ return validator;
21
90
  }
22
- async findOne(options) {
23
- return super._findOne(options);
91
+ /**
92
+ * Retrieves the codec.
93
+ */
94
+ getOutputCodec(operation) {
95
+ let validator = this._outputCodecs[operation];
96
+ if (validator)
97
+ return validator;
98
+ const options = { projection: '*', partial: 'deep' };
99
+ const dataType = this.dataType;
100
+ validator = dataType.generateCodec('decode', options);
101
+ this._outputCodecs[operation] = validator;
102
+ return validator;
24
103
  }
25
- async findMany(options) {
26
- return super._findMany(options);
104
+ /**
105
+ * Insert a new record into database
106
+ *
107
+ * @param {PartialDTO<T>} input - The input data
108
+ * @param {SqbEntityService.CreateOptions} [options] - The options object
109
+ * @returns {Promise<PartialDTO<T>>} A promise that resolves to the created resource
110
+ * @throws {InternalServerError} if an unknown error occurs while creating the resource
111
+ * @protected
112
+ */
113
+ async _create(input, options) {
114
+ const inputCodec = this.getInputCodec('create');
115
+ const outputCodec = this.getOutputCodec('create');
116
+ const data = inputCodec(input);
117
+ const out = await this._dbCreate(data, options);
118
+ if (out)
119
+ return outputCodec(out);
120
+ throw new InternalServerError(`Unknown error while creating document for "${this.getResourceName()}"`);
27
121
  }
28
- async exists(options) {
29
- return super._exists(options);
122
+ /**
123
+ * Returns the count of records based on the provided options
124
+ *
125
+ * @param {SqbEntityService.CountOptions} options - The options for the count operation.
126
+ * @return {Promise<number>} - A promise that resolves to the count of records
127
+ * @protected
128
+ */
129
+ async _count(options) {
130
+ return this._dbCount(options);
30
131
  }
31
- async update(keyValue, data, options) {
32
- return super._update(keyValue, data, options);
132
+ /**
133
+ * Deletes a record from the collection.
134
+ *
135
+ * @param {SQBAdapter.IdOrIds} id - The ID of the document to delete.
136
+ * @param {SqbEntityService.DeleteOptions} [options] - Optional delete options.
137
+ * @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
138
+ * @protected
139
+ */
140
+ async _delete(id, options) {
141
+ return this._dbDelete(id, options);
33
142
  }
34
- async updateMany(data, options) {
35
- return super._updateMany(data, options);
143
+ /**
144
+ * Deletes multiple documents from the collection that meet the specified filter criteria.
145
+ *
146
+ * @param {SqbEntityService.DeleteManyOptions} options - The options for the delete operation.
147
+ * @return {Promise<number>} - A promise that resolves to the number of documents deleted.
148
+ * @protected
149
+ */
150
+ async _deleteMany(options) {
151
+ return await this._dbDeleteMany(options);
152
+ }
153
+ /**
154
+ * Checks if a record with the given id exists.
155
+ *
156
+ * @param {SQBAdapter.IdOrIds} id - The id of the object to check.
157
+ * @param {SqbEntityService.ExistsOptions} [options] - The options for the query (optional).
158
+ * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
159
+ * @protected
160
+ */
161
+ async _exists(id, options) {
162
+ return await this._dbExists(id, options);
163
+ }
164
+ /**
165
+ * Checks if a record with the given arguments exists.
166
+ *
167
+ * @param {SqbEntityService.ExistsOneOptions} [options] - The options for the query (optional).
168
+ * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
169
+ * @protected
170
+ */
171
+ async _existsOne(options) {
172
+ return await this._dbExistsOne(options);
173
+ }
174
+ /**
175
+ * Finds a record by ID.
176
+ *
177
+ * @param {SQBAdapter.Id} id - The ID of the record.
178
+ * @param {SqbEntityService.FindOneOptions} [options] - The options for the find query.
179
+ * @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
180
+ * @protected
181
+ */
182
+ async _findById(id, options) {
183
+ const decode = this.getOutputCodec('find');
184
+ const out = await this._dbFindById(id, options);
185
+ return out ? decode(out) : undefined;
186
+ }
187
+ /**
188
+ * Finds a record in the collection that matches the specified options.
189
+ *
190
+ * @param {SqbEntityService.FindOneOptions} [options] - The options for the query.
191
+ * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
192
+ * @protected
193
+ */
194
+ async _findOne(options) {
195
+ const decode = this.getOutputCodec('find');
196
+ const out = await this._dbFindOne(options);
197
+ return out ? decode(out) : undefined;
198
+ }
199
+ /**
200
+ * Finds multiple records in collection.
201
+ *
202
+ * @param {SqbEntityService.FindManyOptions} [options] - The options for the find operation.
203
+ * @return A Promise that resolves to an array of partial outputs of type T.
204
+ * @protected
205
+ */
206
+ async _findMany(options) {
207
+ const decode = this.getOutputCodec('find');
208
+ const out = await this._dbFindMany(options);
209
+ if (out?.length) {
210
+ return out.map(x => decode(x));
211
+ }
212
+ return out;
213
+ }
214
+ /**
215
+ * Updates a record with the given id in the collection.
216
+ *
217
+ * @param {SQBAdapter.IdOrIds} id - The id of the document to update.
218
+ * @param {PatchDTO<T>} input - The partial input object containing the fields to update.
219
+ * @param {SqbEntityService.UpdateOptions} [options] - The options for the update operation.
220
+ * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
221
+ * undefined if the document was not found.
222
+ * @protected
223
+ */
224
+ async _update(id, input, options) {
225
+ const inputCodec = this.getInputCodec('update');
226
+ const data = inputCodec(input);
227
+ const out = await this._dbUpdate(id, data, options);
228
+ const outputCodec = this.getOutputCodec('update');
229
+ if (out)
230
+ return outputCodec(out);
231
+ }
232
+ /**
233
+ * Updates a record in the collection with the specified ID and returns updated record count
234
+ *
235
+ * @param {any} id - The ID of the document to update.
236
+ * @param {PatchDTO<T>} input - The partial input data to update the document with.
237
+ * @param {SqbEntityService.UpdateOptions} options - The options for updating the document.
238
+ * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
239
+ * @protected
240
+ */
241
+ async _updateOnly(id, input, options) {
242
+ const inputCodec = this.getInputCodec('create');
243
+ const data = inputCodec(input);
244
+ return await this._dbUpdateOnly(id, data, options);
245
+ }
246
+ /**
247
+ * Updates multiple records in the collection based on the specified input and options.
248
+ *
249
+ * @param {PatchDTO<T>} input - The partial input to update the documents with.
250
+ * @param {SqbEntityService.UpdateManyOptions} options - The options for updating the documents.
251
+ * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
252
+ * @protected
253
+ */
254
+ async _updateMany(input, options) {
255
+ const inputCodec = this.getInputCodec('update');
256
+ const data = inputCodec(input);
257
+ return await this._dbUpdateMany(data, options);
258
+ }
259
+ /**
260
+ * Acquires a connection and performs Repository.create operation
261
+ *
262
+ * @param input - The document to insert
263
+ * @param options - Optional settings for the command
264
+ * @protected
265
+ */
266
+ async _dbCreate(input, options) {
267
+ const conn = await this.getConnection();
268
+ const repo = conn.getRepository(this.dataTypeClass);
269
+ return await repo.create(input, options);
270
+ }
271
+ /**
272
+ * Acquires a connection and performs Repository.count operation
273
+ *
274
+ * @param options - The options for counting documents.
275
+ * @protected
276
+ */
277
+ async _dbCount(options) {
278
+ const conn = await this.getConnection();
279
+ const repo = conn.getRepository(this.dataTypeClass);
280
+ if (options?.filter)
281
+ options.filter = SQBAdapter.parseFilter(options.filter);
282
+ return await repo.count(options);
283
+ }
284
+ /**
285
+ * Acquires a connection and performs Repository.delete operation
286
+ *
287
+ * @param id - Value of the key field used to select the record
288
+ * @param options - Optional settings for the command
289
+ * @protected
290
+ */
291
+ async _dbDelete(id, options) {
292
+ const conn = await this.getConnection();
293
+ const repo = conn.getRepository(this.dataTypeClass);
294
+ if (options?.filter)
295
+ options.filter = SQBAdapter.parseFilter(options.filter);
296
+ return (await repo.delete(id, options)) ? 1 : 0;
297
+ }
298
+ /**
299
+ * Acquires a connection and performs Repository.deleteMany operation
300
+ *
301
+ * @param options - Optional settings for the command
302
+ * @protected
303
+ */
304
+ async _dbDeleteMany(options) {
305
+ const conn = await this.getConnection();
306
+ const repo = conn.getRepository(this.dataTypeClass);
307
+ if (options?.filter)
308
+ options.filter = SQBAdapter.parseFilter(options.filter);
309
+ return await repo.deleteMany(options);
310
+ }
311
+ /**
312
+ * Acquires a connection and performs Repository.exists operation
313
+ *
314
+ * @param id - Value of the key field used to select the record
315
+ * @param options - Optional settings for the command
316
+ * @protected
317
+ */
318
+ async _dbExists(id, options) {
319
+ const conn = await this.getConnection();
320
+ const repo = conn.getRepository(this.dataTypeClass);
321
+ if (options?.filter)
322
+ options.filter = SQBAdapter.parseFilter(options.filter);
323
+ return await repo.exists(id, options);
324
+ }
325
+ /**
326
+ * Acquires a connection and performs Repository.existsOne operation
327
+ *
328
+ * @param options - Optional settings for the command
329
+ * @protected
330
+ */
331
+ async _dbExistsOne(options) {
332
+ const conn = await this.getConnection();
333
+ const repo = conn.getRepository(this.dataTypeClass);
334
+ if (options?.filter)
335
+ options.filter = SQBAdapter.parseFilter(options.filter);
336
+ return await repo.existsOne(options);
337
+ }
338
+ /**
339
+ * Acquires a connection and performs Repository.findById operation
340
+ *
341
+ * @param id - Value of the key field used to select the record
342
+ * @param options - Optional settings for the command
343
+ * @protected
344
+ */
345
+ async _dbFindById(id, options) {
346
+ const conn = await this.getConnection();
347
+ const repo = conn.getRepository(this.dataTypeClass);
348
+ if (options?.filter)
349
+ options.filter = SQBAdapter.parseFilter(options.filter);
350
+ return await repo.findById(id, options);
351
+ }
352
+ /**
353
+ * Acquires a connection and performs Repository.findOne operation
354
+ *
355
+ * @param options - Optional settings for the command
356
+ * @protected
357
+ */
358
+ async _dbFindOne(options) {
359
+ const conn = await this.getConnection();
360
+ const repo = conn.getRepository(this.dataTypeClass);
361
+ if (options?.filter)
362
+ options.filter = SQBAdapter.parseFilter(options.filter);
363
+ return await repo.findOne(options);
364
+ }
365
+ /**
366
+ * Acquires a connection and performs Repository.findMany operation
367
+ *
368
+ * @param options - Optional settings for the command
369
+ * @protected
370
+ */
371
+ async _dbFindMany(options) {
372
+ const conn = await this.getConnection();
373
+ const repo = conn.getRepository(this.dataTypeClass);
374
+ if (options?.filter)
375
+ options.filter = SQBAdapter.parseFilter(options.filter);
376
+ return await repo.findMany(options);
377
+ }
378
+ /**
379
+ * Acquires a connection and performs Repository.update operation
380
+ *
381
+ * @param id - Value of the key field used to select the record
382
+ * @param data - The update values to be applied to the document
383
+ * @param options - Optional settings for the command
384
+ * @protected
385
+ */
386
+ async _dbUpdate(id, data, options) {
387
+ const conn = await this.getConnection();
388
+ const repo = conn.getRepository(this.dataTypeClass);
389
+ if (options?.filter)
390
+ options.filter = SQBAdapter.parseFilter(options.filter);
391
+ return await repo.update(id, data, options);
392
+ }
393
+ /**
394
+ * Acquires a connection and performs Repository.updateOnly operation
395
+ *
396
+ * @param id - Value of the key field used to select the record
397
+ * @param data - The update values to be applied to the document
398
+ * @param options - Optional settings for the command
399
+ * @protected
400
+ */
401
+ async _dbUpdateOnly(id, data, options) {
402
+ const conn = await this.getConnection();
403
+ const repo = conn.getRepository(this.dataTypeClass);
404
+ if (options?.filter)
405
+ options.filter = SQBAdapter.parseFilter(options.filter);
406
+ return await repo.updateOnly(id, data, options);
407
+ }
408
+ /**
409
+ * Acquires a connection and performs Repository.updateMany operation
410
+ *
411
+ * @param data - The update values to be applied to the document
412
+ * @param options - Optional settings for the command
413
+ * @protected
414
+ */
415
+ async _dbUpdateMany(data, options) {
416
+ const conn = await this.getConnection();
417
+ const repo = conn.getRepository(this.dataTypeClass);
418
+ if (options?.filter)
419
+ options.filter = SQBAdapter.parseFilter(options.filter);
420
+ return await repo.updateMany(data, options);
421
+ }
422
+ /**
423
+ * Retrieves the database connection.
424
+ *
425
+ * @protected
426
+ *
427
+ * @throws {Error} If the context or database is not set.
428
+ */
429
+ getConnection() {
430
+ const db = typeof this.db === 'function' ? this.db(this) : this.db;
431
+ if (!db)
432
+ throw new Error(`Database not set!`);
433
+ return db;
434
+ }
435
+ /**
436
+ * Retrieves the common filter used for querying documents.
437
+ * This method is mostly used for security issues like securing multi-tenant applications.
438
+ *
439
+ * @protected
440
+ * @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
441
+ * that resolves to the common filter, or undefined if not available.
442
+ */
443
+ _getCommonFilter(args) {
444
+ return typeof this.$commonFilter === 'function' ? this.$commonFilter(args, this) : this.$commonFilter;
445
+ }
446
+ async _intercept(callback, args) {
447
+ try {
448
+ if (this.$interceptor)
449
+ return this.$interceptor(callback, args, this);
450
+ return callback();
451
+ }
452
+ catch (e) {
453
+ Error.captureStackTrace(e, this._intercept);
454
+ await this.$onError?.(e, this);
455
+ throw e;
456
+ }
36
457
  }
37
458
  }
@@ -0,0 +1,177 @@
1
+ import { ResourceNotAvailableError } from '@opra/common';
2
+ import { EntityMetadata } from '@sqb/connect';
3
+ import { SQBAdapter } from './sqb-adapter.js';
4
+ import { SqbEntityService } from './sqb-entity-service.js';
5
+ /**
6
+ * @class SqbSingletonService
7
+ * @template T - The data type class type of the resource
8
+ */
9
+ export class SqbSingletonService extends SqbEntityService {
10
+ /**
11
+ * Constructs a new instance
12
+ *
13
+ * @param {Type | string} dataType - The data type of the array elements.
14
+ * @param {SqbSingletonService.Options} [options] - The options for the array service.
15
+ * @constructor
16
+ */
17
+ constructor(dataType, options) {
18
+ super(dataType, options);
19
+ this.id = this.id || options?.id || 1;
20
+ }
21
+ /**
22
+ * Asserts the existence of a resource based on the given options.
23
+ *
24
+ * @returns {Promise<void>} A Promise that resolves when the resource exists.
25
+ * @throws {ResourceNotAvailableError} If the resource does not exist.
26
+ */
27
+ async assert(options) {
28
+ if (!(await this.exists(options)))
29
+ throw new ResourceNotAvailableError(this.getResourceName());
30
+ }
31
+ /**
32
+ * Inserts a single record into the database.
33
+ *
34
+ * @param {PartialDTO<T>} input - The input data
35
+ * @param {SqbSingletonService.CreateOptions} [options] - The options object
36
+ * @returns {Promise<PartialDTO<T>>} A promise that resolves to the created resource
37
+ * @throws {Error} if an unknown error occurs while creating the resource
38
+ */
39
+ async create(input, options) {
40
+ const info = {
41
+ crud: 'create',
42
+ method: 'create',
43
+ byId: false,
44
+ input,
45
+ options,
46
+ };
47
+ return this._intercept(async () => {
48
+ const primaryFields = EntityMetadata.getPrimaryIndexColumns(this.entityMetadata);
49
+ const data = { ...input };
50
+ if (primaryFields.length > 1) {
51
+ if (typeof primaryFields !== 'object') {
52
+ throw new TypeError(`"${this.entityMetadata.name}" should has multiple primary key fields. So you should provide and object that contains key fields`);
53
+ }
54
+ for (const field of primaryFields) {
55
+ data[field.name] = this.id[field.name];
56
+ }
57
+ }
58
+ else
59
+ data[primaryFields[0].name] = this.id;
60
+ return await this._create(data, options);
61
+ }, info);
62
+ }
63
+ /**
64
+ * Deletes the singleton record
65
+ *
66
+ * @param {SqbSingletonService.DeleteOptions} [options] - The options object
67
+ * @return {Promise<number>} - A Promise that resolves to the number of records deleted
68
+ */
69
+ async delete(options) {
70
+ const info = {
71
+ crud: 'delete',
72
+ method: 'delete',
73
+ byId: true,
74
+ documentId: this.id,
75
+ options,
76
+ };
77
+ return this._intercept(async () => {
78
+ const filter = SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
79
+ return this._delete(this.id, { ...options, filter });
80
+ }, info);
81
+ }
82
+ /**
83
+ * Checks if the singleton record exists.
84
+ *
85
+ * @param {SqbSingletonService.ExistsOptions} [options] - The options for the query (optional).
86
+ * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
87
+ */
88
+ async exists(options) {
89
+ const info = {
90
+ crud: 'read',
91
+ method: 'exists',
92
+ byId: true,
93
+ documentId: this.id,
94
+ options,
95
+ };
96
+ return this._intercept(async () => {
97
+ const filter = SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
98
+ return this._exists(this.id, { ...options, filter });
99
+ }, info);
100
+ }
101
+ /**
102
+ * Finds the singleton record. Returns `undefined` if not found
103
+ *
104
+ * @param {SqbSingletonService.FindOneOptions} options - The options for the query.
105
+ * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
106
+ */
107
+ async find(options) {
108
+ const info = {
109
+ crud: 'read',
110
+ method: 'findById',
111
+ byId: true,
112
+ documentId: this.id,
113
+ options,
114
+ };
115
+ return this._intercept(async () => {
116
+ const filter = SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
117
+ return this._findById(this.id, { ...options, filter });
118
+ }, info);
119
+ }
120
+ /**
121
+ * Retrieves the singleton record. Throws error if not found.
122
+ *
123
+ * @param {SqbSingletonService.FindOptions} [options] - Optional options for the `find` operation.
124
+ * @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
125
+ * or rejects with a ResourceNotFoundError if the document does not exist.
126
+ * @throws {ResourceNotAvailableError} - If the document does not exist.
127
+ */
128
+ async get(options) {
129
+ const out = await this.find(options);
130
+ if (!out)
131
+ throw new ResourceNotAvailableError(this.getResourceName());
132
+ return out;
133
+ }
134
+ /**
135
+ * Updates the singleton.
136
+ *
137
+ * @param {PatchDTO<T>} input - The partial input object containing the fields to update.
138
+ * @param {SqbSingletonService.UpdateOptions} [options] - The options for the update operation.
139
+ * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
140
+ * undefined if the document was not found.
141
+ */
142
+ async update(input, options) {
143
+ const info = {
144
+ crud: 'update',
145
+ method: 'update',
146
+ documentId: this.id,
147
+ byId: true,
148
+ input,
149
+ options,
150
+ };
151
+ return this._intercept(async () => {
152
+ const filter = SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
153
+ return this._update(this.id, input, { ...options, filter });
154
+ }, info);
155
+ }
156
+ /**
157
+ * Updates the singleton and returns updated record count
158
+ *
159
+ * @param {PatchDTO<T>} input - The partial input data to update the document with.
160
+ * @param {SqbSingletonService.UpdateOptions} options - The options for updating the document.
161
+ * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
162
+ */
163
+ async updateOnly(input, options) {
164
+ const info = {
165
+ crud: 'update',
166
+ method: 'update',
167
+ documentId: this.id,
168
+ byId: true,
169
+ input,
170
+ options,
171
+ };
172
+ return this._intercept(async () => {
173
+ const filter = SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
174
+ return this._updateOnly(this.id, input, { ...options, filter });
175
+ }, info);
176
+ }
177
+ }