@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
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { InternalServerError } from '@opra/common';
|
|
2
2
|
import { ServiceBase } from '@opra/core';
|
|
3
|
+
import { op } from '@sqb/builder';
|
|
3
4
|
import { EntityMetadata } from '@sqb/connect';
|
|
5
|
+
import { isNotNullish } from 'valgen';
|
|
4
6
|
import { SQBAdapter } from './sqb-adapter.js';
|
|
5
7
|
/**
|
|
6
8
|
* @class SqbEntityService
|
|
@@ -20,9 +22,9 @@ export class SqbEntityService extends ServiceBase {
|
|
|
20
22
|
this._outputCodecs = {};
|
|
21
23
|
this._dataType_ = dataType;
|
|
22
24
|
this.db = options?.db;
|
|
23
|
-
this
|
|
24
|
-
this
|
|
25
|
-
this
|
|
25
|
+
this.resourceName = options?.resourceName;
|
|
26
|
+
this.commonFilter = options?.commonFilter;
|
|
27
|
+
this.interceptor = options?.interceptor;
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
28
30
|
* Retrieves the OPRA data type
|
|
@@ -59,6 +61,17 @@ export class SqbEntityService extends ServiceBase {
|
|
|
59
61
|
}
|
|
60
62
|
return this._entityMetadata;
|
|
61
63
|
}
|
|
64
|
+
for(context, overwriteProperties, overwriteContext) {
|
|
65
|
+
if (overwriteProperties?.commonFilter && this.commonFilter) {
|
|
66
|
+
overwriteProperties.commonFilter = [
|
|
67
|
+
...(Array.isArray(this.commonFilter) ? this.commonFilter : [this.commonFilter]),
|
|
68
|
+
...(Array.isArray(overwriteProperties?.commonFilter)
|
|
69
|
+
? overwriteProperties?.commonFilter
|
|
70
|
+
: [overwriteProperties?.commonFilter]),
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
return super.for(context, overwriteProperties, overwriteContext);
|
|
74
|
+
}
|
|
62
75
|
/**
|
|
63
76
|
* Retrieves the resource name.
|
|
64
77
|
*
|
|
@@ -66,7 +79,7 @@ export class SqbEntityService extends ServiceBase {
|
|
|
66
79
|
* @throws {Error} If the collection name is not defined.
|
|
67
80
|
*/
|
|
68
81
|
getResourceName() {
|
|
69
|
-
const out = typeof this
|
|
82
|
+
const out = typeof this.resourceName === 'function' ? this.resourceName(this) : this.resourceName || this.dataType.name;
|
|
70
83
|
if (out)
|
|
71
84
|
return out;
|
|
72
85
|
throw new Error('resourceName is not defined');
|
|
@@ -104,108 +117,125 @@ export class SqbEntityService extends ServiceBase {
|
|
|
104
117
|
/**
|
|
105
118
|
* Insert a new record into database
|
|
106
119
|
*
|
|
107
|
-
* @param
|
|
108
|
-
* @
|
|
109
|
-
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created resource
|
|
110
|
-
* @throws {InternalServerError} if an unknown error occurs while creating the resource
|
|
120
|
+
* @param command
|
|
121
|
+
* @returns - A promise that resolves to the created resource
|
|
111
122
|
* @protected
|
|
112
123
|
*/
|
|
113
|
-
async _create(
|
|
124
|
+
async _create(command) {
|
|
125
|
+
const { input, options } = command;
|
|
126
|
+
isNotNullish(command.input, { label: 'input' });
|
|
114
127
|
const inputCodec = this.getInputCodec('create');
|
|
115
128
|
const outputCodec = this.getOutputCodec('create');
|
|
116
129
|
const data = inputCodec(input);
|
|
117
|
-
const
|
|
130
|
+
const conn = await this.getConnection();
|
|
131
|
+
const repo = conn.getRepository(this.dataTypeClass);
|
|
132
|
+
const out = await repo.create(data, options);
|
|
118
133
|
if (out)
|
|
119
134
|
return outputCodec(out);
|
|
120
135
|
throw new InternalServerError(`Unknown error while creating document for "${this.getResourceName()}"`);
|
|
121
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Insert a new record into database
|
|
139
|
+
*
|
|
140
|
+
* @param command
|
|
141
|
+
* @returns - A promise that resolves to the created resource
|
|
142
|
+
* @protected
|
|
143
|
+
*/
|
|
144
|
+
async _createOnly(command) {
|
|
145
|
+
const { input, options } = command;
|
|
146
|
+
isNotNullish(command.input, { label: 'input' });
|
|
147
|
+
const inputCodec = this.getInputCodec('create');
|
|
148
|
+
const data = inputCodec(input);
|
|
149
|
+
const conn = await this.getConnection();
|
|
150
|
+
const repo = conn.getRepository(this.dataTypeClass);
|
|
151
|
+
return await repo.createOnly(data, options);
|
|
152
|
+
}
|
|
122
153
|
/**
|
|
123
154
|
* Returns the count of records based on the provided options
|
|
124
155
|
*
|
|
125
|
-
* @param
|
|
126
|
-
* @return
|
|
156
|
+
* @param command
|
|
157
|
+
* @return - A promise that resolves to the count of records
|
|
127
158
|
* @protected
|
|
128
159
|
*/
|
|
129
|
-
async _count(
|
|
130
|
-
return this._dbCount(options);
|
|
160
|
+
async _count(command) {
|
|
161
|
+
return this._dbCount(command.options);
|
|
131
162
|
}
|
|
132
163
|
/**
|
|
133
164
|
* Deletes a record from the collection.
|
|
134
165
|
*
|
|
135
|
-
* @param
|
|
136
|
-
* @
|
|
137
|
-
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
166
|
+
* @param command
|
|
167
|
+
* @return - A Promise that resolves to the number of documents deleted.
|
|
138
168
|
* @protected
|
|
139
169
|
*/
|
|
140
|
-
async _delete(
|
|
141
|
-
|
|
170
|
+
async _delete(command) {
|
|
171
|
+
isNotNullish(command.documentId, { label: 'documentId' });
|
|
172
|
+
return this._dbDelete(command.documentId, command.options);
|
|
142
173
|
}
|
|
143
174
|
/**
|
|
144
175
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
145
176
|
*
|
|
146
|
-
* @param
|
|
147
|
-
* @return
|
|
177
|
+
* @param command
|
|
178
|
+
* @return - A promise that resolves to the number of documents deleted.
|
|
148
179
|
* @protected
|
|
149
180
|
*/
|
|
150
|
-
async _deleteMany(
|
|
151
|
-
return await this._dbDeleteMany(options);
|
|
181
|
+
async _deleteMany(command) {
|
|
182
|
+
return await this._dbDeleteMany(command.options);
|
|
152
183
|
}
|
|
153
184
|
/**
|
|
154
185
|
* Checks if a record with the given id exists.
|
|
155
186
|
*
|
|
156
|
-
* @param
|
|
157
|
-
* @param {SqbEntityService.ExistsOptions} [options] - The options for the query (optional).
|
|
158
|
-
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
|
|
187
|
+
* @param command
|
|
159
188
|
* @protected
|
|
160
189
|
*/
|
|
161
|
-
async _exists(
|
|
162
|
-
|
|
190
|
+
async _exists(command) {
|
|
191
|
+
isNotNullish(command.documentId, { label: 'documentId' });
|
|
192
|
+
return await this._dbExists(command.documentId, command.options);
|
|
163
193
|
}
|
|
164
194
|
/**
|
|
165
195
|
* Checks if a record with the given arguments exists.
|
|
166
196
|
*
|
|
167
|
-
* @param
|
|
168
|
-
* @return
|
|
197
|
+
* @param command
|
|
198
|
+
* @return - A Promise that resolves to a boolean indicating whether the record exists or not.
|
|
169
199
|
* @protected
|
|
170
200
|
*/
|
|
171
|
-
async _existsOne(
|
|
172
|
-
return await this._dbExistsOne(options);
|
|
201
|
+
async _existsOne(command) {
|
|
202
|
+
return await this._dbExistsOne(command.options);
|
|
173
203
|
}
|
|
174
204
|
/**
|
|
175
205
|
* Finds a record by ID.
|
|
176
206
|
*
|
|
177
|
-
* @param
|
|
178
|
-
* @
|
|
179
|
-
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
207
|
+
* @param command
|
|
208
|
+
* @return - A promise resolving to the found document, or undefined if not found.
|
|
180
209
|
* @protected
|
|
181
210
|
*/
|
|
182
|
-
async _findById(
|
|
211
|
+
async _findById(command) {
|
|
212
|
+
isNotNullish(command.documentId, { label: 'documentId' });
|
|
183
213
|
const decode = this.getOutputCodec('find');
|
|
184
|
-
const out = await this._dbFindById(
|
|
214
|
+
const out = await this._dbFindById(command.documentId, command.options);
|
|
185
215
|
return out ? decode(out) : undefined;
|
|
186
216
|
}
|
|
187
217
|
/**
|
|
188
218
|
* Finds a record in the collection that matches the specified options.
|
|
189
219
|
*
|
|
190
|
-
* @param
|
|
191
|
-
* @return
|
|
220
|
+
* @param command
|
|
221
|
+
* @return - A promise that resolves with the found document or undefined if no document is found.
|
|
192
222
|
* @protected
|
|
193
223
|
*/
|
|
194
|
-
async _findOne(
|
|
224
|
+
async _findOne(command) {
|
|
195
225
|
const decode = this.getOutputCodec('find');
|
|
196
|
-
const out = await this._dbFindOne(options);
|
|
226
|
+
const out = await this._dbFindOne(command.options);
|
|
197
227
|
return out ? decode(out) : undefined;
|
|
198
228
|
}
|
|
199
229
|
/**
|
|
200
230
|
* Finds multiple records in collection.
|
|
201
231
|
*
|
|
202
|
-
* @param
|
|
203
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
232
|
+
* @param command
|
|
233
|
+
* @return - A Promise that resolves to an array of partial outputs of type T.
|
|
204
234
|
* @protected
|
|
205
235
|
*/
|
|
206
|
-
async _findMany(
|
|
236
|
+
async _findMany(command) {
|
|
207
237
|
const decode = this.getOutputCodec('find');
|
|
208
|
-
const out = await this._dbFindMany(options);
|
|
238
|
+
const out = await this._dbFindMany(command.options);
|
|
209
239
|
if (out?.length) {
|
|
210
240
|
return out.map(x => decode(x));
|
|
211
241
|
}
|
|
@@ -214,17 +244,17 @@ export class SqbEntityService extends ServiceBase {
|
|
|
214
244
|
/**
|
|
215
245
|
* Updates a record with the given id in the collection.
|
|
216
246
|
*
|
|
217
|
-
* @param
|
|
218
|
-
* @
|
|
219
|
-
* @param {SqbEntityService.UpdateOptions} [options] - The options for the update operation.
|
|
220
|
-
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
221
|
-
* undefined if the document was not found.
|
|
247
|
+
* @param command
|
|
248
|
+
* @returns A promise that resolves to the updated document or undefined if the document was not found.
|
|
222
249
|
* @protected
|
|
223
250
|
*/
|
|
224
|
-
async _update(
|
|
251
|
+
async _update(command) {
|
|
252
|
+
isNotNullish(command.documentId, { label: 'documentId' });
|
|
253
|
+
isNotNullish(command.input, { label: 'input' });
|
|
254
|
+
const { documentId, input, options } = command;
|
|
225
255
|
const inputCodec = this.getInputCodec('update');
|
|
226
256
|
const data = inputCodec(input);
|
|
227
|
-
const out = await this._dbUpdate(
|
|
257
|
+
const out = await this._dbUpdate(documentId, data, options);
|
|
228
258
|
const outputCodec = this.getOutputCodec('update');
|
|
229
259
|
if (out)
|
|
230
260
|
return outputCodec(out);
|
|
@@ -232,29 +262,30 @@ export class SqbEntityService extends ServiceBase {
|
|
|
232
262
|
/**
|
|
233
263
|
* Updates a record in the collection with the specified ID and returns updated record count
|
|
234
264
|
*
|
|
235
|
-
* @param
|
|
236
|
-
* @
|
|
237
|
-
* @param {SqbEntityService.UpdateOptions} options - The options for updating the document.
|
|
238
|
-
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
265
|
+
* @param command
|
|
266
|
+
* @returns - A promise that resolves to the number of documents modified.
|
|
239
267
|
* @protected
|
|
240
268
|
*/
|
|
241
|
-
async _updateOnly(
|
|
242
|
-
|
|
269
|
+
async _updateOnly(command) {
|
|
270
|
+
isNotNullish(command.documentId, { label: 'documentId' });
|
|
271
|
+
isNotNullish(command.input, { label: 'input' });
|
|
272
|
+
const { documentId, input, options } = command;
|
|
273
|
+
const inputCodec = this.getInputCodec('update');
|
|
243
274
|
const data = inputCodec(input);
|
|
244
|
-
return await this._dbUpdateOnly(
|
|
275
|
+
return await this._dbUpdateOnly(documentId, data, options);
|
|
245
276
|
}
|
|
246
277
|
/**
|
|
247
278
|
* Updates multiple records in the collection based on the specified input and options.
|
|
248
279
|
*
|
|
249
|
-
* @param
|
|
250
|
-
* @
|
|
251
|
-
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
280
|
+
* @param command
|
|
281
|
+
* @return - A promise that resolves to the number of documents matched and modified.
|
|
252
282
|
* @protected
|
|
253
283
|
*/
|
|
254
|
-
async _updateMany(
|
|
284
|
+
async _updateMany(command) {
|
|
285
|
+
isNotNullish(command.input, { label: 'input' });
|
|
255
286
|
const inputCodec = this.getInputCodec('update');
|
|
256
|
-
const data = inputCodec(input);
|
|
257
|
-
return await this._dbUpdateMany(data, options);
|
|
287
|
+
const data = inputCodec(command.input);
|
|
288
|
+
return await this._dbUpdateMany(data, command.options);
|
|
258
289
|
}
|
|
259
290
|
/**
|
|
260
291
|
* Acquires a connection and performs Repository.create operation
|
|
@@ -440,19 +471,104 @@ export class SqbEntityService extends ServiceBase {
|
|
|
440
471
|
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
441
472
|
* that resolves to the common filter, or undefined if not available.
|
|
442
473
|
*/
|
|
443
|
-
_getCommonFilter(
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
474
|
+
_getCommonFilter(command) {
|
|
475
|
+
const commonFilter = Array.isArray(this.commonFilter) ? this.commonFilter : [this.commonFilter];
|
|
476
|
+
const mapped = commonFilter.map(f => (typeof f === 'function' ? f(command, this) : f));
|
|
477
|
+
return mapped.length > 1 ? op.and(...mapped) : mapped[0];
|
|
478
|
+
}
|
|
479
|
+
async _executeCommand(command, commandFn) {
|
|
480
|
+
let proto;
|
|
481
|
+
const next = async () => {
|
|
482
|
+
proto = proto ? Object.getPrototypeOf(proto) : this;
|
|
483
|
+
while (proto) {
|
|
484
|
+
if (proto.interceptor && Object.prototype.hasOwnProperty.call(proto, 'interceptor')) {
|
|
485
|
+
return await proto.interceptor.call(this, next, command, this);
|
|
486
|
+
}
|
|
487
|
+
proto = Object.getPrototypeOf(proto);
|
|
488
|
+
if (!(proto instanceof SqbEntityService))
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
/** Call before[X] hooks */
|
|
492
|
+
if (command.crud === 'create')
|
|
493
|
+
await this._beforeCreate(command);
|
|
494
|
+
else if (command.crud === 'update' && command.byId) {
|
|
495
|
+
await this._beforeUpdate(command);
|
|
496
|
+
}
|
|
497
|
+
else if (command.crud === 'update' && !command.byId) {
|
|
498
|
+
await this._beforeUpdateMany(command);
|
|
499
|
+
}
|
|
500
|
+
else if (command.crud === 'delete' && command.byId) {
|
|
501
|
+
await this._beforeDelete(command);
|
|
502
|
+
}
|
|
503
|
+
else if (command.crud === 'delete' && !command.byId) {
|
|
504
|
+
await this._beforeDeleteMany(command);
|
|
505
|
+
}
|
|
506
|
+
/** Call command function */
|
|
507
|
+
return commandFn();
|
|
508
|
+
};
|
|
447
509
|
try {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
510
|
+
const result = await next();
|
|
511
|
+
/** Call after[X] hooks */
|
|
512
|
+
if (command.crud === 'create')
|
|
513
|
+
await this._afterCreate(command, result);
|
|
514
|
+
else if (command.crud === 'update' && command.byId) {
|
|
515
|
+
await this._afterUpdate(command, result);
|
|
516
|
+
}
|
|
517
|
+
else if (command.crud === 'update' && !command.byId) {
|
|
518
|
+
await this._afterUpdateMany(command, result);
|
|
519
|
+
}
|
|
520
|
+
else if (command.crud === 'delete' && command.byId) {
|
|
521
|
+
await this._afterDelete(command, result);
|
|
522
|
+
}
|
|
523
|
+
else if (command.crud === 'delete' && !command.byId) {
|
|
524
|
+
await this._afterDeleteMany(command, result);
|
|
525
|
+
}
|
|
526
|
+
return result;
|
|
451
527
|
}
|
|
452
528
|
catch (e) {
|
|
453
|
-
Error.captureStackTrace(e, this.
|
|
454
|
-
await this
|
|
529
|
+
Error.captureStackTrace(e, this._executeCommand);
|
|
530
|
+
await this.onError?.(e, this);
|
|
455
531
|
throw e;
|
|
456
532
|
}
|
|
457
533
|
}
|
|
534
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
535
|
+
async _beforeCreate(command) {
|
|
536
|
+
// Do nothing
|
|
537
|
+
}
|
|
538
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
539
|
+
async _beforeUpdate(command) {
|
|
540
|
+
// Do nothing
|
|
541
|
+
}
|
|
542
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
543
|
+
async _beforeUpdateMany(command) {
|
|
544
|
+
// Do nothing
|
|
545
|
+
}
|
|
546
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
547
|
+
async _beforeDelete(command) {
|
|
548
|
+
// Do nothing
|
|
549
|
+
}
|
|
550
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
551
|
+
async _beforeDeleteMany(command) {
|
|
552
|
+
// Do nothing
|
|
553
|
+
}
|
|
554
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
555
|
+
async _afterCreate(command, result) {
|
|
556
|
+
// Do nothing
|
|
557
|
+
}
|
|
558
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
559
|
+
async _afterUpdate(command, result) {
|
|
560
|
+
// Do nothing
|
|
561
|
+
}
|
|
562
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
563
|
+
async _afterUpdateMany(command, affected) {
|
|
564
|
+
// Do nothing
|
|
565
|
+
}
|
|
566
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
567
|
+
async _afterDelete(command, affected) {
|
|
568
|
+
// Do nothing
|
|
569
|
+
}
|
|
570
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
571
|
+
async _afterDeleteMany(command, affected) {
|
|
572
|
+
// Do nothing
|
|
573
|
+
}
|
|
458
574
|
}
|
|
@@ -16,7 +16,7 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
16
16
|
*/
|
|
17
17
|
constructor(dataType, options) {
|
|
18
18
|
super(dataType, options);
|
|
19
|
-
this.id =
|
|
19
|
+
this.id = options?.id || 1;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
22
|
* Asserts the existence of a resource based on the given options.
|
|
@@ -28,25 +28,17 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
28
28
|
if (!(await this.exists(options)))
|
|
29
29
|
throw new ResourceNotAvailableError(this.getResourceName());
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Inserts a single record into the database.
|
|
33
|
-
*
|
|
34
|
-
* @param {PartialDTO<T>} input - The input data
|
|
35
|
-
* @param {SqbSingletonService.CreateOptions} [options] - The options object
|
|
36
|
-
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created resource
|
|
37
|
-
* @throws {Error} if an unknown error occurs while creating the resource
|
|
38
|
-
*/
|
|
39
31
|
async create(input, options) {
|
|
40
|
-
const
|
|
32
|
+
const command = {
|
|
41
33
|
crud: 'create',
|
|
42
34
|
method: 'create',
|
|
43
35
|
byId: false,
|
|
44
36
|
input,
|
|
45
37
|
options,
|
|
46
38
|
};
|
|
47
|
-
return this.
|
|
39
|
+
return this._executeCommand(command, async () => {
|
|
48
40
|
const primaryFields = EntityMetadata.getPrimaryIndexColumns(this.entityMetadata);
|
|
49
|
-
const data = { ...input };
|
|
41
|
+
const data = { ...command.input };
|
|
50
42
|
if (primaryFields.length > 1) {
|
|
51
43
|
if (typeof primaryFields !== 'object') {
|
|
52
44
|
throw new TypeError(`"${this.entityMetadata.name}" should has multiple primary key fields. So you should provide and object that contains key fields`);
|
|
@@ -57,8 +49,9 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
57
49
|
}
|
|
58
50
|
else
|
|
59
51
|
data[primaryFields[0].name] = this.id;
|
|
60
|
-
|
|
61
|
-
|
|
52
|
+
command.input = data;
|
|
53
|
+
return await this._create(command);
|
|
54
|
+
});
|
|
62
55
|
}
|
|
63
56
|
/**
|
|
64
57
|
* Deletes the singleton record
|
|
@@ -67,17 +60,18 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
67
60
|
* @return {Promise<number>} - A Promise that resolves to the number of records deleted
|
|
68
61
|
*/
|
|
69
62
|
async delete(options) {
|
|
70
|
-
const
|
|
63
|
+
const command = {
|
|
71
64
|
crud: 'delete',
|
|
72
65
|
method: 'delete',
|
|
73
66
|
byId: true,
|
|
74
67
|
documentId: this.id,
|
|
75
68
|
options,
|
|
76
69
|
};
|
|
77
|
-
return this.
|
|
78
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
79
|
-
|
|
80
|
-
|
|
70
|
+
return this._executeCommand(command, async () => {
|
|
71
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
72
|
+
command.options = { ...command.options, filter };
|
|
73
|
+
return this._delete(command);
|
|
74
|
+
});
|
|
81
75
|
}
|
|
82
76
|
/**
|
|
83
77
|
* Checks if the singleton record exists.
|
|
@@ -86,61 +80,41 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
86
80
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
|
|
87
81
|
*/
|
|
88
82
|
async exists(options) {
|
|
89
|
-
const
|
|
83
|
+
const command = {
|
|
90
84
|
crud: 'read',
|
|
91
85
|
method: 'exists',
|
|
92
86
|
byId: true,
|
|
93
87
|
documentId: this.id,
|
|
94
88
|
options,
|
|
95
89
|
};
|
|
96
|
-
return this.
|
|
97
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
98
|
-
|
|
99
|
-
|
|
90
|
+
return this._executeCommand(command, async () => {
|
|
91
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
92
|
+
command.options = { ...command.options, filter };
|
|
93
|
+
return this._exists(command);
|
|
94
|
+
});
|
|
100
95
|
}
|
|
101
|
-
/**
|
|
102
|
-
* Finds the singleton record. Returns `undefined` if not found
|
|
103
|
-
*
|
|
104
|
-
* @param {SqbSingletonService.FindOneOptions} options - The options for the query.
|
|
105
|
-
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
106
|
-
*/
|
|
107
96
|
async find(options) {
|
|
108
|
-
const
|
|
97
|
+
const command = {
|
|
109
98
|
crud: 'read',
|
|
110
99
|
method: 'findById',
|
|
111
100
|
byId: true,
|
|
112
101
|
documentId: this.id,
|
|
113
102
|
options,
|
|
114
103
|
};
|
|
115
|
-
return this.
|
|
116
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
117
|
-
|
|
118
|
-
|
|
104
|
+
return this._executeCommand(command, async () => {
|
|
105
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
106
|
+
command.options = { ...command.options, filter };
|
|
107
|
+
return this._findById(command);
|
|
108
|
+
});
|
|
119
109
|
}
|
|
120
|
-
/**
|
|
121
|
-
* Retrieves the singleton record. Throws error if not found.
|
|
122
|
-
*
|
|
123
|
-
* @param {SqbSingletonService.FindOptions} [options] - Optional options for the `find` operation.
|
|
124
|
-
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
|
|
125
|
-
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
126
|
-
* @throws {ResourceNotAvailableError} - If the document does not exist.
|
|
127
|
-
*/
|
|
128
110
|
async get(options) {
|
|
129
111
|
const out = await this.find(options);
|
|
130
112
|
if (!out)
|
|
131
113
|
throw new ResourceNotAvailableError(this.getResourceName());
|
|
132
114
|
return out;
|
|
133
115
|
}
|
|
134
|
-
/**
|
|
135
|
-
* Updates the singleton.
|
|
136
|
-
*
|
|
137
|
-
* @param {PatchDTO<T>} input - The partial input object containing the fields to update.
|
|
138
|
-
* @param {SqbSingletonService.UpdateOptions} [options] - The options for the update operation.
|
|
139
|
-
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
140
|
-
* undefined if the document was not found.
|
|
141
|
-
*/
|
|
142
116
|
async update(input, options) {
|
|
143
|
-
const
|
|
117
|
+
const command = {
|
|
144
118
|
crud: 'update',
|
|
145
119
|
method: 'update',
|
|
146
120
|
documentId: this.id,
|
|
@@ -148,10 +122,11 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
148
122
|
input,
|
|
149
123
|
options,
|
|
150
124
|
};
|
|
151
|
-
return this.
|
|
152
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
153
|
-
|
|
154
|
-
|
|
125
|
+
return this._executeCommand(command, async () => {
|
|
126
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
127
|
+
command.options = { ...command.options, filter };
|
|
128
|
+
return this._update(command);
|
|
129
|
+
});
|
|
155
130
|
}
|
|
156
131
|
/**
|
|
157
132
|
* Updates the singleton and returns updated record count
|
|
@@ -161,7 +136,7 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
161
136
|
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
162
137
|
*/
|
|
163
138
|
async updateOnly(input, options) {
|
|
164
|
-
const
|
|
139
|
+
const command = {
|
|
165
140
|
crud: 'update',
|
|
166
141
|
method: 'update',
|
|
167
142
|
documentId: this.id,
|
|
@@ -169,9 +144,10 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
169
144
|
input,
|
|
170
145
|
options,
|
|
171
146
|
};
|
|
172
|
-
return this.
|
|
173
|
-
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
174
|
-
|
|
175
|
-
|
|
147
|
+
return this._executeCommand(command, async () => {
|
|
148
|
+
const filter = SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
149
|
+
command.options = { ...command.options, filter };
|
|
150
|
+
return this._updateOnly(command);
|
|
151
|
+
});
|
|
176
152
|
}
|
|
177
153
|
}
|
package/package.json
CHANGED
|
@@ -1,51 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/sqb",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
4
|
"description": "Opra SQB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "https://github.com/panates/opra.git",
|
|
10
|
-
"directory": "packages/sqb"
|
|
11
|
-
},
|
|
12
|
-
"scripts": {
|
|
13
|
-
"compile": "tsc",
|
|
14
|
-
"prebuild": "npm run lint && npm run clean",
|
|
15
|
-
"build": "npm run build:cjs && npm run build:esm",
|
|
16
|
-
"build:cjs": "tsc -b tsconfig-build-cjs.json",
|
|
17
|
-
"build:esm": "tsc -b tsconfig-build-esm.json",
|
|
18
|
-
"postbuild": "cp README.md package.json ../../LICENSE ../../build/sqb && cp ../../package.cjs.json ../../build/sqb/cjs/package.json",
|
|
19
|
-
"lint": "eslint . --max-warnings=0",
|
|
20
|
-
"lint:fix": "eslint . --max-warnings=0 --fix",
|
|
21
|
-
"format": "prettier . --write --log-level=warn",
|
|
22
|
-
"test": "jest --passWithNoTests",
|
|
23
|
-
"cover": "jest --passWithNoTests --collect-coverage",
|
|
24
|
-
"clean": "npm run clean:src && npm run clean:test && npm run clean:dist && npm run clean:cover",
|
|
25
|
-
"clean:src": "ts-cleanup -s src --all",
|
|
26
|
-
"clean:test": "ts-cleanup -s test --all",
|
|
27
|
-
"clean:dist": "rimraf ../../build/client",
|
|
28
|
-
"clean:cover": "rimraf ../../coverage/client"
|
|
29
|
-
},
|
|
30
7
|
"dependencies": {
|
|
31
|
-
"reflect-metadata": "^0.2.2"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"@faker-js/faker": "^8.4.1",
|
|
35
|
-
"@sqb/builder": "^4.12.1",
|
|
36
|
-
"@sqb/connect": "^4.12.1",
|
|
37
|
-
"@sqb/postgres": "^4.12.1",
|
|
38
|
-
"postgresql-client": "^2.11.2",
|
|
39
|
-
"ts-gems": "^3.4.0"
|
|
8
|
+
"reflect-metadata": "^0.2.2",
|
|
9
|
+
"tslib": "^2.7.0",
|
|
10
|
+
"valgen": "^5.9.0"
|
|
40
11
|
},
|
|
41
12
|
"peerDependencies": {
|
|
42
|
-
"@opra/core": "^1.0.0-
|
|
43
|
-
"@sqb/connect": ">= 4.
|
|
13
|
+
"@opra/core": "^1.0.0-beta.2",
|
|
14
|
+
"@sqb/connect": ">= 4.18.0"
|
|
44
15
|
},
|
|
45
16
|
"type": "module",
|
|
46
|
-
"
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": {
|
|
20
|
+
"types": "./types/index.d.ts",
|
|
21
|
+
"default": "./esm/index.js"
|
|
22
|
+
},
|
|
23
|
+
"require": {
|
|
24
|
+
"types": "./types/index.d.cts",
|
|
25
|
+
"default": "./cjs/index.js"
|
|
26
|
+
},
|
|
27
|
+
"default": "./esm/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
47
31
|
"main": "./cjs/index.js",
|
|
32
|
+
"module": "./esm/index.js",
|
|
48
33
|
"types": "./types/index.d.ts",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/panates/opra.git",
|
|
37
|
+
"directory": "packages/sqb"
|
|
38
|
+
},
|
|
49
39
|
"engines": {
|
|
50
40
|
"node": ">=16.0",
|
|
51
41
|
"npm": ">=7.0.0"
|
|
@@ -63,4 +53,4 @@
|
|
|
63
53
|
"sqb",
|
|
64
54
|
"adapter"
|
|
65
55
|
]
|
|
66
|
-
}
|
|
56
|
+
}
|