@opra/sqb 1.0.6 → 1.0.8

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/index.js CHANGED
@@ -7,4 +7,5 @@ require("./augmentation/mixin-type.augmentation.js");
7
7
  tslib_1.__exportStar(require("./sqb-adapter.js"), exports);
8
8
  tslib_1.__exportStar(require("./sqb-collection-service.js"), exports);
9
9
  tslib_1.__exportStar(require("./sqb-entity-service.js"), exports);
10
+ tslib_1.__exportStar(require("./sqb-service-base.js"), exports);
10
11
  tslib_1.__exportStar(require("./sqb-singleton-service.js"), exports);
@@ -2,17 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SqbEntityService = void 0;
4
4
  const common_1 = require("@opra/common");
5
- const core_1 = require("@opra/core");
6
5
  const builder_1 = require("@sqb/builder");
7
6
  const connect_1 = require("@sqb/connect");
8
7
  const valgen_1 = require("valgen");
9
8
  const sqb_adapter_js_1 = require("./sqb-adapter.js");
10
- const transactionKey = Symbol.for('transaction');
9
+ const sqb_service_base_js_1 = require("./sqb-service-base.js");
11
10
  /**
12
11
  * @class SqbEntityService
13
12
  * @template T - The data type class type of the resource
14
13
  */
15
- class SqbEntityService extends core_1.ServiceBase {
14
+ class SqbEntityService extends sqb_service_base_js_1.SqbServiceBase {
16
15
  /**
17
16
  * Constructs a new instance
18
17
  *
@@ -21,11 +20,10 @@ class SqbEntityService extends core_1.ServiceBase {
21
20
  * @constructor
22
21
  */
23
22
  constructor(dataType, options) {
24
- super();
23
+ super(options);
25
24
  this._inputCodecs = {};
26
25
  this._outputCodecs = {};
27
26
  this._dataType_ = dataType;
28
- this.db = options?.db;
29
27
  this.resourceName = options?.resourceName;
30
28
  this.commonFilter = options?.commonFilter;
31
29
  this.interceptor = options?.interceptor;
@@ -76,54 +74,6 @@ class SqbEntityService extends core_1.ServiceBase {
76
74
  }
77
75
  return super.for(context, overwriteProperties, overwriteContext);
78
76
  }
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.commit();
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
- }
127
77
  /**
128
78
  * Retrieves the resource name.
129
79
  *
@@ -513,23 +463,6 @@ class SqbEntityService extends core_1.ServiceBase {
513
463
  options.filter = sqb_adapter_js_1.SQBAdapter.parseFilter(options.filter);
514
464
  return await repo.updateMany(data, options);
515
465
  }
516
- /**
517
- * Retrieves the database connection.
518
- *
519
- * @protected
520
- *
521
- * @throws {Error} If the context or database is not set.
522
- */
523
- getConnection() {
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!`);
532
- }
533
466
  /**
534
467
  * Retrieves the common filter used for querying documents.
535
468
  * This method is mostly used for security issues like securing multi-tenant applications.
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SqbServiceBase = void 0;
4
+ const core_1 = require("@opra/core");
5
+ const connect_1 = require("@sqb/connect");
6
+ const transactionKey = Symbol.for('transaction');
7
+ /**
8
+ * @class SqbServiceBase
9
+ * @template T - The data type class type of the resource
10
+ */
11
+ class SqbServiceBase extends core_1.ServiceBase {
12
+ /**
13
+ * Constructs a new instance
14
+ *
15
+ * @param [options] - The options for the service.
16
+ * @constructor
17
+ */
18
+ constructor(options) {
19
+ super();
20
+ this.db = options?.db;
21
+ }
22
+ /**
23
+ * Executes the provided function within a transaction.
24
+ *
25
+ * @param callback - The function to be executed within the transaction.
26
+ */
27
+ async withTransaction(callback) {
28
+ const ctx = this.context;
29
+ let closeSessionOnFinish = false;
30
+ let connection = ctx[transactionKey];
31
+ if (!connection) {
32
+ /** Determine the SqbClient or SqbConnection instance */
33
+ const db = await this.getConnection();
34
+ if (db instanceof connect_1.SqbConnection) {
35
+ connection = db;
36
+ }
37
+ else {
38
+ /** Acquire a connection. New connection should be at the end */
39
+ connection = await db.acquire({ autoCommit: false });
40
+ closeSessionOnFinish = true;
41
+ }
42
+ /** Store transaction connection in current context */
43
+ ctx[transactionKey] = connection;
44
+ }
45
+ const oldInTransaction = connection.inTransaction;
46
+ connection.retain();
47
+ try {
48
+ if (!oldInTransaction)
49
+ await connection.startTransaction();
50
+ const out = await callback(connection, this);
51
+ if (!oldInTransaction && connection.inTransaction)
52
+ await connection.commit();
53
+ return out;
54
+ }
55
+ catch (e) {
56
+ if (!oldInTransaction && connection.inTransaction)
57
+ await connection.rollback();
58
+ throw e;
59
+ }
60
+ finally {
61
+ delete ctx[transactionKey];
62
+ /** Release connection */
63
+ if (closeSessionOnFinish) {
64
+ await connection.close();
65
+ }
66
+ else
67
+ connection.release();
68
+ }
69
+ }
70
+ /**
71
+ * Retrieves the database connection.
72
+ *
73
+ * @protected
74
+ *
75
+ * @throws {Error} If the context or database is not set.
76
+ */
77
+ getConnection() {
78
+ const ctx = this.context;
79
+ let db = ctx[transactionKey];
80
+ if (db)
81
+ return db;
82
+ db = typeof this.db === 'function' ? this.db(this) : this.db;
83
+ if (db)
84
+ return db;
85
+ throw new Error(`Database not set!`);
86
+ }
87
+ }
88
+ exports.SqbServiceBase = SqbServiceBase;
package/esm/index.js CHANGED
@@ -4,4 +4,5 @@ import './augmentation/mixin-type.augmentation.js';
4
4
  export * from './sqb-adapter.js';
5
5
  export * from './sqb-collection-service.js';
6
6
  export * from './sqb-entity-service.js';
7
+ export * from './sqb-service-base.js';
7
8
  export * from './sqb-singleton-service.js';
@@ -1,15 +1,14 @@
1
1
  import { InternalServerError } from '@opra/common';
2
- import { ServiceBase } from '@opra/core';
3
2
  import { op } from '@sqb/builder';
4
- import { EntityMetadata, SqbConnection } from '@sqb/connect';
3
+ import { EntityMetadata } from '@sqb/connect';
5
4
  import { isNotNullish } from 'valgen';
6
5
  import { SQBAdapter } from './sqb-adapter.js';
7
- const transactionKey = Symbol.for('transaction');
6
+ import { SqbServiceBase } from './sqb-service-base.js';
8
7
  /**
9
8
  * @class SqbEntityService
10
9
  * @template T - The data type class type of the resource
11
10
  */
12
- export class SqbEntityService extends ServiceBase {
11
+ export class SqbEntityService extends SqbServiceBase {
13
12
  /**
14
13
  * Constructs a new instance
15
14
  *
@@ -18,11 +17,10 @@ export class SqbEntityService extends ServiceBase {
18
17
  * @constructor
19
18
  */
20
19
  constructor(dataType, options) {
21
- super();
20
+ super(options);
22
21
  this._inputCodecs = {};
23
22
  this._outputCodecs = {};
24
23
  this._dataType_ = dataType;
25
- this.db = options?.db;
26
24
  this.resourceName = options?.resourceName;
27
25
  this.commonFilter = options?.commonFilter;
28
26
  this.interceptor = options?.interceptor;
@@ -73,54 +71,6 @@ export class SqbEntityService extends ServiceBase {
73
71
  }
74
72
  return super.for(context, overwriteProperties, overwriteContext);
75
73
  }
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.commit();
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
- }
124
74
  /**
125
75
  * Retrieves the resource name.
126
76
  *
@@ -510,23 +460,6 @@ export class SqbEntityService extends ServiceBase {
510
460
  options.filter = SQBAdapter.parseFilter(options.filter);
511
461
  return await repo.updateMany(data, options);
512
462
  }
513
- /**
514
- * Retrieves the database connection.
515
- *
516
- * @protected
517
- *
518
- * @throws {Error} If the context or database is not set.
519
- */
520
- getConnection() {
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!`);
529
- }
530
463
  /**
531
464
  * Retrieves the common filter used for querying documents.
532
465
  * This method is mostly used for security issues like securing multi-tenant applications.
@@ -0,0 +1,84 @@
1
+ import { ServiceBase } from '@opra/core';
2
+ import { SqbConnection } from '@sqb/connect';
3
+ const transactionKey = Symbol.for('transaction');
4
+ /**
5
+ * @class SqbServiceBase
6
+ * @template T - The data type class type of the resource
7
+ */
8
+ export class SqbServiceBase extends ServiceBase {
9
+ /**
10
+ * Constructs a new instance
11
+ *
12
+ * @param [options] - The options for the service.
13
+ * @constructor
14
+ */
15
+ constructor(options) {
16
+ super();
17
+ this.db = options?.db;
18
+ }
19
+ /**
20
+ * Executes the provided function within a transaction.
21
+ *
22
+ * @param callback - The function to be executed within the transaction.
23
+ */
24
+ async withTransaction(callback) {
25
+ const ctx = this.context;
26
+ let closeSessionOnFinish = false;
27
+ let connection = ctx[transactionKey];
28
+ if (!connection) {
29
+ /** Determine the SqbClient or SqbConnection instance */
30
+ const db = await this.getConnection();
31
+ if (db instanceof SqbConnection) {
32
+ connection = db;
33
+ }
34
+ else {
35
+ /** Acquire a connection. New connection should be at the end */
36
+ connection = await db.acquire({ autoCommit: false });
37
+ closeSessionOnFinish = true;
38
+ }
39
+ /** Store transaction connection in current context */
40
+ ctx[transactionKey] = connection;
41
+ }
42
+ const oldInTransaction = connection.inTransaction;
43
+ connection.retain();
44
+ try {
45
+ if (!oldInTransaction)
46
+ await connection.startTransaction();
47
+ const out = await callback(connection, this);
48
+ if (!oldInTransaction && connection.inTransaction)
49
+ await connection.commit();
50
+ return out;
51
+ }
52
+ catch (e) {
53
+ if (!oldInTransaction && connection.inTransaction)
54
+ await connection.rollback();
55
+ throw e;
56
+ }
57
+ finally {
58
+ delete ctx[transactionKey];
59
+ /** Release connection */
60
+ if (closeSessionOnFinish) {
61
+ await connection.close();
62
+ }
63
+ else
64
+ connection.release();
65
+ }
66
+ }
67
+ /**
68
+ * Retrieves the database connection.
69
+ *
70
+ * @protected
71
+ *
72
+ * @throws {Error} If the context or database is not set.
73
+ */
74
+ getConnection() {
75
+ const ctx = this.context;
76
+ let db = ctx[transactionKey];
77
+ if (db)
78
+ return db;
79
+ db = typeof this.db === 'function' ? this.db(this) : this.db;
80
+ if (db)
81
+ return db;
82
+ throw new Error(`Database not set!`);
83
+ }
84
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/sqb",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
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.6",
14
- "@opra/http": "^1.0.6",
13
+ "@opra/core": "^1.0.8",
14
+ "@opra/http": "^1.0.8",
15
15
  "@sqb/connect": ">= 4.19.1"
16
16
  },
17
17
  "type": "module",
package/types/index.d.cts CHANGED
@@ -4,4 +4,5 @@ import './augmentation/mixin-type.augmentation.js';
4
4
  export * from './sqb-adapter.js';
5
5
  export * from './sqb-collection-service.js';
6
6
  export * from './sqb-entity-service.js';
7
+ export * from './sqb-service-base.js';
7
8
  export * from './sqb-singleton-service.js';
package/types/index.d.ts CHANGED
@@ -4,4 +4,5 @@ import './augmentation/mixin-type.augmentation.js';
4
4
  export * from './sqb-adapter.js';
5
5
  export * from './sqb-collection-service.js';
6
6
  export * from './sqb-entity-service.js';
7
+ export * from './sqb-service-base.js';
7
8
  export * from './sqb-singleton-service.js';
@@ -1,15 +1,15 @@
1
1
  import { ComplexType } from '@opra/common';
2
2
  import { ExecutionContext, ServiceBase } from '@opra/core';
3
- import { EntityMetadata, Repository, SqbClient, SqbConnection } from '@sqb/connect';
3
+ import { EntityMetadata, Repository } from '@sqb/connect';
4
4
  import type { Nullish, PartialDTO, PatchDTO, RequiredSome, StrictOmit, Type } from 'ts-gems';
5
5
  import { type IsObject } from 'valgen';
6
6
  import { SQBAdapter } from './sqb-adapter.js';
7
+ import { SqbServiceBase } from './sqb-service-base.js';
7
8
  /**
8
9
  * @namespace SqbEntityService
9
10
  */
10
11
  export declare namespace SqbEntityService {
11
- interface Options {
12
- db?: SqbEntityService<any>['db'];
12
+ interface Options extends SqbServiceBase.Options {
13
13
  resourceName?: SqbEntityService<any>['resourceName'];
14
14
  onError?: SqbEntityService<any>['onError'];
15
15
  commonFilter?: SqbEntityService<any>['commonFilter'];
@@ -153,17 +153,13 @@ export interface SqbEntityService {
153
153
  * @class SqbEntityService
154
154
  * @template T - The data type class type of the resource
155
155
  */
156
- export declare class SqbEntityService<T extends object = object> extends ServiceBase {
156
+ export declare class SqbEntityService<T extends object = object> extends SqbServiceBase {
157
157
  protected _dataType_: Type | string;
158
158
  protected _dataType?: ComplexType;
159
159
  protected _dataTypeClass?: Type;
160
160
  protected _entityMetadata?: EntityMetadata;
161
161
  protected _inputCodecs: Record<string, IsObject.Validator<T>>;
162
162
  protected _outputCodecs: Record<string, IsObject.Validator<T>>;
163
- /**
164
- * Represents a SqbClient or SqbConnection object
165
- */
166
- db?: (SqbClient | SqbConnection) | ((_this: this) => SqbClient | SqbConnection);
167
163
  /**
168
164
  * Represents the name of a resource.
169
165
  * @type {string}
@@ -209,12 +205,6 @@ export declare class SqbEntityService<T extends object = object> extends Service
209
205
  */
210
206
  get entityMetadata(): EntityMetadata;
211
207
  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>;
218
208
  /**
219
209
  * Retrieves the resource name.
220
210
  *
@@ -432,14 +422,6 @@ export declare class SqbEntityService<T extends object = object> extends Service
432
422
  * @protected
433
423
  */
434
424
  protected _dbUpdateMany(data: PatchDTO<T>, options?: Repository.UpdateManyOptions): Promise<number>;
435
- /**
436
- * Retrieves the database connection.
437
- *
438
- * @protected
439
- *
440
- * @throws {Error} If the context or database is not set.
441
- */
442
- getConnection(): SqbConnection | SqbClient | Promise<SqbConnection | SqbClient>;
443
425
  /**
444
426
  * Retrieves the common filter used for querying documents.
445
427
  * This method is mostly used for security issues like securing multi-tenant applications.
@@ -0,0 +1,41 @@
1
+ import { ServiceBase } from '@opra/core';
2
+ import { SqbClient, SqbConnection } from '@sqb/connect';
3
+ /**
4
+ * @namespace SqbServiceBase
5
+ */
6
+ export declare namespace SqbServiceBase {
7
+ interface Options {
8
+ db?: SqbServiceBase['db'];
9
+ }
10
+ }
11
+ /**
12
+ * @class SqbServiceBase
13
+ * @template T - The data type class type of the resource
14
+ */
15
+ export declare class SqbServiceBase extends ServiceBase {
16
+ /**
17
+ * Represents a SqbClient or SqbConnection object
18
+ */
19
+ db?: (SqbClient | SqbConnection) | ((_this: this) => SqbClient | SqbConnection);
20
+ /**
21
+ * Constructs a new instance
22
+ *
23
+ * @param [options] - The options for the service.
24
+ * @constructor
25
+ */
26
+ constructor(options?: SqbServiceBase.Options);
27
+ /**
28
+ * Executes the provided function within a transaction.
29
+ *
30
+ * @param callback - The function to be executed within the transaction.
31
+ */
32
+ withTransaction(callback: (connection: SqbConnection, _this: this) => any): Promise<any>;
33
+ /**
34
+ * Retrieves the database connection.
35
+ *
36
+ * @protected
37
+ *
38
+ * @throws {Error} If the context or database is not set.
39
+ */
40
+ getConnection(): SqbConnection | SqbClient | Promise<SqbConnection | SqbClient>;
41
+ }