@opra/sqb 1.26.3 → 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.
@@ -6,17 +6,41 @@ import { type vg } from 'valgen';
6
6
  import { SQBAdapter } from './sqb-adapter.js';
7
7
  import { SqbServiceBase } from './sqb-service-base.js';
8
8
  /**
9
- * @namespace SqbEntityService
9
+ * Namespace containing types and options for SqbEntityService.
10
10
  */
11
11
  export declare namespace SqbEntityService {
12
+ /**
13
+ * Configuration options for SqbEntityService.
14
+ */
12
15
  interface Options extends SqbServiceBase.Options {
16
+ /**
17
+ * The name of the resource managed by this service.
18
+ */
13
19
  resourceName?: SqbEntityService<any>['resourceName'];
20
+ /**
21
+ * Optional error handler.
22
+ */
14
23
  onError?: SqbEntityService<any>['onError'];
24
+ /**
25
+ * Optional common filter applied to all read/write operations.
26
+ */
15
27
  commonFilter?: SqbEntityService<any>['commonFilter'];
28
+ /**
29
+ * Optional interceptor for the service operations.
30
+ */
16
31
  interceptor?: SqbEntityService<any>['interceptor'];
32
+ /**
33
+ * Optional scope for the service.
34
+ */
17
35
  scope?: SqbEntityService<any>['scope'];
18
36
  }
37
+ /**
38
+ * Represents the CRUD operation types.
39
+ */
19
40
  type CrudOp = 'create' | 'read' | 'update' | 'delete';
41
+ /**
42
+ * Information about the command being executed.
43
+ */
20
44
  interface CommandInfo {
21
45
  crud: SqbEntityService.CrudOp;
22
46
  method: string;
@@ -25,77 +49,35 @@ export declare namespace SqbEntityService {
25
49
  input?: Record<string, any>;
26
50
  options?: Record<string, any>;
27
51
  }
28
- type CommonFilter = SQBAdapter.FilterInput | ((args: SqbEntityService.CommandInfo, _this: SqbEntityService<any>) => SQBAdapter.FilterInput | Promise<SQBAdapter.FilterInput> | undefined);
29
52
  /**
30
- * Represents options for "create" operation
31
- *
32
- * @interface
53
+ * Type definition for a common filter.
33
54
  */
55
+ type CommonFilter = SQBAdapter.FilterInput | ((args: SqbEntityService.CommandInfo, _this: SqbEntityService<any>) => SQBAdapter.FilterInput | Promise<SQBAdapter.FilterInput> | undefined);
34
56
  interface CreateOptions extends Repository.CreateOptions {
35
57
  }
36
- /**
37
- * Represents options for "count" operation
38
- *
39
- * @interface
40
- */
41
58
  interface CountOptions extends StrictOmit<Repository.CountOptions, 'filter'> {
42
59
  filter?: Repository.CountOptions['filter'] | string;
43
60
  }
44
- /**
45
- * Represents options for "delete" operation
46
- *
47
- * @interface
48
- */
49
61
  interface DeleteOptions extends StrictOmit<Repository.DeleteOptions, 'filter'> {
50
62
  filter?: Repository.DeleteOptions['filter'] | string;
51
63
  }
52
- /**
53
- * Represents options for "deleteMany" operation
54
- *
55
- * @interface
56
- */
57
64
  interface DeleteManyOptions extends StrictOmit<Repository.DeleteManyOptions, 'filter'> {
58
65
  filter?: Repository.DeleteManyOptions['filter'] | string;
59
66
  }
60
- /**
61
- * Represents options for "exists" operation
62
- *
63
- * @interface
64
- */
65
67
  interface ExistsOptions extends StrictOmit<Repository.ExistsOptions, 'filter'> {
66
68
  filter?: Repository.ExistsOptions['filter'] | string;
67
69
  }
68
- /**
69
- * Represents options for "findOne" operation
70
- *
71
- * @interface
72
- */
73
70
  interface FindOneOptions extends StrictOmit<Repository.FindOneOptions, 'filter' | 'offset'> {
74
71
  filter?: Repository.FindOneOptions['filter'] | string;
75
72
  skip?: number;
76
73
  }
77
- /**
78
- * Represents options for "findMany" operation
79
- *
80
- * @interface
81
- */
82
74
  interface FindManyOptions extends StrictOmit<Repository.FindManyOptions, 'filter' | 'offset'> {
83
75
  filter?: Repository.FindManyOptions['filter'] | string;
84
76
  skip?: number;
85
77
  }
86
- /**
87
- * Represents options for "update" operation
88
- *
89
- * @interface
90
- */
91
78
  interface UpdateOneOptions extends StrictOmit<Repository.UpdateOptions, 'filter'> {
92
79
  filter?: Repository.UpdateOptions['filter'] | string;
93
80
  }
94
- /**
95
- * Represents options for "updateMany" operation
96
- *
97
- * @interface
98
- */
99
81
  interface UpdateManyOptions extends StrictOmit<Repository.UpdateManyOptions, 'filter'> {
100
82
  filter?: Repository.UpdateManyOptions['filter'] | string;
101
83
  }
@@ -141,18 +123,19 @@ export declare namespace SqbEntityService {
141
123
  }
142
124
  export interface SqbEntityService {
143
125
  /**
144
- * Interceptor function for handling callback execution with provided arguments.
145
- * @type Function
146
- * @param next - The callback function to be intercepted.
147
- * @param {SqbEntityService.CommandInfo} command - The arguments object containing the following properties:
148
- * @param _this - The reference to the current object.
149
- * @returns - The promise that resolves to the result of the callback execution.
126
+ * Optional interceptor that wraps every command execution.
127
+ *
128
+ * @param next - Calls the next interceptor or the actual command handler.
129
+ * @param command - Metadata describing the current operation.
130
+ * @param _this - Reference to the service instance.
131
+ * @returns The result of the command execution.
150
132
  */
151
133
  interceptor?(next: () => any, command: SqbEntityService.CommandInfo, _this: any): Promise<any>;
152
134
  }
153
135
  /**
154
- * @class SqbEntityService
155
- * @template T - The data type class type of the resource
136
+ * Base service providing CRUD operations over an SQB entity.
137
+ *
138
+ * @typeParam T - The entity type managed by this service
156
139
  */
157
140
  export declare class SqbEntityService<T extends object = object> extends SqbServiceBase {
158
141
  protected _dataTypeScope?: string;
@@ -163,278 +146,273 @@ export declare class SqbEntityService<T extends object = object> extends SqbServ
163
146
  protected _inputCodecs: Record<string, vg.isObject.Validator<T>>;
164
147
  protected _outputCodecs: Record<string, vg.isObject.Validator<T>>;
165
148
  /**
166
- * Defines comma-delimited scopes for the api document
149
+ * Comma-delimited scopes used to filter the API document.
167
150
  */
168
151
  scope?: string;
169
152
  /**
170
- * Represents the name of a resource.
171
- * @type {string}
153
+ * Override for the resource name exposed in error messages and API metadata.
154
+ * Accepts a static string or a function that returns one.
172
155
  */
173
156
  resourceName?: string | ((_this: this) => string);
174
157
  /**
175
- * Represents a common filter function for a service.
176
- *
177
- * @type {SqbEntityService.Filter | Function}
158
+ * Filter(s) automatically applied to every query for this service.
159
+ * Useful for multi-tenant isolation or other cross-cutting constraints.
178
160
  */
179
161
  commonFilter?: SqbEntityService.CommonFilter | SqbEntityService.CommonFilter[];
180
162
  /**
181
- * Callback function for handling errors.
163
+ * Called whenever a command throws. Useful for logging or transforming errors.
182
164
  *
183
- * @param {unknown} error - The error object.
184
- * @param {SqbEntityService} _this - The context object.
165
+ * @param error - The thrown error.
166
+ * @param _this - The service instance.
185
167
  */
186
168
  onError?: (error: unknown, _this: any) => void | Promise<void>;
187
169
  /**
188
- * Constructs a new instance
170
+ * Constructs a new instance.
189
171
  *
190
- * @param dataType - The data type of the returning results
191
- * @param [options] - The options for the service.
192
- * @constructor
172
+ * @param dataType - The entity class or its registered name.
173
+ * @param options - Options for the service.
193
174
  */
194
175
  constructor(dataType: Type<T> | string, options?: SqbEntityService.Options);
195
176
  /**
196
- * Retrieves the OPRA data type
177
+ * Returns the resolved OPRA `ComplexType` for this service's entity.
197
178
  *
198
- * @throws {NotAcceptableError} If the data type is not a ComplexType.
179
+ * @throws If the data type is not registered as a `ComplexType`.
199
180
  */
200
181
  get dataType(): ComplexType;
201
182
  /**
202
- * Retrieves the Class of the data type
183
+ * Returns the constructor class of the entity data type.
203
184
  *
204
- * @throws {NotAcceptableError} If the data type is not a ComplexType.
185
+ * @throws If the data type is not registered as a `ComplexType`.
205
186
  */
206
187
  get dataTypeClass(): Type;
207
188
  /**
208
- * Retrieves the SQB entity metadata object
189
+ * Returns the SQB `EntityMetadata` for the entity class.
209
190
  *
210
- * @throws {TypeError} If metadata is not available
191
+ * @throws If the class is not decorated with `@Entity()`.
211
192
  */
212
193
  get entityMetadata(): EntityMetadata;
213
194
  for<C extends ExecutionContext, P extends Partial<this>>(context: C | ServiceBase, overwriteProperties?: Nullish<P>, overwriteContext?: Partial<C>): this & Required<P>;
214
195
  /**
215
- * Retrieves the resource name.
196
+ * Returns the resource name used in error messages and API metadata.
216
197
  *
217
- * @returns {string} The resource name.
218
- * @throws {Error} If the collection name is not defined.
198
+ * @throws If neither `resourceName` nor the data type name is available.
219
199
  */
220
200
  getResourceName(): string;
221
201
  /**
222
- * Retrieves the codec for the specified operation.
202
+ * Returns the input codec for the given operation (e.g. `'create'`, `'update'`).
223
203
  *
224
- * @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
204
+ * @param operation - The operation name.
225
205
  */
226
206
  getInputCodec(operation: string): vg.isObject.Validator<T>;
227
207
  /**
228
- * Retrieves the codec.
208
+ * Returns the output codec for the given operation.
209
+ *
210
+ * @param operation - The operation name.
229
211
  */
230
212
  getOutputCodec(operation: string): vg.isObject.Validator<T>;
231
213
  /**
232
- * Insert a new record into database
214
+ * Inserts a new record into the database and returns the created document.
233
215
  *
234
- * @param command
235
- * @returns - A promise that resolves to the created resource
216
+ * @param command - The create command.
217
+ * @returns The created document.
236
218
  * @protected
237
219
  */
238
220
  protected _create(command: SqbEntityService.CreateCommand<T>): Promise<PartialDTO<T>>;
239
221
  /**
240
- * Insert a new record into database
222
+ * Inserts a new record into the database without returning it.
241
223
  *
242
- * @param command
243
- * @returns - A promise that resolves to the created resource
224
+ * @param command - The create command.
244
225
  * @protected
245
226
  */
246
227
  protected _createOnly(command: SqbEntityService.CreateCommand<T>): Promise<any>;
247
228
  /**
248
- * Returns the count of records based on the provided options
229
+ * Returns the count of records matching the command options.
249
230
  *
250
- * @param command
251
- * @return - A promise that resolves to the count of records
231
+ * @param command - The count command.
252
232
  * @protected
253
233
  */
254
234
  protected _count(command: SqbEntityService.CountCommand): Promise<number>;
255
235
  /**
256
- * Deletes a record from the collection.
236
+ * Deletes the record identified by `command.documentId`.
257
237
  *
258
- * @param command
259
- * @return - A Promise that resolves to the number of documents deleted.
238
+ * @param command - The delete command.
239
+ * @returns The number of records deleted.
260
240
  * @protected
261
241
  */
262
242
  protected _delete(command: SqbEntityService.DeleteOneCommand): Promise<number>;
263
243
  /**
264
- * Deletes multiple documents from the collection that meet the specified filter criteria.
244
+ * Deletes all records matching the command filter.
265
245
  *
266
- * @param command
267
- * @return - A promise that resolves to the number of documents deleted.
246
+ * @param command - The deleteMany command.
247
+ * @returns The number of records deleted.
268
248
  * @protected
269
249
  */
270
250
  protected _deleteMany(command: SqbEntityService.DeleteManyCommand): Promise<number>;
271
251
  /**
272
- * Checks if a record with the given id exists.
252
+ * Checks whether the record identified by `command.documentId` exists.
273
253
  *
274
- * @param command
254
+ * @param command - The exists command.
275
255
  * @protected
276
256
  */
277
257
  protected _exists(command: SqbEntityService.ExistsCommand): Promise<boolean>;
278
258
  /**
279
- * Checks if a record with the given arguments exists.
259
+ * Checks whether any record matching the command filter exists.
280
260
  *
281
- * @param command
282
- * @return - A Promise that resolves to a boolean indicating whether the record exists or not.
261
+ * @param command - The existsOne command.
283
262
  * @protected
284
263
  */
285
264
  protected _existsOne(command: SqbEntityService.ExistsCommand): Promise<boolean>;
286
265
  /**
287
- * Finds a record by ID.
266
+ * Finds the record identified by `command.documentId`.
288
267
  *
289
- * @param command
290
- * @return - A promise resolving to the found document, or undefined if not found.
268
+ * @param command - The findById command.
269
+ * @returns The found record, or `undefined` if not found.
291
270
  * @protected
292
271
  */
293
272
  protected _findById(command: SqbEntityService.FindOneCommand): Promise<PartialDTO<T> | undefined>;
294
273
  /**
295
- * Finds a record in the collection that matches the specified options.
274
+ * Finds the first record matching the command filter.
296
275
  *
297
- * @param command
298
- * @return - A promise that resolves with the found document or undefined if no document is found.
276
+ * @param command - The findOne command.
277
+ * @returns The found record, or `undefined` if not found.
299
278
  * @protected
300
279
  */
301
280
  protected _findOne(command: SqbEntityService.FindOneCommand): Promise<PartialDTO<T> | undefined>;
302
281
  /**
303
- * Finds multiple records in collection.
282
+ * Finds all records matching the command filter.
304
283
  *
305
- * @param command
306
- * @return - A Promise that resolves to an array of partial outputs of type T.
284
+ * @param command - The findMany command.
285
+ * @returns An array of matching records.
307
286
  * @protected
308
287
  */
309
288
  protected _findMany(command: SqbEntityService.FindManyCommand): Promise<PartialDTO<T>[]>;
310
289
  /**
311
- * Updates a record with the given id in the collection.
290
+ * Updates the record identified by `command.documentId` and returns it.
312
291
  *
313
- * @param command
314
- * @returns A promise that resolves to the updated document or undefined if the document was not found.
292
+ * @param command - The update command.
293
+ * @returns The updated record, or `undefined` if not found.
315
294
  * @protected
316
295
  */
317
296
  protected _update(command: SqbEntityService.UpdateOneCommand<T>): Promise<PartialDTO<T> | undefined>;
318
297
  /**
319
- * Updates a record in the collection with the specified ID and returns updated record count
298
+ * Updates the record identified by `command.documentId` without returning it.
320
299
  *
321
- * @param command
322
- * @returns - A promise that resolves to the number of documents modified.
300
+ * @param command - The updateOnly command.
301
+ * @returns The number of records modified.
323
302
  * @protected
324
303
  */
325
304
  protected _updateOnly(command: SqbEntityService.UpdateOneCommand<T>): Promise<number>;
326
305
  /**
327
- * Updates multiple records in the collection based on the specified input and options.
306
+ * Updates all records matching the command filter.
328
307
  *
329
- * @param command
330
- * @return - A promise that resolves to the number of documents matched and modified.
308
+ * @param command - The updateMany command.
309
+ * @returns The number of records modified.
331
310
  * @protected
332
311
  */
333
312
  protected _updateMany(command: SqbEntityService.UpdateOneCommand<T>): Promise<number>;
334
313
  /**
335
- * Acquires a connection and performs Repository.create operation
314
+ * Acquires a connection and performs `Repository.create`.
336
315
  *
337
- * @param input - The document to insert
338
- * @param options - Optional settings for the command
316
+ * @param input - The document to insert.
317
+ * @param options - Optional settings.
339
318
  * @protected
340
319
  */
341
320
  protected _dbCreate(input: PartialDTO<T>, options?: Repository.CreateOptions): Promise<PartialDTO<T>>;
342
321
  /**
343
- * Acquires a connection and performs Repository.count operation
322
+ * Acquires a connection and performs `Repository.count`.
344
323
  *
345
- * @param options - The options for counting documents.
324
+ * @param options - Optional settings.
346
325
  * @protected
347
326
  */
348
327
  protected _dbCount(options?: Repository.CountOptions): Promise<number>;
349
328
  /**
350
- * Acquires a connection and performs Repository.delete operation
329
+ * Acquires a connection and performs `Repository.delete`.
351
330
  *
352
- * @param id - Value of the key field used to select the record
353
- * @param options - Optional settings for the command
331
+ * @param id - The key field value identifying the record.
332
+ * @param options - Optional settings.
354
333
  * @protected
355
334
  */
356
335
  protected _dbDelete(id: SQBAdapter.IdOrIds, options?: Repository.DeleteOptions): Promise<number>;
357
336
  /**
358
- * Acquires a connection and performs Repository.deleteMany operation
337
+ * Acquires a connection and performs `Repository.deleteMany`.
359
338
  *
360
- * @param options - Optional settings for the command
339
+ * @param options - Optional settings.
361
340
  * @protected
362
341
  */
363
342
  protected _dbDeleteMany(options?: Repository.DeleteManyOptions): Promise<number>;
364
343
  /**
365
- * Acquires a connection and performs Repository.exists operation
344
+ * Acquires a connection and performs `Repository.exists`.
366
345
  *
367
- * @param id - Value of the key field used to select the record
368
- * @param options - Optional settings for the command
346
+ * @param id - The key field value identifying the record.
347
+ * @param options - Optional settings.
369
348
  * @protected
370
349
  */
371
350
  protected _dbExists(id: SQBAdapter.IdOrIds, options?: Repository.ExistsOptions): Promise<boolean>;
372
351
  /**
373
- * Acquires a connection and performs Repository.existsOne operation
352
+ * Acquires a connection and performs `Repository.existsOne`.
374
353
  *
375
- * @param options - Optional settings for the command
354
+ * @param options - Optional settings.
376
355
  * @protected
377
356
  */
378
357
  protected _dbExistsOne(options?: Repository.ExistsOptions): Promise<boolean>;
379
358
  /**
380
- * Acquires a connection and performs Repository.findById operation
359
+ * Acquires a connection and performs `Repository.findById`.
381
360
  *
382
- * @param id - Value of the key field used to select the record
383
- * @param options - Optional settings for the command
361
+ * @param id - The key field value identifying the record.
362
+ * @param options - Optional settings.
384
363
  * @protected
385
364
  */
386
365
  protected _dbFindById(id: SQBAdapter.IdOrIds, options?: Repository.FindOptions): Promise<PartialDTO<T> | undefined>;
387
366
  /**
388
- * Acquires a connection and performs Repository.findOne operation
367
+ * Acquires a connection and performs `Repository.findOne`.
389
368
  *
390
- * @param options - Optional settings for the command
369
+ * @param options - Optional settings.
391
370
  * @protected
392
371
  */
393
372
  protected _dbFindOne(options?: StrictOmit<Repository.FindOneOptions, 'offset'> & {
394
373
  skip?: number;
395
374
  }): Promise<PartialDTO<T> | undefined>;
396
375
  /**
397
- * Acquires a connection and performs Repository.findMany operation
376
+ * Acquires a connection and performs `Repository.findMany`.
398
377
  *
399
- * @param options - Optional settings for the command
378
+ * @param options - Optional settings.
400
379
  * @protected
401
380
  */
402
381
  protected _dbFindMany(options?: StrictOmit<Repository.FindManyOptions, 'offset'> & {
403
382
  skip?: number;
404
383
  }): Promise<PartialDTO<T>[]>;
405
384
  /**
406
- * Acquires a connection and performs Repository.update operation
385
+ * Acquires a connection and performs `Repository.update`.
407
386
  *
408
- * @param id - Value of the key field used to select the record
409
- * @param data - The update values to be applied to the document
410
- * @param options - Optional settings for the command
387
+ * @param id - The key field value identifying the record.
388
+ * @param data - The update values.
389
+ * @param options - Optional settings.
411
390
  * @protected
412
391
  */
413
392
  protected _dbUpdate(id: SQBAdapter.IdOrIds, data: PatchDTO<T>, options?: Repository.UpdateOptions): Promise<PartialDTO<T> | undefined>;
414
393
  /**
415
- * Acquires a connection and performs Repository.updateOnly operation
394
+ * Acquires a connection and performs `Repository.updateOnly`.
416
395
  *
417
- * @param id - Value of the key field used to select the record
418
- * @param data - The update values to be applied to the document
419
- * @param options - Optional settings for the command
396
+ * @param id - The key field value identifying the record.
397
+ * @param data - The update values.
398
+ * @param options - Optional settings.
420
399
  * @protected
421
400
  */
422
401
  protected _dbUpdateOnly(id: SQBAdapter.IdOrIds, data: PatchDTO<T>, options?: Repository.UpdateOptions): Promise<number>;
423
402
  /**
424
- * Acquires a connection and performs Repository.updateMany operation
403
+ * Acquires a connection and performs `Repository.updateMany`.
425
404
  *
426
- * @param data - The update values to be applied to the document
427
- * @param options - Optional settings for the command
405
+ * @param data - The update values.
406
+ * @param options - Optional settings.
428
407
  * @protected
429
408
  */
430
409
  protected _dbUpdateMany(data: PatchDTO<T>, options?: Repository.UpdateManyOptions): Promise<number>;
431
410
  /**
432
- * Retrieves the common filter used for querying documents.
433
- * This method is mostly used for security issues like securing multi-tenant applications.
411
+ * Builds the common filter for the given command.
412
+ * Used primarily for multi-tenant isolation and similar cross-cutting concerns.
434
413
  *
435
414
  * @protected
436
- * @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
437
- * that resolves to the common filter, or undefined if not available.
415
+ * @returns The resolved filter input, or `undefined` if none is configured.
438
416
  */
439
417
  protected _getCommonFilter(command: SqbEntityService.CommandInfo): SQBAdapter.FilterInput | Promise<SQBAdapter.FilterInput> | undefined;
440
418
  protected _executeCommand(command: SqbEntityService.CommandInfo, commandFn: () => any): Promise<any>;