@opra/sqb 1.0.0-alpha.9 → 1.0.0-beta.2
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/cjs/adapter-utils/parse-filter.js +5 -5
- package/cjs/sqb-adapter.js +4 -4
- package/cjs/sqb-collection-service.js +85 -63
- package/cjs/sqb-entity-service.js +190 -74
- package/cjs/sqb-singleton-service.js +37 -61
- package/esm/adapter-utils/parse-filter.js +4 -4
- package/esm/package.json +3 -0
- package/esm/sqb-adapter.js +4 -4
- package/esm/sqb-collection-service.js +85 -63
- package/esm/sqb-entity-service.js +190 -74
- package/esm/sqb-singleton-service.js +37 -61
- package/package.json +27 -37
- package/types/index.d.cts +7 -0
- package/types/sqb-collection-service.d.ts +20 -10
- package/types/sqb-entity-service.d.ts +120 -92
- package/types/sqb-singleton-service.d.ts +13 -11
|
@@ -30,23 +30,33 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
30
30
|
if (!(await this.exists(id, options)))
|
|
31
31
|
throw new ResourceNotAvailableError(this.getResourceName(), id);
|
|
32
32
|
}
|
|
33
|
+
async create(input, options) {
|
|
34
|
+
const command = {
|
|
35
|
+
crud: 'create',
|
|
36
|
+
method: 'create',
|
|
37
|
+
byId: false,
|
|
38
|
+
input,
|
|
39
|
+
options,
|
|
40
|
+
};
|
|
41
|
+
return this._executeCommand(command, () => this._create(command));
|
|
42
|
+
}
|
|
33
43
|
/**
|
|
34
44
|
* Creates a new resource
|
|
35
45
|
*
|
|
36
46
|
* @param {PartialDTO<T>} input - The input data
|
|
37
47
|
* @param {SqbCollectionService.CreateOptions} [options] - The options object
|
|
38
|
-
* @returns {Promise<
|
|
48
|
+
* @returns {Promise<void>} A promise that resolves create operation result
|
|
39
49
|
* @throws {Error} if an unknown error occurs while creating the resource
|
|
40
50
|
*/
|
|
41
|
-
async
|
|
42
|
-
const
|
|
51
|
+
async createOnly(input, options) {
|
|
52
|
+
const command = {
|
|
43
53
|
crud: 'create',
|
|
44
|
-
method: '
|
|
54
|
+
method: 'createOnly',
|
|
45
55
|
byId: false,
|
|
46
56
|
input,
|
|
47
57
|
options,
|
|
48
58
|
};
|
|
49
|
-
return this.
|
|
59
|
+
return this._executeCommand(command, () => this._createOnly(command));
|
|
50
60
|
}
|
|
51
61
|
/**
|
|
52
62
|
* Returns the count of records based on the provided options
|
|
@@ -55,16 +65,17 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
55
65
|
* @return {Promise<number>} - A promise that resolves to the count of records
|
|
56
66
|
*/
|
|
57
67
|
async count(options) {
|
|
58
|
-
const
|
|
68
|
+
const command = {
|
|
59
69
|
crud: 'read',
|
|
60
70
|
method: 'count',
|
|
61
71
|
byId: false,
|
|
62
72
|
options,
|
|
63
73
|
};
|
|
64
|
-
return this.
|
|
65
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
return this._executeCommand(command, async () => {
|
|
75
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
76
|
+
command.options = { ...command.options, filter };
|
|
77
|
+
return this._count(command);
|
|
78
|
+
});
|
|
68
79
|
}
|
|
69
80
|
/**
|
|
70
81
|
* Deletes a record from the collection.
|
|
@@ -74,17 +85,18 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
74
85
|
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
75
86
|
*/
|
|
76
87
|
async delete(id, options) {
|
|
77
|
-
const
|
|
88
|
+
const command = {
|
|
78
89
|
crud: 'delete',
|
|
79
90
|
method: 'delete',
|
|
80
91
|
byId: true,
|
|
81
92
|
documentId: id,
|
|
82
93
|
options,
|
|
83
94
|
};
|
|
84
|
-
return this.
|
|
85
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
86
|
-
|
|
87
|
-
|
|
95
|
+
return this._executeCommand(command, async () => {
|
|
96
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
97
|
+
command.options = { ...command.options, filter };
|
|
98
|
+
return this._delete(command);
|
|
99
|
+
});
|
|
88
100
|
}
|
|
89
101
|
/**
|
|
90
102
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
@@ -93,16 +105,17 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
93
105
|
* @return {Promise<number>} - A promise that resolves to the number of documents deleted.
|
|
94
106
|
*/
|
|
95
107
|
async deleteMany(options) {
|
|
96
|
-
const
|
|
108
|
+
const command = {
|
|
97
109
|
crud: 'delete',
|
|
98
110
|
method: 'deleteMany',
|
|
99
111
|
byId: false,
|
|
100
112
|
options,
|
|
101
113
|
};
|
|
102
|
-
return this.
|
|
103
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
104
|
-
|
|
105
|
-
|
|
114
|
+
return this._executeCommand(command, async () => {
|
|
115
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
116
|
+
command.options = { ...command.options, filter };
|
|
117
|
+
return this._deleteMany(command);
|
|
118
|
+
});
|
|
106
119
|
}
|
|
107
120
|
/**
|
|
108
121
|
* Checks if a record with the given id exists.
|
|
@@ -112,17 +125,18 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
112
125
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
|
|
113
126
|
*/
|
|
114
127
|
async exists(id, options) {
|
|
115
|
-
const
|
|
128
|
+
const command = {
|
|
116
129
|
crud: 'read',
|
|
117
130
|
method: 'exists',
|
|
118
131
|
byId: true,
|
|
119
132
|
documentId: id,
|
|
120
133
|
options,
|
|
121
134
|
};
|
|
122
|
-
return this.
|
|
123
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
124
|
-
|
|
125
|
-
|
|
135
|
+
return this._executeCommand(command, async () => {
|
|
136
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
137
|
+
command.options = { ...command.options, filter };
|
|
138
|
+
return this._exists(command);
|
|
139
|
+
});
|
|
126
140
|
}
|
|
127
141
|
/**
|
|
128
142
|
* Checks if a record with the given arguments exists.
|
|
@@ -131,37 +145,39 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
131
145
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
|
|
132
146
|
*/
|
|
133
147
|
async existsOne(options) {
|
|
134
|
-
const
|
|
148
|
+
const command = {
|
|
135
149
|
crud: 'read',
|
|
136
150
|
method: 'existsOne',
|
|
137
151
|
byId: false,
|
|
138
152
|
options,
|
|
139
153
|
};
|
|
140
|
-
return this.
|
|
141
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
142
|
-
|
|
143
|
-
|
|
154
|
+
return this._executeCommand(command, async () => {
|
|
155
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
156
|
+
command.options = { ...command.options, filter };
|
|
157
|
+
return this._existsOne(command);
|
|
158
|
+
});
|
|
144
159
|
}
|
|
145
160
|
/**
|
|
146
161
|
* Finds a record by ID.
|
|
147
162
|
*
|
|
148
|
-
* @param {SQBAdapter.
|
|
163
|
+
* @param {SQBAdapter.IdOrIds} id - The ID of the record.
|
|
149
164
|
* @param {SqbCollectionService.FindOneOptions} [options] - The options for the find query.
|
|
150
165
|
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
151
166
|
*/
|
|
152
167
|
async findById(id, options) {
|
|
153
|
-
const
|
|
168
|
+
const command = {
|
|
154
169
|
crud: 'read',
|
|
155
170
|
method: 'findById',
|
|
156
171
|
byId: true,
|
|
157
172
|
documentId: id,
|
|
158
173
|
options,
|
|
159
174
|
};
|
|
160
|
-
return this.
|
|
161
|
-
const documentFilter = await this._getCommonFilter(
|
|
162
|
-
const filter = SQBAdapter.parseFilter([documentFilter, options?.filter]);
|
|
163
|
-
|
|
164
|
-
|
|
175
|
+
return this._executeCommand(command, async () => {
|
|
176
|
+
const documentFilter = await this._getCommonFilter(command);
|
|
177
|
+
const filter = SQBAdapter.parseFilter([documentFilter, command.options?.filter]);
|
|
178
|
+
command.options = { ...command.options, filter };
|
|
179
|
+
return this._findById(command);
|
|
180
|
+
});
|
|
165
181
|
}
|
|
166
182
|
/**
|
|
167
183
|
* Finds a record in the collection that matches the specified options.
|
|
@@ -170,16 +186,17 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
170
186
|
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
171
187
|
*/
|
|
172
188
|
async findOne(options) {
|
|
173
|
-
const
|
|
189
|
+
const command = {
|
|
174
190
|
crud: 'read',
|
|
175
191
|
method: 'findOne',
|
|
176
192
|
byId: false,
|
|
177
193
|
options,
|
|
178
194
|
};
|
|
179
|
-
return this.
|
|
180
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
181
|
-
|
|
182
|
-
|
|
195
|
+
return this._executeCommand(command, async () => {
|
|
196
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
197
|
+
command.options = { ...command.options, filter };
|
|
198
|
+
return this._findOne(command);
|
|
199
|
+
});
|
|
183
200
|
}
|
|
184
201
|
/**
|
|
185
202
|
* Finds multiple records in collection.
|
|
@@ -188,16 +205,18 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
188
205
|
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
189
206
|
*/
|
|
190
207
|
async findMany(options) {
|
|
191
|
-
const
|
|
208
|
+
const command = {
|
|
192
209
|
crud: 'read',
|
|
193
210
|
method: 'findMany',
|
|
194
211
|
byId: false,
|
|
195
212
|
options,
|
|
196
213
|
};
|
|
197
|
-
return this.
|
|
198
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
199
|
-
|
|
200
|
-
|
|
214
|
+
return this._executeCommand(command, async () => {
|
|
215
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
216
|
+
const limit = command.options?.limit || this.defaultLimit;
|
|
217
|
+
command.options = { ...command.options, filter, limit };
|
|
218
|
+
return this._findMany(command);
|
|
219
|
+
});
|
|
201
220
|
}
|
|
202
221
|
/**
|
|
203
222
|
* Finds multiple records in the collection and returns both records (max limit)
|
|
@@ -213,7 +232,7 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
213
232
|
/**
|
|
214
233
|
* Retrieves a records from the collection by its ID. Throws error if not found.
|
|
215
234
|
*
|
|
216
|
-
* @param {SQBAdapter.
|
|
235
|
+
* @param {SQBAdapter.IdOrIds} id - The ID of the document to retrieve.
|
|
217
236
|
* @param {SqbCollectionService.FindOneOptions} [options] - Optional options for the findOne operation.
|
|
218
237
|
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
|
|
219
238
|
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
@@ -235,7 +254,7 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
235
254
|
* undefined if the document was not found.
|
|
236
255
|
*/
|
|
237
256
|
async update(id, input, options) {
|
|
238
|
-
const
|
|
257
|
+
const command = {
|
|
239
258
|
crud: 'update',
|
|
240
259
|
method: 'update',
|
|
241
260
|
documentId: id,
|
|
@@ -243,10 +262,11 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
243
262
|
input,
|
|
244
263
|
options,
|
|
245
264
|
};
|
|
246
|
-
return this.
|
|
247
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
248
|
-
|
|
249
|
-
|
|
265
|
+
return this._executeCommand(command, async () => {
|
|
266
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
267
|
+
command.options = { ...command.options, filter };
|
|
268
|
+
return this._update(command);
|
|
269
|
+
});
|
|
250
270
|
}
|
|
251
271
|
/**
|
|
252
272
|
* Updates a record in the collection with the specified ID and returns updated record count
|
|
@@ -257,7 +277,7 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
257
277
|
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
258
278
|
*/
|
|
259
279
|
async updateOnly(id, input, options) {
|
|
260
|
-
const
|
|
280
|
+
const command = {
|
|
261
281
|
crud: 'update',
|
|
262
282
|
method: 'update',
|
|
263
283
|
documentId: id,
|
|
@@ -265,10 +285,11 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
265
285
|
input,
|
|
266
286
|
options,
|
|
267
287
|
};
|
|
268
|
-
return this.
|
|
269
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
270
|
-
|
|
271
|
-
|
|
288
|
+
return this._executeCommand(command, async () => {
|
|
289
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
290
|
+
command.options = { ...command.options, filter };
|
|
291
|
+
return this._updateOnly(command);
|
|
292
|
+
});
|
|
272
293
|
}
|
|
273
294
|
/**
|
|
274
295
|
* Updates multiple records in the collection based on the specified input and options.
|
|
@@ -278,16 +299,17 @@ export class SqbCollectionService extends SqbEntityService {
|
|
|
278
299
|
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
279
300
|
*/
|
|
280
301
|
async updateMany(input, options) {
|
|
281
|
-
const
|
|
302
|
+
const command = {
|
|
282
303
|
crud: 'update',
|
|
283
304
|
method: 'updateMany',
|
|
284
305
|
byId: false,
|
|
285
306
|
input,
|
|
286
307
|
options,
|
|
287
308
|
};
|
|
288
|
-
return this.
|
|
289
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
290
|
-
|
|
291
|
-
|
|
309
|
+
return this._executeCommand(command, async () => {
|
|
310
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
311
|
+
command.options = { ...command.options, filter };
|
|
312
|
+
return this._updateMany(command);
|
|
313
|
+
});
|
|
292
314
|
}
|
|
293
315
|
}
|