@opra/sqb 1.28.5 → 1.29.0
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/README.md +6 -2
- package/package.json +4 -4
- package/sqb-service-base.d.ts +1 -1
- package/sqb-service-base.js +35 -9
package/README.md
CHANGED
|
@@ -50,11 +50,15 @@ export class OrdersController extends SqbCollectionService<Order> {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
@HttpOperation.Entity.FindMany({ type: Order })
|
|
53
|
-
findMany() {
|
|
53
|
+
findMany() {
|
|
54
|
+
return super.findMany();
|
|
55
|
+
}
|
|
54
56
|
|
|
55
57
|
@HttpOperation.Entity.Create({ type: Order })
|
|
56
58
|
async create(dto: CreateOrderDto) {
|
|
57
|
-
return this.withTransaction(conn =>
|
|
59
|
+
return this.withTransaction(conn =>
|
|
60
|
+
super.create(dto, { connection: conn }),
|
|
61
|
+
);
|
|
58
62
|
}
|
|
59
63
|
}
|
|
60
64
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/sqb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.29.0",
|
|
4
4
|
"description": "Opra SQB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"reflect-metadata": "^0.2.2",
|
|
10
10
|
"tslib": "^2.8.1",
|
|
11
|
-
"valgen": "^6.2.
|
|
11
|
+
"valgen": "^6.2.2"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"@opra/core": "^1.
|
|
15
|
-
"@opra/http": "^1.
|
|
14
|
+
"@opra/core": "^1.29.0",
|
|
15
|
+
"@opra/http": "^1.29.0",
|
|
16
16
|
"@sqb/builder": ">=5.0.1 <6",
|
|
17
17
|
"@sqb/connect": ">=5.0.1 <6"
|
|
18
18
|
},
|
package/sqb-service-base.d.ts
CHANGED
|
@@ -41,5 +41,5 @@ export declare class SqbServiceBase extends ServiceBase {
|
|
|
41
41
|
*
|
|
42
42
|
* @throws If no database connection is configured.
|
|
43
43
|
*/
|
|
44
|
-
getConnection():
|
|
44
|
+
getConnection(): Promise<SqbConnection | SqbClient>;
|
|
45
45
|
}
|
package/sqb-service-base.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ServiceBase } from '@opra/core';
|
|
2
2
|
import { SqbClient, SqbConnection } from '@sqb/connect';
|
|
3
|
-
const
|
|
3
|
+
const dbSessionKey = Symbol.for('db-session');
|
|
4
4
|
/**
|
|
5
5
|
* Base class for services using SQB (SQL Builder) for database operations.
|
|
6
6
|
*/
|
|
@@ -28,7 +28,7 @@ export class SqbServiceBase extends ServiceBase {
|
|
|
28
28
|
async withTransaction(callback) {
|
|
29
29
|
const ctx = this.context;
|
|
30
30
|
let closeSessionOnFinish = false;
|
|
31
|
-
let connection = ctx[
|
|
31
|
+
let connection = ctx[dbSessionKey] || ctx.bundle?.[dbSessionKey];
|
|
32
32
|
if (!connection) {
|
|
33
33
|
/* Determine the SqbClient or SqbConnection instance */
|
|
34
34
|
const db = await this.getConnection();
|
|
@@ -41,7 +41,7 @@ export class SqbServiceBase extends ServiceBase {
|
|
|
41
41
|
closeSessionOnFinish = true;
|
|
42
42
|
}
|
|
43
43
|
/* Store transaction connection in current context */
|
|
44
|
-
ctx[
|
|
44
|
+
ctx[dbSessionKey] = connection;
|
|
45
45
|
}
|
|
46
46
|
const oldInTransaction = connection.inTransaction;
|
|
47
47
|
connection.retain();
|
|
@@ -59,7 +59,7 @@ export class SqbServiceBase extends ServiceBase {
|
|
|
59
59
|
throw e;
|
|
60
60
|
}
|
|
61
61
|
finally {
|
|
62
|
-
delete ctx[
|
|
62
|
+
delete ctx[dbSessionKey];
|
|
63
63
|
/* Release connection */
|
|
64
64
|
if (closeSessionOnFinish) {
|
|
65
65
|
await connection.close();
|
|
@@ -73,14 +73,40 @@ export class SqbServiceBase extends ServiceBase {
|
|
|
73
73
|
*
|
|
74
74
|
* @throws If no database connection is configured.
|
|
75
75
|
*/
|
|
76
|
-
getConnection() {
|
|
76
|
+
async getConnection() {
|
|
77
77
|
const ctx = this.context;
|
|
78
|
-
let db = ctx[
|
|
78
|
+
let db = ctx[dbSessionKey] || ctx.bundle?.[dbSessionKey];
|
|
79
79
|
if (db)
|
|
80
80
|
return db;
|
|
81
81
|
db = typeof this.db === 'function' ? this.db(this) : this.db;
|
|
82
|
-
if (db)
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
if (!db)
|
|
83
|
+
throw new Error(`Database not set!`);
|
|
84
|
+
if (ctx.bundle?.transaction) {
|
|
85
|
+
const connection = db instanceof SqbConnection
|
|
86
|
+
? db
|
|
87
|
+
: await db.acquire({ autoCommit: false });
|
|
88
|
+
/* Start transaction */
|
|
89
|
+
if (!connection.inTransaction) {
|
|
90
|
+
await connection.startTransaction();
|
|
91
|
+
}
|
|
92
|
+
ctx.bundle[dbSessionKey] = connection;
|
|
93
|
+
/* Commit or Rollback transaction after executed */
|
|
94
|
+
ctx.bundle.on('after-execute', async () => {
|
|
95
|
+
delete ctx.bundle[dbSessionKey];
|
|
96
|
+
try {
|
|
97
|
+
if (connection.inTransaction) {
|
|
98
|
+
if (ctx.bundle.success)
|
|
99
|
+
await connection.commit();
|
|
100
|
+
else
|
|
101
|
+
await connection.rollback();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
await connection.close();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return connection;
|
|
109
|
+
}
|
|
110
|
+
return db;
|
|
85
111
|
}
|
|
86
112
|
}
|