@opra/sqb 1.0.5 → 1.0.7
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 +1 -0
- package/cjs/sqb-entity-service.js +3 -4
- package/cjs/sqb-service-base.js +88 -0
- package/esm/index.js +1 -0
- package/esm/sqb-entity-service.js +3 -4
- package/esm/sqb-service-base.js +84 -0
- package/package.json +3 -3
- package/types/index.d.cts +1 -0
- package/types/index.d.ts +1 -0
- package/types/sqb-entity-service.d.ts +3 -7
- package/types/sqb-service-base.d.ts +41 -0
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,17 @@
|
|
|
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");
|
|
9
|
+
const sqb_service_base_js_1 = require("./sqb-service-base.js");
|
|
10
10
|
const transactionKey = Symbol.for('transaction');
|
|
11
11
|
/**
|
|
12
12
|
* @class SqbEntityService
|
|
13
13
|
* @template T - The data type class type of the resource
|
|
14
14
|
*/
|
|
15
|
-
class SqbEntityService extends
|
|
15
|
+
class SqbEntityService extends sqb_service_base_js_1.SqbServiceBase {
|
|
16
16
|
/**
|
|
17
17
|
* Constructs a new instance
|
|
18
18
|
*
|
|
@@ -21,11 +21,10 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
21
21
|
* @constructor
|
|
22
22
|
*/
|
|
23
23
|
constructor(dataType, options) {
|
|
24
|
-
super();
|
|
24
|
+
super(options);
|
|
25
25
|
this._inputCodecs = {};
|
|
26
26
|
this._outputCodecs = {};
|
|
27
27
|
this._dataType_ = dataType;
|
|
28
|
-
this.db = options?.db;
|
|
29
28
|
this.resourceName = options?.resourceName;
|
|
30
29
|
this.commonFilter = options?.commonFilter;
|
|
31
30
|
this.interceptor = options?.interceptor;
|
|
@@ -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
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { InternalServerError } from '@opra/common';
|
|
2
|
-
import { ServiceBase } from '@opra/core';
|
|
3
2
|
import { op } from '@sqb/builder';
|
|
4
3
|
import { EntityMetadata, SqbConnection } from '@sqb/connect';
|
|
5
4
|
import { isNotNullish } from 'valgen';
|
|
6
5
|
import { SQBAdapter } from './sqb-adapter.js';
|
|
6
|
+
import { SqbServiceBase } from './sqb-service-base.js';
|
|
7
7
|
const transactionKey = Symbol.for('transaction');
|
|
8
8
|
/**
|
|
9
9
|
* @class SqbEntityService
|
|
10
10
|
* @template T - The data type class type of the resource
|
|
11
11
|
*/
|
|
12
|
-
export class SqbEntityService extends
|
|
12
|
+
export class SqbEntityService extends SqbServiceBase {
|
|
13
13
|
/**
|
|
14
14
|
* Constructs a new instance
|
|
15
15
|
*
|
|
@@ -18,11 +18,10 @@ export class SqbEntityService extends ServiceBase {
|
|
|
18
18
|
* @constructor
|
|
19
19
|
*/
|
|
20
20
|
constructor(dataType, options) {
|
|
21
|
-
super();
|
|
21
|
+
super(options);
|
|
22
22
|
this._inputCodecs = {};
|
|
23
23
|
this._outputCodecs = {};
|
|
24
24
|
this._dataType_ = dataType;
|
|
25
|
-
this.db = options?.db;
|
|
26
25
|
this.resourceName = options?.resourceName;
|
|
27
26
|
this.commonFilter = options?.commonFilter;
|
|
28
27
|
this.interceptor = options?.interceptor;
|
|
@@ -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.
|
|
3
|
+
"version": "1.0.7",
|
|
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.
|
|
14
|
-
"@opra/http": "^1.0.
|
|
13
|
+
"@opra/core": "^1.0.7",
|
|
14
|
+
"@opra/http": "^1.0.7",
|
|
15
15
|
"@sqb/connect": ">= 4.19.1"
|
|
16
16
|
},
|
|
17
17
|
"type": "module",
|
package/types/index.d.cts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -4,12 +4,12 @@ import { EntityMetadata, Repository, SqbClient, SqbConnection } from '@sqb/conne
|
|
|
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
|
|
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}
|
|
@@ -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
|
+
}
|