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