@opra/sqb 1.28.4 → 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/LICENSE +1 -1
- package/README.md +58 -3
- package/package.json +5 -4
- package/sqb-service-base.d.ts +1 -1
- package/sqb-service-base.js +35 -9
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,19 +1,74 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<a href="https://oprajs.com">
|
|
4
|
+
<img src="https://oprajs.com/img/opra-header-block.webp" width="880" alt="OPRA — Open Platform for Rich APIs" />
|
|
5
|
+
</a>
|
|
6
|
+
|
|
1
7
|
# @opra/sqb
|
|
2
8
|
|
|
9
|
+
SQL data service adapter for the OPRA framework, powered by SQB
|
|
10
|
+
|
|
3
11
|
[![NPM Version][npm-image]][npm-url]
|
|
4
12
|
[![NPM Downloads][downloads-image]][downloads-url]
|
|
5
13
|
[![CI Tests][ci-test-image]][ci-test-url]
|
|
6
14
|
[![Test Coverage][coveralls-image]][coveralls-url]
|
|
7
15
|
|
|
16
|
+
[🌐 Documentation](https://oprajs.com) · [🚀 Getting Started](https://oprajs.com/docs/introduction) · [📦 Packages](https://github.com/panates/opra#packages) · [💬 Issues](https://github.com/panates/opra/issues)
|
|
17
|
+
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
SQL data service adapter for the [OPRA](https://oprajs.com) framework, powered by [SQB](https://github.com/panates/sqb). Connect your relational database to OPRA's operation model with full transaction support.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- **`SqbServiceBase`** — Base service managing SqbClient or SqbConnection with transaction support
|
|
27
|
+
- **`SqbCollectionService`** — Table-level CRUD service with automatic query generation
|
|
28
|
+
- **`SqbEntityService`** — Row-level service for single entity operations
|
|
29
|
+
- **`SQBAdapter`** — Utility namespace: `prepareFilter()`, `parseRequest()`
|
|
30
|
+
- Automatic translation of OPRA filter DSL to SQL WHERE clauses
|
|
31
|
+
- `withTransaction()` helper for multi-step atomic operations
|
|
32
|
+
- Compatible with PostgreSQL, MySQL, SQLite, and other SQB-supported databases
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
8
35
|
|
|
9
|
-
|
|
10
|
-
|
|
36
|
+
```bash
|
|
37
|
+
npm install @opra/sqb
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { SqbCollectionService } from '@opra/sqb';
|
|
44
|
+
import { SqbClient } from '@sqb/connect';
|
|
45
|
+
|
|
46
|
+
@HttpController({ path: 'orders' })
|
|
47
|
+
export class OrdersController extends SqbCollectionService<Order> {
|
|
48
|
+
constructor(client: SqbClient) {
|
|
49
|
+
super(Order, client, 'orders');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@HttpOperation.Entity.FindMany({ type: Order })
|
|
53
|
+
findMany() {
|
|
54
|
+
return super.findMany();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@HttpOperation.Entity.Create({ type: Order })
|
|
58
|
+
async create(dto: CreateOrderDto) {
|
|
59
|
+
return this.withTransaction(conn =>
|
|
60
|
+
super.create(dto, { connection: conn }),
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
11
65
|
|
|
12
66
|
## Node Compatibility
|
|
13
|
-
- node >= 20.x
|
|
14
67
|
|
|
68
|
+
- node >= 20.x
|
|
15
69
|
|
|
16
70
|
## License
|
|
71
|
+
|
|
17
72
|
Available under [MIT](LICENSE) license.
|
|
18
73
|
|
|
19
74
|
[npm-image]: https://img.shields.io/npm/v/@opra/sqb
|
package/package.json
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
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",
|
|
7
|
+
"homepage": "https://www.oprajs.com",
|
|
7
8
|
"dependencies": {
|
|
8
9
|
"reflect-metadata": "^0.2.2",
|
|
9
10
|
"tslib": "^2.8.1",
|
|
10
|
-
"valgen": "^6.2.
|
|
11
|
+
"valgen": "^6.2.2"
|
|
11
12
|
},
|
|
12
13
|
"peerDependencies": {
|
|
13
|
-
"@opra/core": "^1.
|
|
14
|
-
"@opra/http": "^1.
|
|
14
|
+
"@opra/core": "^1.29.0",
|
|
15
|
+
"@opra/http": "^1.29.0",
|
|
15
16
|
"@sqb/builder": ">=5.0.1 <6",
|
|
16
17
|
"@sqb/connect": ">=5.0.1 <6"
|
|
17
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
|
}
|