@opra/sqb 1.0.0-beta.3 → 1.0.0-beta.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/cjs/sqb-adapter.js +1 -1
- package/cjs/sqb-entity-service.js +82 -18
- package/esm/sqb-adapter.js +1 -1
- package/esm/sqb-entity-service.js +83 -19
- package/package.json +3 -3
- package/types/sqb-collection-service.d.ts +1 -1
- package/types/sqb-entity-service.d.ts +34 -21
- package/types/sqb-singleton-service.d.ts +1 -1
package/cjs/sqb-adapter.js
CHANGED
|
@@ -47,7 +47,7 @@ var SQBAdapter;
|
|
|
47
47
|
filter: SQBAdapter.parseFilter(ctx.queryParams.filter),
|
|
48
48
|
projection: ctx.queryParams.projection || operation.compositionOptions.defaultProjection,
|
|
49
49
|
limit: ctx.queryParams.limit || operation.compositionOptions.defaultLimit,
|
|
50
|
-
|
|
50
|
+
skip: ctx.queryParams.skip,
|
|
51
51
|
sort: ctx.queryParams.sort || operation.compositionOptions.defaultSort,
|
|
52
52
|
};
|
|
53
53
|
return { method: 'findMany', options };
|
|
@@ -7,6 +7,7 @@ const builder_1 = require("@sqb/builder");
|
|
|
7
7
|
const connect_1 = require("@sqb/connect");
|
|
8
8
|
const valgen_1 = require("valgen");
|
|
9
9
|
const sqb_adapter_js_1 = require("./sqb-adapter.js");
|
|
10
|
+
const transactionKey = Symbol.for('transaction');
|
|
10
11
|
/**
|
|
11
12
|
* @class SqbEntityService
|
|
12
13
|
* @template T - The data type class type of the resource
|
|
@@ -75,6 +76,54 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
75
76
|
}
|
|
76
77
|
return super.for(context, overwriteProperties, overwriteContext);
|
|
77
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Executes the provided function within a transaction.
|
|
81
|
+
*
|
|
82
|
+
* @param callback - The function to be executed within the transaction.
|
|
83
|
+
*/
|
|
84
|
+
async withTransaction(callback) {
|
|
85
|
+
const ctx = this.context;
|
|
86
|
+
let closeSessionOnFinish = false;
|
|
87
|
+
let connection = ctx[transactionKey];
|
|
88
|
+
if (!connection) {
|
|
89
|
+
/** Determine the SqbClient or SqbConnection instance */
|
|
90
|
+
const db = await this.getConnection();
|
|
91
|
+
if (db instanceof connect_1.SqbConnection) {
|
|
92
|
+
connection = db;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
/** Acquire a connection. New connection should be at the end */
|
|
96
|
+
connection = await db.acquire({ autoCommit: false });
|
|
97
|
+
closeSessionOnFinish = true;
|
|
98
|
+
}
|
|
99
|
+
/** Store transaction connection in current context */
|
|
100
|
+
ctx[transactionKey] = connection;
|
|
101
|
+
}
|
|
102
|
+
const oldInTransaction = connection.inTransaction;
|
|
103
|
+
connection.retain();
|
|
104
|
+
try {
|
|
105
|
+
if (!oldInTransaction)
|
|
106
|
+
await connection.startTransaction();
|
|
107
|
+
const out = await callback(connection, this);
|
|
108
|
+
if (!oldInTransaction && connection.inTransaction)
|
|
109
|
+
await connection.rollback();
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
if (!oldInTransaction && connection.inTransaction)
|
|
114
|
+
await connection.rollback();
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
delete ctx[transactionKey];
|
|
119
|
+
/** Release connection */
|
|
120
|
+
if (closeSessionOnFinish) {
|
|
121
|
+
await connection.close();
|
|
122
|
+
}
|
|
123
|
+
else
|
|
124
|
+
connection.release();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
78
127
|
/**
|
|
79
128
|
* Retrieves the resource name.
|
|
80
129
|
*
|
|
@@ -161,7 +210,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
161
210
|
* @protected
|
|
162
211
|
*/
|
|
163
212
|
async _count(command) {
|
|
164
|
-
|
|
213
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
214
|
+
return this._dbCount({ ...command.options, filter });
|
|
165
215
|
}
|
|
166
216
|
/**
|
|
167
217
|
* Deletes a record from the collection.
|
|
@@ -172,7 +222,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
172
222
|
*/
|
|
173
223
|
async _delete(command) {
|
|
174
224
|
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
175
|
-
|
|
225
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
226
|
+
return this._dbDelete(command.documentId, { ...command.options, filter });
|
|
176
227
|
}
|
|
177
228
|
/**
|
|
178
229
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
@@ -182,7 +233,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
182
233
|
* @protected
|
|
183
234
|
*/
|
|
184
235
|
async _deleteMany(command) {
|
|
185
|
-
|
|
236
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
237
|
+
return await this._dbDeleteMany({ ...command.options, filter });
|
|
186
238
|
}
|
|
187
239
|
/**
|
|
188
240
|
* Checks if a record with the given id exists.
|
|
@@ -192,7 +244,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
192
244
|
*/
|
|
193
245
|
async _exists(command) {
|
|
194
246
|
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
195
|
-
|
|
247
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
248
|
+
return await this._dbExists(command.documentId, { ...command.options, filter });
|
|
196
249
|
}
|
|
197
250
|
/**
|
|
198
251
|
* Checks if a record with the given arguments exists.
|
|
@@ -202,7 +255,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
202
255
|
* @protected
|
|
203
256
|
*/
|
|
204
257
|
async _existsOne(command) {
|
|
205
|
-
|
|
258
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
259
|
+
return await this._dbExistsOne({ ...command.options, filter });
|
|
206
260
|
}
|
|
207
261
|
/**
|
|
208
262
|
* Finds a record by ID.
|
|
@@ -214,7 +268,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
214
268
|
async _findById(command) {
|
|
215
269
|
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
216
270
|
const decode = this.getOutputCodec('find');
|
|
217
|
-
const
|
|
271
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
272
|
+
const out = await this._dbFindById(command.documentId, { ...command.options, filter });
|
|
218
273
|
return out ? decode(out) : undefined;
|
|
219
274
|
}
|
|
220
275
|
/**
|
|
@@ -226,7 +281,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
226
281
|
*/
|
|
227
282
|
async _findOne(command) {
|
|
228
283
|
const decode = this.getOutputCodec('find');
|
|
229
|
-
const
|
|
284
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
285
|
+
const out = await this._dbFindOne({ ...command.options, filter });
|
|
230
286
|
return out ? decode(out) : undefined;
|
|
231
287
|
}
|
|
232
288
|
/**
|
|
@@ -238,7 +294,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
238
294
|
*/
|
|
239
295
|
async _findMany(command) {
|
|
240
296
|
const decode = this.getOutputCodec('find');
|
|
241
|
-
const
|
|
297
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
298
|
+
const out = await this._dbFindMany({ ...command.options, filter });
|
|
242
299
|
if (out?.length) {
|
|
243
300
|
return out.map(x => decode(x));
|
|
244
301
|
}
|
|
@@ -257,7 +314,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
257
314
|
const { documentId, input, options } = command;
|
|
258
315
|
const inputCodec = this.getInputCodec('update');
|
|
259
316
|
const data = inputCodec(input);
|
|
260
|
-
const
|
|
317
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
318
|
+
const out = await this._dbUpdate(documentId, data, { ...options, filter });
|
|
261
319
|
const outputCodec = this.getOutputCodec('update');
|
|
262
320
|
if (out)
|
|
263
321
|
return outputCodec(out);
|
|
@@ -275,7 +333,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
275
333
|
const { documentId, input, options } = command;
|
|
276
334
|
const inputCodec = this.getInputCodec('update');
|
|
277
335
|
const data = inputCodec(input);
|
|
278
|
-
|
|
336
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
337
|
+
return await this._dbUpdateOnly(documentId, data, { ...options, filter });
|
|
279
338
|
}
|
|
280
339
|
/**
|
|
281
340
|
* Updates multiple records in the collection based on the specified input and options.
|
|
@@ -288,7 +347,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
288
347
|
(0, valgen_1.isNotNullish)(command.input, { label: 'input' });
|
|
289
348
|
const inputCodec = this.getInputCodec('update');
|
|
290
349
|
const data = inputCodec(command.input);
|
|
291
|
-
|
|
350
|
+
const filter = command.options?.filter ? sqb_adapter_js_1.SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
351
|
+
return await this._dbUpdateMany(data, { ...command.options, filter });
|
|
292
352
|
}
|
|
293
353
|
/**
|
|
294
354
|
* Acquires a connection and performs Repository.create operation
|
|
@@ -394,7 +454,7 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
394
454
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
395
455
|
if (options?.filter)
|
|
396
456
|
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
397
|
-
return await repo.findOne(options);
|
|
457
|
+
return await repo.findOne({ ...options, offset: options?.skip });
|
|
398
458
|
}
|
|
399
459
|
/**
|
|
400
460
|
* Acquires a connection and performs Repository.findMany operation
|
|
@@ -407,7 +467,7 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
407
467
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
408
468
|
if (options?.filter)
|
|
409
469
|
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
410
|
-
return await repo.findMany(options);
|
|
470
|
+
return await repo.findMany({ ...options, offset: options?.skip });
|
|
411
471
|
}
|
|
412
472
|
/**
|
|
413
473
|
* Acquires a connection and performs Repository.update operation
|
|
@@ -437,7 +497,7 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
437
497
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
438
498
|
if (options?.filter)
|
|
439
499
|
options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
|
|
440
|
-
return await repo.updateOnly(id, data, options);
|
|
500
|
+
return (await repo.updateOnly(id, data, options)) ? 1 : 0;
|
|
441
501
|
}
|
|
442
502
|
/**
|
|
443
503
|
* Acquires a connection and performs Repository.updateMany operation
|
|
@@ -461,10 +521,14 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
461
521
|
* @throws {Error} If the context or database is not set.
|
|
462
522
|
*/
|
|
463
523
|
getConnection() {
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
524
|
+
const ctx = this.context;
|
|
525
|
+
let db = ctx[transactionKey];
|
|
526
|
+
if (db)
|
|
527
|
+
return db;
|
|
528
|
+
db = typeof this.db === 'function' ? this.db(this) : this.db;
|
|
529
|
+
if (db)
|
|
530
|
+
return db;
|
|
531
|
+
throw new Error(`Database not set!`);
|
|
468
532
|
}
|
|
469
533
|
/**
|
|
470
534
|
* Retrieves the common filter used for querying documents.
|
package/esm/sqb-adapter.js
CHANGED
|
@@ -43,7 +43,7 @@ export var SQBAdapter;
|
|
|
43
43
|
filter: SQBAdapter.parseFilter(ctx.queryParams.filter),
|
|
44
44
|
projection: ctx.queryParams.projection || operation.compositionOptions.defaultProjection,
|
|
45
45
|
limit: ctx.queryParams.limit || operation.compositionOptions.defaultLimit,
|
|
46
|
-
|
|
46
|
+
skip: ctx.queryParams.skip,
|
|
47
47
|
sort: ctx.queryParams.sort || operation.compositionOptions.defaultSort,
|
|
48
48
|
};
|
|
49
49
|
return { method: 'findMany', options };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { InternalServerError } from '@opra/common';
|
|
2
2
|
import { ServiceBase } from '@opra/core';
|
|
3
3
|
import { op } from '@sqb/builder';
|
|
4
|
-
import { EntityMetadata } from '@sqb/connect';
|
|
4
|
+
import { EntityMetadata, SqbConnection } from '@sqb/connect';
|
|
5
5
|
import { isNotNullish } from 'valgen';
|
|
6
6
|
import { SQBAdapter } from './sqb-adapter.js';
|
|
7
|
+
const transactionKey = Symbol.for('transaction');
|
|
7
8
|
/**
|
|
8
9
|
* @class SqbEntityService
|
|
9
10
|
* @template T - The data type class type of the resource
|
|
@@ -72,6 +73,54 @@ export class SqbEntityService extends ServiceBase {
|
|
|
72
73
|
}
|
|
73
74
|
return super.for(context, overwriteProperties, overwriteContext);
|
|
74
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Executes the provided function within a transaction.
|
|
78
|
+
*
|
|
79
|
+
* @param callback - The function to be executed within the transaction.
|
|
80
|
+
*/
|
|
81
|
+
async withTransaction(callback) {
|
|
82
|
+
const ctx = this.context;
|
|
83
|
+
let closeSessionOnFinish = false;
|
|
84
|
+
let connection = ctx[transactionKey];
|
|
85
|
+
if (!connection) {
|
|
86
|
+
/** Determine the SqbClient or SqbConnection instance */
|
|
87
|
+
const db = await this.getConnection();
|
|
88
|
+
if (db instanceof SqbConnection) {
|
|
89
|
+
connection = db;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
/** Acquire a connection. New connection should be at the end */
|
|
93
|
+
connection = await db.acquire({ autoCommit: false });
|
|
94
|
+
closeSessionOnFinish = true;
|
|
95
|
+
}
|
|
96
|
+
/** Store transaction connection in current context */
|
|
97
|
+
ctx[transactionKey] = connection;
|
|
98
|
+
}
|
|
99
|
+
const oldInTransaction = connection.inTransaction;
|
|
100
|
+
connection.retain();
|
|
101
|
+
try {
|
|
102
|
+
if (!oldInTransaction)
|
|
103
|
+
await connection.startTransaction();
|
|
104
|
+
const out = await callback(connection, this);
|
|
105
|
+
if (!oldInTransaction && connection.inTransaction)
|
|
106
|
+
await connection.rollback();
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
if (!oldInTransaction && connection.inTransaction)
|
|
111
|
+
await connection.rollback();
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
delete ctx[transactionKey];
|
|
116
|
+
/** Release connection */
|
|
117
|
+
if (closeSessionOnFinish) {
|
|
118
|
+
await connection.close();
|
|
119
|
+
}
|
|
120
|
+
else
|
|
121
|
+
connection.release();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
75
124
|
/**
|
|
76
125
|
* Retrieves the resource name.
|
|
77
126
|
*
|
|
@@ -158,7 +207,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
158
207
|
* @protected
|
|
159
208
|
*/
|
|
160
209
|
async _count(command) {
|
|
161
|
-
|
|
210
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
211
|
+
return this._dbCount({ ...command.options, filter });
|
|
162
212
|
}
|
|
163
213
|
/**
|
|
164
214
|
* Deletes a record from the collection.
|
|
@@ -169,7 +219,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
169
219
|
*/
|
|
170
220
|
async _delete(command) {
|
|
171
221
|
isNotNullish(command.documentId, { label: 'documentId' });
|
|
172
|
-
|
|
222
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
223
|
+
return this._dbDelete(command.documentId, { ...command.options, filter });
|
|
173
224
|
}
|
|
174
225
|
/**
|
|
175
226
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
@@ -179,7 +230,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
179
230
|
* @protected
|
|
180
231
|
*/
|
|
181
232
|
async _deleteMany(command) {
|
|
182
|
-
|
|
233
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
234
|
+
return await this._dbDeleteMany({ ...command.options, filter });
|
|
183
235
|
}
|
|
184
236
|
/**
|
|
185
237
|
* Checks if a record with the given id exists.
|
|
@@ -189,7 +241,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
189
241
|
*/
|
|
190
242
|
async _exists(command) {
|
|
191
243
|
isNotNullish(command.documentId, { label: 'documentId' });
|
|
192
|
-
|
|
244
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
245
|
+
return await this._dbExists(command.documentId, { ...command.options, filter });
|
|
193
246
|
}
|
|
194
247
|
/**
|
|
195
248
|
* Checks if a record with the given arguments exists.
|
|
@@ -199,7 +252,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
199
252
|
* @protected
|
|
200
253
|
*/
|
|
201
254
|
async _existsOne(command) {
|
|
202
|
-
|
|
255
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
256
|
+
return await this._dbExistsOne({ ...command.options, filter });
|
|
203
257
|
}
|
|
204
258
|
/**
|
|
205
259
|
* Finds a record by ID.
|
|
@@ -211,7 +265,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
211
265
|
async _findById(command) {
|
|
212
266
|
isNotNullish(command.documentId, { label: 'documentId' });
|
|
213
267
|
const decode = this.getOutputCodec('find');
|
|
214
|
-
const
|
|
268
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
269
|
+
const out = await this._dbFindById(command.documentId, { ...command.options, filter });
|
|
215
270
|
return out ? decode(out) : undefined;
|
|
216
271
|
}
|
|
217
272
|
/**
|
|
@@ -223,7 +278,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
223
278
|
*/
|
|
224
279
|
async _findOne(command) {
|
|
225
280
|
const decode = this.getOutputCodec('find');
|
|
226
|
-
const
|
|
281
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
282
|
+
const out = await this._dbFindOne({ ...command.options, filter });
|
|
227
283
|
return out ? decode(out) : undefined;
|
|
228
284
|
}
|
|
229
285
|
/**
|
|
@@ -235,7 +291,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
235
291
|
*/
|
|
236
292
|
async _findMany(command) {
|
|
237
293
|
const decode = this.getOutputCodec('find');
|
|
238
|
-
const
|
|
294
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
295
|
+
const out = await this._dbFindMany({ ...command.options, filter });
|
|
239
296
|
if (out?.length) {
|
|
240
297
|
return out.map(x => decode(x));
|
|
241
298
|
}
|
|
@@ -254,7 +311,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
254
311
|
const { documentId, input, options } = command;
|
|
255
312
|
const inputCodec = this.getInputCodec('update');
|
|
256
313
|
const data = inputCodec(input);
|
|
257
|
-
const
|
|
314
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
315
|
+
const out = await this._dbUpdate(documentId, data, { ...options, filter });
|
|
258
316
|
const outputCodec = this.getOutputCodec('update');
|
|
259
317
|
if (out)
|
|
260
318
|
return outputCodec(out);
|
|
@@ -272,7 +330,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
272
330
|
const { documentId, input, options } = command;
|
|
273
331
|
const inputCodec = this.getInputCodec('update');
|
|
274
332
|
const data = inputCodec(input);
|
|
275
|
-
|
|
333
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
334
|
+
return await this._dbUpdateOnly(documentId, data, { ...options, filter });
|
|
276
335
|
}
|
|
277
336
|
/**
|
|
278
337
|
* Updates multiple records in the collection based on the specified input and options.
|
|
@@ -285,7 +344,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
285
344
|
isNotNullish(command.input, { label: 'input' });
|
|
286
345
|
const inputCodec = this.getInputCodec('update');
|
|
287
346
|
const data = inputCodec(command.input);
|
|
288
|
-
|
|
347
|
+
const filter = command.options?.filter ? SQBAdapter.parseFilter(command.options.filter) : undefined;
|
|
348
|
+
return await this._dbUpdateMany(data, { ...command.options, filter });
|
|
289
349
|
}
|
|
290
350
|
/**
|
|
291
351
|
* Acquires a connection and performs Repository.create operation
|
|
@@ -391,7 +451,7 @@ export class SqbEntityService extends ServiceBase {
|
|
|
391
451
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
392
452
|
if (options?.filter)
|
|
393
453
|
options.filter = SQBAdapter.parseFilter(options.filter);
|
|
394
|
-
return await repo.findOne(options);
|
|
454
|
+
return await repo.findOne({ ...options, offset: options?.skip });
|
|
395
455
|
}
|
|
396
456
|
/**
|
|
397
457
|
* Acquires a connection and performs Repository.findMany operation
|
|
@@ -404,7 +464,7 @@ export class SqbEntityService extends ServiceBase {
|
|
|
404
464
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
405
465
|
if (options?.filter)
|
|
406
466
|
options.filter = SQBAdapter.parseFilter(options.filter);
|
|
407
|
-
return await repo.findMany(options);
|
|
467
|
+
return await repo.findMany({ ...options, offset: options?.skip });
|
|
408
468
|
}
|
|
409
469
|
/**
|
|
410
470
|
* Acquires a connection and performs Repository.update operation
|
|
@@ -434,7 +494,7 @@ export class SqbEntityService extends ServiceBase {
|
|
|
434
494
|
const repo = conn.getRepository(this.dataTypeClass);
|
|
435
495
|
if (options?.filter)
|
|
436
496
|
options.filter = SQBAdapter.parseFilter(options.filter);
|
|
437
|
-
return await repo.updateOnly(id, data, options);
|
|
497
|
+
return (await repo.updateOnly(id, data, options)) ? 1 : 0;
|
|
438
498
|
}
|
|
439
499
|
/**
|
|
440
500
|
* Acquires a connection and performs Repository.updateMany operation
|
|
@@ -458,10 +518,14 @@ export class SqbEntityService extends ServiceBase {
|
|
|
458
518
|
* @throws {Error} If the context or database is not set.
|
|
459
519
|
*/
|
|
460
520
|
getConnection() {
|
|
461
|
-
const
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
521
|
+
const ctx = this.context;
|
|
522
|
+
let db = ctx[transactionKey];
|
|
523
|
+
if (db)
|
|
524
|
+
return db;
|
|
525
|
+
db = typeof this.db === 'function' ? this.db(this) : this.db;
|
|
526
|
+
if (db)
|
|
527
|
+
return db;
|
|
528
|
+
throw new Error(`Database not set!`);
|
|
465
529
|
}
|
|
466
530
|
/**
|
|
467
531
|
* Retrieves the common filter used for querying documents.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/sqb",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.4",
|
|
4
4
|
"description": "Opra SQB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
"valgen": "^5.10.0"
|
|
11
11
|
},
|
|
12
12
|
"peerDependencies": {
|
|
13
|
-
"@opra/core": "^1.0.0-beta.
|
|
14
|
-
"@opra/http": "^1.0.0-beta.
|
|
13
|
+
"@opra/core": "^1.0.0-beta.4",
|
|
14
|
+
"@opra/http": "^1.0.0-beta.4",
|
|
15
15
|
"@sqb/connect": ">= 4.18.0"
|
|
16
16
|
},
|
|
17
17
|
"type": "module",
|
|
@@ -91,7 +91,7 @@ export declare namespace SqbCollectionService {
|
|
|
91
91
|
* @class SqbCollectionService
|
|
92
92
|
* @template T - The data type class type of the resource
|
|
93
93
|
*/
|
|
94
|
-
export declare
|
|
94
|
+
export declare class SqbCollectionService<T extends object = object> extends SqbEntityService {
|
|
95
95
|
/**
|
|
96
96
|
* Represents default limit for findMany operation
|
|
97
97
|
*/
|
|
@@ -37,63 +37,66 @@ export declare namespace SqbEntityService {
|
|
|
37
37
|
*
|
|
38
38
|
* @interface
|
|
39
39
|
*/
|
|
40
|
-
interface CountOptions extends Repository.CountOptions {
|
|
40
|
+
interface CountOptions extends StrictOmit<Repository.CountOptions, 'filter'> {
|
|
41
|
+
filter?: Repository.CountOptions['filter'] | string;
|
|
41
42
|
}
|
|
42
43
|
/**
|
|
43
44
|
* Represents options for "delete" operation
|
|
44
45
|
*
|
|
45
46
|
* @interface
|
|
46
47
|
*/
|
|
47
|
-
interface DeleteOptions extends Repository.DeleteOptions {
|
|
48
|
+
interface DeleteOptions extends StrictOmit<Repository.DeleteOptions, 'filter'> {
|
|
49
|
+
filter?: Repository.DeleteOptions['filter'] | string;
|
|
48
50
|
}
|
|
49
51
|
/**
|
|
50
52
|
* Represents options for "deleteMany" operation
|
|
51
53
|
*
|
|
52
54
|
* @interface
|
|
53
55
|
*/
|
|
54
|
-
interface DeleteManyOptions extends Repository.DeleteManyOptions {
|
|
56
|
+
interface DeleteManyOptions extends StrictOmit<Repository.DeleteManyOptions, 'filter'> {
|
|
57
|
+
filter?: Repository.DeleteManyOptions['filter'] | string;
|
|
55
58
|
}
|
|
56
59
|
/**
|
|
57
60
|
* Represents options for "exists" operation
|
|
58
61
|
*
|
|
59
62
|
* @interface
|
|
60
63
|
*/
|
|
61
|
-
interface ExistsOptions extends Repository.ExistsOptions {
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Represents options for "existsOne" operation
|
|
65
|
-
*
|
|
66
|
-
* @interface
|
|
67
|
-
*/
|
|
68
|
-
interface ExistsOneOptions extends Repository.ExistsOptions {
|
|
64
|
+
interface ExistsOptions extends StrictOmit<Repository.ExistsOptions, 'filter'> {
|
|
65
|
+
filter?: Repository.ExistsOptions['filter'] | string;
|
|
69
66
|
}
|
|
70
67
|
/**
|
|
71
68
|
* Represents options for "findOne" operation
|
|
72
69
|
*
|
|
73
70
|
* @interface
|
|
74
71
|
*/
|
|
75
|
-
interface FindOneOptions extends Repository.FindOneOptions {
|
|
72
|
+
interface FindOneOptions extends StrictOmit<Repository.FindOneOptions, 'filter' | 'offset'> {
|
|
73
|
+
filter?: Repository.FindOneOptions['filter'] | string;
|
|
74
|
+
skip?: number;
|
|
76
75
|
}
|
|
77
76
|
/**
|
|
78
77
|
* Represents options for "findMany" operation
|
|
79
78
|
*
|
|
80
79
|
* @interface
|
|
81
80
|
*/
|
|
82
|
-
interface FindManyOptions extends Repository.FindManyOptions {
|
|
81
|
+
interface FindManyOptions extends StrictOmit<Repository.FindManyOptions, 'filter' | 'offset'> {
|
|
82
|
+
filter?: Repository.FindManyOptions['filter'] | string;
|
|
83
|
+
skip?: number;
|
|
83
84
|
}
|
|
84
85
|
/**
|
|
85
86
|
* Represents options for "update" operation
|
|
86
87
|
*
|
|
87
88
|
* @interface
|
|
88
89
|
*/
|
|
89
|
-
interface UpdateOneOptions extends Repository.UpdateOptions {
|
|
90
|
+
interface UpdateOneOptions extends StrictOmit<Repository.UpdateOptions, 'filter'> {
|
|
91
|
+
filter?: Repository.UpdateOptions['filter'] | string;
|
|
90
92
|
}
|
|
91
93
|
/**
|
|
92
94
|
* Represents options for "updateMany" operation
|
|
93
95
|
*
|
|
94
96
|
* @interface
|
|
95
97
|
*/
|
|
96
|
-
interface UpdateManyOptions extends Repository.UpdateManyOptions {
|
|
98
|
+
interface UpdateManyOptions extends StrictOmit<Repository.UpdateManyOptions, 'filter'> {
|
|
99
|
+
filter?: Repository.UpdateManyOptions['filter'] | string;
|
|
97
100
|
}
|
|
98
101
|
interface CreateCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'input'>, 'documentId'> {
|
|
99
102
|
crud: 'create';
|
|
@@ -205,7 +208,13 @@ export declare class SqbEntityService<T extends object = object> extends Service
|
|
|
205
208
|
* @throws {TypeError} If metadata is not available
|
|
206
209
|
*/
|
|
207
210
|
get entityMetadata(): EntityMetadata;
|
|
208
|
-
for<C extends ExecutionContext, P extends Partial<this>>(context: C, overwriteProperties?: Nullish<P>, overwriteContext?: Partial<C>): this & Required<P>;
|
|
211
|
+
for<C extends ExecutionContext, P extends Partial<this>>(context: C | ServiceBase, overwriteProperties?: Nullish<P>, overwriteContext?: Partial<C>): this & Required<P>;
|
|
212
|
+
/**
|
|
213
|
+
* Executes the provided function within a transaction.
|
|
214
|
+
*
|
|
215
|
+
* @param callback - The function to be executed within the transaction.
|
|
216
|
+
*/
|
|
217
|
+
withTransaction(callback: (connection: SqbConnection, _this: this) => any): Promise<any>;
|
|
209
218
|
/**
|
|
210
219
|
* Retrieves the resource name.
|
|
211
220
|
*
|
|
@@ -317,7 +326,7 @@ export declare class SqbEntityService<T extends object = object> extends Service
|
|
|
317
326
|
* @returns - A promise that resolves to the number of documents modified.
|
|
318
327
|
* @protected
|
|
319
328
|
*/
|
|
320
|
-
protected _updateOnly(command: SqbEntityService.UpdateOneCommand<T>): Promise<
|
|
329
|
+
protected _updateOnly(command: SqbEntityService.UpdateOneCommand<T>): Promise<number>;
|
|
321
330
|
/**
|
|
322
331
|
* Updates multiple records in the collection based on the specified input and options.
|
|
323
332
|
*
|
|
@@ -385,14 +394,18 @@ export declare class SqbEntityService<T extends object = object> extends Service
|
|
|
385
394
|
* @param options - Optional settings for the command
|
|
386
395
|
* @protected
|
|
387
396
|
*/
|
|
388
|
-
protected _dbFindOne(options?: Repository.FindOneOptions
|
|
397
|
+
protected _dbFindOne(options?: StrictOmit<Repository.FindOneOptions, 'offset'> & {
|
|
398
|
+
skip?: number;
|
|
399
|
+
}): Promise<PartialDTO<T> | undefined>;
|
|
389
400
|
/**
|
|
390
401
|
* Acquires a connection and performs Repository.findMany operation
|
|
391
402
|
*
|
|
392
403
|
* @param options - Optional settings for the command
|
|
393
404
|
* @protected
|
|
394
405
|
*/
|
|
395
|
-
protected _dbFindMany(options?: Repository.FindManyOptions
|
|
406
|
+
protected _dbFindMany(options?: StrictOmit<Repository.FindManyOptions, 'offset'> & {
|
|
407
|
+
skip?: number;
|
|
408
|
+
}): Promise<PartialDTO<T>[]>;
|
|
396
409
|
/**
|
|
397
410
|
* Acquires a connection and performs Repository.update operation
|
|
398
411
|
*
|
|
@@ -410,7 +423,7 @@ export declare class SqbEntityService<T extends object = object> extends Service
|
|
|
410
423
|
* @param options - Optional settings for the command
|
|
411
424
|
* @protected
|
|
412
425
|
*/
|
|
413
|
-
protected _dbUpdateOnly(id: SQBAdapter.IdOrIds, data: PatchDTO<T>, options?: Repository.UpdateOptions): Promise<
|
|
426
|
+
protected _dbUpdateOnly(id: SQBAdapter.IdOrIds, data: PatchDTO<T>, options?: Repository.UpdateOptions): Promise<number>;
|
|
414
427
|
/**
|
|
415
428
|
* Acquires a connection and performs Repository.updateMany operation
|
|
416
429
|
*
|
|
@@ -426,7 +439,7 @@ export declare class SqbEntityService<T extends object = object> extends Service
|
|
|
426
439
|
*
|
|
427
440
|
* @throws {Error} If the context or database is not set.
|
|
428
441
|
*/
|
|
429
|
-
|
|
442
|
+
getConnection(): SqbConnection | SqbClient | Promise<SqbConnection | SqbClient>;
|
|
430
443
|
/**
|
|
431
444
|
* Retrieves the common filter used for querying documents.
|
|
432
445
|
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
@@ -55,7 +55,7 @@ export declare namespace SqbSingletonService {
|
|
|
55
55
|
* @class SqbSingletonService
|
|
56
56
|
* @template T - The data type class type of the resource
|
|
57
57
|
*/
|
|
58
|
-
export declare
|
|
58
|
+
export declare class SqbSingletonService<T extends object = object> extends SqbEntityService {
|
|
59
59
|
/**
|
|
60
60
|
* Represents a unique identifier for singleton record
|
|
61
61
|
* @property {SQBAdapter.IdOrIds}
|