@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.
@@ -2,33 +2,32 @@ import { ResourceNotAvailableError } from '@opra/common';
2
2
  import { SQBAdapter } from './sqb-adapter.js';
3
3
  import { SqbEntityService } from './sqb-entity-service.js';
4
4
  /**
5
- * @class SqbCollectionService
6
- * @template T - The data type class type of the resource
5
+ * Service for managing a collection of entities backed by an SQB data source.
6
+ *
7
+ * @typeParam T - The entity type managed by this service
7
8
  */
8
9
  export class SqbCollectionService extends SqbEntityService {
9
10
  /**
10
- * Represents default limit for findMany operation
11
+ * Default maximum number of records returned by `findMany`.
11
12
  */
12
13
  defaultLimit;
13
14
  /**
14
- * Constructs a new instance
15
+ * Constructs a new instance.
15
16
  *
16
- * @param {Type | string} dataType - The data type of the array elements.
17
- * @param {SqbCollectionService.Options} [options] - The options for the array service.
18
- * @constructor
17
+ * @param dataType - The data type of the collection elements.
18
+ * @param options - Options for the collection service.
19
19
  */
20
20
  constructor(dataType, options) {
21
21
  super(dataType, options);
22
22
  this.defaultLimit = options?.defaultLimit || 100;
23
23
  }
24
24
  /**
25
- * Asserts the existence of a resource with the given ID.
26
- * Throws a ResourceNotFoundError if the resource does not exist.
25
+ * Asserts that a resource with the given ID exists.
26
+ * Throws {@link ResourceNotAvailableError} if it does not.
27
27
  *
28
- * @param {SQBAdapter.IdOrIds} id - The ID of the resource to assert.
29
- * @param {SqbCollectionService.ExistsOptions} [options] - Optional options for checking the existence.
30
- * @returns {Promise<void>} - A Promise that resolves when the resource exists.
31
- * @throws {ResourceNotAvailableError} - If the resource does not exist.
28
+ * @param id - The ID of the resource to check.
29
+ * @param options - Optional existence check options.
30
+ * @throws {@link ResourceNotAvailableError} If the resource does not exist.
32
31
  */
33
32
  async assert(id, options) {
34
33
  if (!(await this.exists(id, options)))
@@ -45,12 +44,10 @@ export class SqbCollectionService extends SqbEntityService {
45
44
  return this._executeCommand(command, () => this._create(command));
46
45
  }
47
46
  /**
48
- * Creates a new resource
47
+ * Creates a new resource without returning it.
49
48
  *
50
- * @param {PartialDTO<T>} input - The input data
51
- * @param {SqbCollectionService.CreateOptions} [options] - The options object
52
- * @returns {Promise<void>} A promise that resolves create operation result
53
- * @throws {Error} if an unknown error occurs while creating the resource
49
+ * @param input - The input data for the new resource.
50
+ * @param options - Optional create options.
54
51
  */
55
52
  async createOnly(input, options) {
56
53
  const command = {
@@ -63,10 +60,10 @@ export class SqbCollectionService extends SqbEntityService {
63
60
  return this._executeCommand(command, () => this._createOnly(command));
64
61
  }
65
62
  /**
66
- * Returns the count of records based on the provided options
63
+ * Returns the number of records matching the given options.
67
64
  *
68
- * @param {SqbCollectionService.CountOptions} options - The options for the count operation.
69
- * @return {Promise<number>} - A promise that resolves to the count of records
65
+ * @param options - Options for the count operation.
66
+ * @returns The number of matching records.
70
67
  */
71
68
  async count(options) {
72
69
  const command = {
@@ -76,7 +73,7 @@ export class SqbCollectionService extends SqbEntityService {
76
73
  options,
77
74
  };
78
75
  return this._executeCommand(command, async () => {
79
- const filter = SQBAdapter.parseFilter([
76
+ const filter = SQBAdapter.prepareFilter([
80
77
  await this._getCommonFilter(command),
81
78
  command.options?.filter,
82
79
  ]);
@@ -85,11 +82,11 @@ export class SqbCollectionService extends SqbEntityService {
85
82
  });
86
83
  }
87
84
  /**
88
- * Deletes a record from the collection.
85
+ * Deletes the record with the given ID.
89
86
  *
90
- * @param {SQBAdapter.IdOrIds} id - The ID of the document to delete.
91
- * @param {SqbCollectionService.DeleteOptions} [options] - Optional delete options.
92
- * @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
87
+ * @param id - The ID of the record to delete.
88
+ * @param options - Optional delete options.
89
+ * @returns The number of records deleted.
93
90
  */
94
91
  async delete(id, options) {
95
92
  const command = {
@@ -100,7 +97,7 @@ export class SqbCollectionService extends SqbEntityService {
100
97
  options,
101
98
  };
102
99
  return this._executeCommand(command, async () => {
103
- const filter = SQBAdapter.parseFilter([
100
+ const filter = SQBAdapter.prepareFilter([
104
101
  await this._getCommonFilter(command),
105
102
  command.options?.filter,
106
103
  ]);
@@ -109,10 +106,10 @@ export class SqbCollectionService extends SqbEntityService {
109
106
  });
110
107
  }
111
108
  /**
112
- * Deletes multiple documents from the collection that meet the specified filter criteria.
109
+ * Deletes all records matching the given options.
113
110
  *
114
- * @param {SqbCollectionService.DeleteManyOptions} options - The options for the delete operation.
115
- * @return {Promise<number>} - A promise that resolves to the number of documents deleted.
111
+ * @param options - Options including filter criteria.
112
+ * @returns The number of records deleted.
116
113
  */
117
114
  async deleteMany(options) {
118
115
  const command = {
@@ -122,7 +119,7 @@ export class SqbCollectionService extends SqbEntityService {
122
119
  options,
123
120
  };
124
121
  return this._executeCommand(command, async () => {
125
- const filter = SQBAdapter.parseFilter([
122
+ const filter = SQBAdapter.prepareFilter([
126
123
  await this._getCommonFilter(command),
127
124
  command.options?.filter,
128
125
  ]);
@@ -131,11 +128,11 @@ export class SqbCollectionService extends SqbEntityService {
131
128
  });
132
129
  }
133
130
  /**
134
- * Checks if a record with the given id exists.
131
+ * Checks whether a record with the given ID exists.
135
132
  *
136
- * @param {SQBAdapter.IdOrIds} id - The id of the object to check.
137
- * @param {SqbCollectionService.ExistsOptions} [options] - The options for the query (optional).
138
- * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
133
+ * @param id - The ID to check.
134
+ * @param options - Optional query options.
135
+ * @returns `true` if the record exists, `false` otherwise.
139
136
  */
140
137
  async exists(id, options) {
141
138
  const command = {
@@ -146,7 +143,7 @@ export class SqbCollectionService extends SqbEntityService {
146
143
  options,
147
144
  };
148
145
  return this._executeCommand(command, async () => {
149
- const filter = SQBAdapter.parseFilter([
146
+ const filter = SQBAdapter.prepareFilter([
150
147
  await this._getCommonFilter(command),
151
148
  command.options?.filter,
152
149
  ]);
@@ -155,10 +152,10 @@ export class SqbCollectionService extends SqbEntityService {
155
152
  });
156
153
  }
157
154
  /**
158
- * Checks if a record with the given arguments exists.
155
+ * Checks whether any record matching the given options exists.
159
156
  *
160
- * @param {SqbCollectionService.ExistsOneOptions} [options] - The options for the query (optional).
161
- * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
157
+ * @param options - Optional query options.
158
+ * @returns `true` if at least one matching record exists, `false` otherwise.
162
159
  */
163
160
  async existsOne(options) {
164
161
  const command = {
@@ -168,7 +165,7 @@ export class SqbCollectionService extends SqbEntityService {
168
165
  options,
169
166
  };
170
167
  return this._executeCommand(command, async () => {
171
- const filter = SQBAdapter.parseFilter([
168
+ const filter = SQBAdapter.prepareFilter([
172
169
  await this._getCommonFilter(command),
173
170
  command.options?.filter,
174
171
  ]);
@@ -176,13 +173,6 @@ export class SqbCollectionService extends SqbEntityService {
176
173
  return this._existsOne(command);
177
174
  });
178
175
  }
179
- /**
180
- * Finds a record by ID.
181
- *
182
- * @param {SQBAdapter.IdOrIds} id - The ID of the record.
183
- * @param {SqbCollectionService.FindOneOptions} [options] - The options for the find query.
184
- * @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
185
- */
186
176
  async findById(id, options) {
187
177
  const command = {
188
178
  crud: 'read',
@@ -193,7 +183,7 @@ export class SqbCollectionService extends SqbEntityService {
193
183
  };
194
184
  return this._executeCommand(command, async () => {
195
185
  const documentFilter = await this._getCommonFilter(command);
196
- const filter = SQBAdapter.parseFilter([
186
+ const filter = SQBAdapter.prepareFilter([
197
187
  documentFilter,
198
188
  command.options?.filter,
199
189
  ]);
@@ -201,12 +191,6 @@ export class SqbCollectionService extends SqbEntityService {
201
191
  return this._findById(command);
202
192
  });
203
193
  }
204
- /**
205
- * Finds a record in the collection that matches the specified options.
206
- *
207
- * @param {SqbCollectionService.FindOneOptions} options - The options for the query.
208
- * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
209
- */
210
194
  async findOne(options) {
211
195
  const command = {
212
196
  crud: 'read',
@@ -215,7 +199,7 @@ export class SqbCollectionService extends SqbEntityService {
215
199
  options,
216
200
  };
217
201
  return this._executeCommand(command, async () => {
218
- const filter = SQBAdapter.parseFilter([
202
+ const filter = SQBAdapter.prepareFilter([
219
203
  await this._getCommonFilter(command),
220
204
  command.options?.filter,
221
205
  ]);
@@ -223,12 +207,6 @@ export class SqbCollectionService extends SqbEntityService {
223
207
  return this._findOne(command);
224
208
  });
225
209
  }
226
- /**
227
- * Finds multiple records in collection.
228
- *
229
- * @param {SqbCollectionService.FindManyOptions} options - The options for the find operation.
230
- * @return A Promise that resolves to an array of partial outputs of type T.
231
- */
232
210
  async findMany(options) {
233
211
  const command = {
234
212
  crud: 'read',
@@ -237,7 +215,7 @@ export class SqbCollectionService extends SqbEntityService {
237
215
  options,
238
216
  };
239
217
  return this._executeCommand(command, async () => {
240
- const filter = SQBAdapter.parseFilter([
218
+ const filter = SQBAdapter.prepareFilter([
241
219
  await this._getCommonFilter(command),
242
220
  command.options?.filter,
243
221
  ]);
@@ -246,13 +224,6 @@ export class SqbCollectionService extends SqbEntityService {
246
224
  return this._findMany(command);
247
225
  });
248
226
  }
249
- /**
250
- * Finds multiple records in the collection and returns both records (max limit)
251
- * and total count that matched the given criteria
252
- *
253
- * @param {SqbCollectionService.FindManyOptions} options - The options for the find operation.
254
- * @return A Promise that resolves to an array of partial outputs of type T and total count.
255
- */
256
227
  async findManyWithCount(options) {
257
228
  const [items, count] = await Promise.all([
258
229
  this.findMany(options),
@@ -260,30 +231,12 @@ export class SqbCollectionService extends SqbEntityService {
260
231
  ]);
261
232
  return { count, items };
262
233
  }
263
- /**
264
- * Retrieves a records from the collection by its ID. Throws error if not found.
265
- *
266
- * @param {SQBAdapter.IdOrIds} id - The ID of the document to retrieve.
267
- * @param {SqbCollectionService.FindOneOptions} [options] - Optional options for the findOne operation.
268
- * @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
269
- * or rejects with a ResourceNotFoundError if the document does not exist.
270
- * @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
271
- */
272
234
  async get(id, options) {
273
235
  const out = await this.findById(id, options);
274
236
  if (!out)
275
237
  throw new ResourceNotAvailableError(this.getResourceName(), id);
276
238
  return out;
277
239
  }
278
- /**
279
- * Updates a record with the given id in the collection.
280
- *
281
- * @param {SQBAdapter.IdOrIds} id - The id of the document to update.
282
- * @param {PatchDTO<T>} input - The partial input object containing the fields to update.
283
- * @param {SqbCollectionService.UpdateOptions} [options] - The options for the update operation.
284
- * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
285
- * undefined if the document was not found.
286
- */
287
240
  async update(id, input, options) {
288
241
  const command = {
289
242
  crud: 'update',
@@ -294,7 +247,7 @@ export class SqbCollectionService extends SqbEntityService {
294
247
  options,
295
248
  };
296
249
  return this._executeCommand(command, async () => {
297
- const filter = SQBAdapter.parseFilter([
250
+ const filter = SQBAdapter.prepareFilter([
298
251
  await this._getCommonFilter(command),
299
252
  command.options?.filter,
300
253
  ]);
@@ -303,12 +256,12 @@ export class SqbCollectionService extends SqbEntityService {
303
256
  });
304
257
  }
305
258
  /**
306
- * Updates a record in the collection with the specified ID and returns updated record count
259
+ * Updates a record by ID without returning it.
307
260
  *
308
- * @param {any} id - The ID of the document to update.
309
- * @param {PatchDTO<T>} input - The partial input data to update the document with.
310
- * @param {SqbCollectionService.UpdateOptions} options - The options for updating the document.
311
- * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
261
+ * @param id - The ID of the record to update.
262
+ * @param input - The fields to update.
263
+ * @param options - Optional update options.
264
+ * @returns The number of records modified.
312
265
  */
313
266
  async updateOnly(id, input, options) {
314
267
  const command = {
@@ -320,7 +273,7 @@ export class SqbCollectionService extends SqbEntityService {
320
273
  options,
321
274
  };
322
275
  return this._executeCommand(command, async () => {
323
- const filter = SQBAdapter.parseFilter([
276
+ const filter = SQBAdapter.prepareFilter([
324
277
  await this._getCommonFilter(command),
325
278
  command.options?.filter,
326
279
  ]);
@@ -329,11 +282,11 @@ export class SqbCollectionService extends SqbEntityService {
329
282
  });
330
283
  }
331
284
  /**
332
- * Updates multiple records in the collection based on the specified input and options.
285
+ * Updates all records matching the given options.
333
286
  *
334
- * @param {PatchDTO<T>} input - The partial input to update the documents with.
335
- * @param {SqbCollectionService.UpdateManyOptions} options - The options for updating the documents.
336
- * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
287
+ * @param input - The fields to update.
288
+ * @param options - Options including filter criteria.
289
+ * @returns The number of records modified.
337
290
  */
338
291
  async updateMany(input, options) {
339
292
  const command = {
@@ -344,7 +297,7 @@ export class SqbCollectionService extends SqbEntityService {
344
297
  options,
345
298
  };
346
299
  return this._executeCommand(command, async () => {
347
- const filter = SQBAdapter.parseFilter([
300
+ const filter = SQBAdapter.prepareFilter([
348
301
  await this._getCommonFilter(command),
349
302
  command.options?.filter,
350
303
  ]);