@opra/sqb 1.26.2 → 1.26.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -1
- package/adapter-utils/prepare-filter.d.ts +3 -4
- package/adapter-utils/prepare-filter.js +3 -4
- package/augmentation/datatype-factory.augmentation.js +1 -1
- package/package.json +3 -3
- package/sqb-adapter.d.ts +41 -1
- package/sqb-adapter.js +21 -6
- package/sqb-collection-service.d.ts +141 -159
- package/sqb-collection-service.js +52 -99
- package/sqb-entity-service.d.ts +131 -153
- package/sqb-entity-service.js +123 -127
- package/sqb-service-base.d.ts +18 -14
- package/sqb-service-base.js +15 -17
- package/sqb-singleton-service.d.ts +78 -88
- package/sqb-singleton-service.js +26 -26
|
@@ -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
|
-
*
|
|
6
|
-
*
|
|
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
|
-
*
|
|
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
|
|
17
|
-
* @param
|
|
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
|
|
26
|
-
* Throws
|
|
25
|
+
* Asserts that a resource with the given ID exists.
|
|
26
|
+
* Throws {@link ResourceNotAvailableError} if it does not.
|
|
27
27
|
*
|
|
28
|
-
* @param
|
|
29
|
-
* @param
|
|
30
|
-
* @
|
|
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
|
|
51
|
-
* @param
|
|
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
|
|
63
|
+
* Returns the number of records matching the given options.
|
|
67
64
|
*
|
|
68
|
-
* @param
|
|
69
|
-
* @
|
|
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.
|
|
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
|
|
85
|
+
* Deletes the record with the given ID.
|
|
89
86
|
*
|
|
90
|
-
* @param
|
|
91
|
-
* @param
|
|
92
|
-
* @
|
|
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.
|
|
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
|
|
109
|
+
* Deletes all records matching the given options.
|
|
113
110
|
*
|
|
114
|
-
* @param
|
|
115
|
-
* @
|
|
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.
|
|
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
|
|
131
|
+
* Checks whether a record with the given ID exists.
|
|
135
132
|
*
|
|
136
|
-
* @param
|
|
137
|
-
* @param
|
|
138
|
-
* @
|
|
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.
|
|
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
|
|
155
|
+
* Checks whether any record matching the given options exists.
|
|
159
156
|
*
|
|
160
|
-
* @param
|
|
161
|
-
* @
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
259
|
+
* Updates a record by ID without returning it.
|
|
307
260
|
*
|
|
308
|
-
* @param
|
|
309
|
-
* @param
|
|
310
|
-
* @param
|
|
311
|
-
* @returns
|
|
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.
|
|
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
|
|
285
|
+
* Updates all records matching the given options.
|
|
333
286
|
*
|
|
334
|
-
* @param
|
|
335
|
-
* @param
|
|
336
|
-
* @
|
|
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.
|
|
300
|
+
const filter = SQBAdapter.prepareFilter([
|
|
348
301
|
await this._getCommonFilter(command),
|
|
349
302
|
command.options?.filter,
|
|
350
303
|
]);
|