@hexaijs/sqlite 0.2.1 → 0.3.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 +27 -10
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -56,10 +56,11 @@ Unlike PostgreSQL's unit of work which accepts a client factory, `SqliteUnitOfWo
|
|
|
56
56
|
|
|
57
57
|
### Transaction Execution
|
|
58
58
|
|
|
59
|
-
Use `
|
|
59
|
+
Use `scope()` to define a transaction boundary:
|
|
60
60
|
|
|
61
61
|
```typescript
|
|
62
|
-
const result = await unitOfWork.
|
|
62
|
+
const result = await unitOfWork.scope(async () => {
|
|
63
|
+
const db = unitOfWork.getClient();
|
|
63
64
|
await db.run("INSERT INTO orders (id, status) VALUES (?, ?)", [orderId, "pending"]);
|
|
64
65
|
await db.run("INSERT INTO order_items (order_id, product_id) VALUES (?, ?)", [orderId, productId]);
|
|
65
66
|
return { orderId };
|
|
@@ -67,11 +68,22 @@ const result = await unitOfWork.wrap(async (db) => {
|
|
|
67
68
|
// Transaction commits if successful
|
|
68
69
|
```
|
|
69
70
|
|
|
71
|
+
`wrap()` is also available but deprecated. In SQLite, `scope()` delegates to `wrap()` internally — both behave identically.
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
/** @deprecated Use scope() instead. */
|
|
75
|
+
const result = await unitOfWork.wrap(async (db) => {
|
|
76
|
+
await db.run("INSERT INTO orders (id, status) VALUES (?, ?)", [orderId, "pending"]);
|
|
77
|
+
return { orderId };
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
70
81
|
If an error is thrown, the transaction rolls back:
|
|
71
82
|
|
|
72
83
|
```typescript
|
|
73
84
|
try {
|
|
74
|
-
await unitOfWork.
|
|
85
|
+
await unitOfWork.scope(async () => {
|
|
86
|
+
const db = unitOfWork.getClient();
|
|
75
87
|
await db.run("INSERT INTO orders (id, status) VALUES (?, ?)", [orderId, "pending"]);
|
|
76
88
|
throw new Error("Something went wrong");
|
|
77
89
|
});
|
|
@@ -90,17 +102,19 @@ const db = ctx.getUnitOfWork().getClient();
|
|
|
90
102
|
await db.run("UPDATE orders SET status = ? WHERE id = ?", ["confirmed", orderId]);
|
|
91
103
|
```
|
|
92
104
|
|
|
93
|
-
Note: `getClient()` throws an error if called outside of a `wrap()` call.
|
|
105
|
+
Note: `getClient()` throws an error if called outside of a `scope()` or `wrap()` call.
|
|
94
106
|
|
|
95
107
|
### Nested Transactions
|
|
96
108
|
|
|
97
|
-
Nested `
|
|
109
|
+
Nested `scope()` calls participate in the same transaction:
|
|
98
110
|
|
|
99
111
|
```typescript
|
|
100
|
-
await unitOfWork.
|
|
112
|
+
await unitOfWork.scope(async () => {
|
|
113
|
+
const db = unitOfWork.getClient();
|
|
101
114
|
await db.run("INSERT INTO orders (id) VALUES (?)", ["order-1"]);
|
|
102
115
|
|
|
103
|
-
await unitOfWork.
|
|
116
|
+
await unitOfWork.scope(async () => {
|
|
117
|
+
const db = unitOfWork.getClient();
|
|
104
118
|
await db.run("INSERT INTO order_items (order_id) VALUES (?)", ["order-1"]);
|
|
105
119
|
});
|
|
106
120
|
// Both inserts are in the same transaction
|
|
@@ -112,10 +126,12 @@ If any nested call throws, the entire transaction rolls back:
|
|
|
112
126
|
|
|
113
127
|
```typescript
|
|
114
128
|
try {
|
|
115
|
-
await unitOfWork.
|
|
129
|
+
await unitOfWork.scope(async () => {
|
|
130
|
+
const db = unitOfWork.getClient();
|
|
116
131
|
await db.run("INSERT INTO orders (id) VALUES (?)", ["order-1"]);
|
|
117
132
|
|
|
118
|
-
await unitOfWork.
|
|
133
|
+
await unitOfWork.scope(async () => {
|
|
134
|
+
const db = unitOfWork.getClient();
|
|
119
135
|
await db.run("INSERT INTO order_items (order_id) VALUES (?)", ["order-1"]);
|
|
120
136
|
throw new Error("Nested failure");
|
|
121
137
|
});
|
|
@@ -160,7 +176,8 @@ describe("OrderRepository", () => {
|
|
|
160
176
|
});
|
|
161
177
|
|
|
162
178
|
it("should persist orders", async () => {
|
|
163
|
-
await unitOfWork.
|
|
179
|
+
await unitOfWork.scope(async () => {
|
|
180
|
+
const db = unitOfWork.getClient();
|
|
164
181
|
await db.run("INSERT INTO orders (id, status) VALUES (?, ?)", ["order-1", "pending"]);
|
|
165
182
|
});
|
|
166
183
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/sqlite-unit-of-work.ts"],"names":[],"mappings":";AAIO,IAAM,gBAAA,GAAN,MAAM,iBAAA,CAAiD;AAAA,EAS1D,YAAoB,EAAA,EAAc;AAAd,IAAA,IAAA,CAAA,EAAA,GAAA,EAAA;AAChB,IAAA,IAAI,CAAC,iBAAA,CAAiB,YAAA,CAAa,GAAA,CAAI,EAAE,CAAA,EAAG;AACxC,MAAA,iBAAA,CAAiB,YAAA,CAAa,IAAI,EAAA,EAAI;AAAA,QAClC,KAAA,EAAO,CAAA;AAAA,QACP,OAAA,EAAS;AAAA,OACZ,CAAA;AAAA,IACL;AAAA,EACJ;AAAA,EAfA,OAAe,YAAA,mBAAe,IAAI,OAAA,EAMhC;AAAA,EAWF,SAAA,GAAsB;AAClB,IAAA,MAAM,OAAA,GAAU,iBAAA,CAAiB,YAAA,CAAa,GAAA,CAAI,KAAK,EAAE,CAAA;AACzD,IAAA,IAAI,CAAC,OAAA,IAAW,OAAA,CAAQ,KAAA,KAAU,CAAA,EAAG;AACjC,MAAA,MAAM,IAAI,MAAM,0BAA0B,CAAA;AAAA,IAC9C;AACA,IAAA,OAAO,IAAA,CAAK,EAAA;AAAA,EAChB;AAAA,EAEA,MAAM,KAAQ,EAAA,EAAkD;AAC5D,IAAA,MAAM,OAAA,GAAU,iBAAA,CAAiB,YAAA,CAAa,GAAA,CAAI,KAAK,EAAE,CAAA;AACzD,IAAA,IAAI,EAAE,OAAA,CAAQ,KAAA,KAAU,CAAA,EAAG;AACvB,MAAA,MAAM,IAAA,CAAK,EAAA,CAAG,GAAA,CAAI,mBAAmB,CAAA;AAAA,IACzC;AAEA,IAAA,IAAI;AACA,MAAA,OAAO,MAAM,EAAA,CAAG,IAAA,CAAK,EAAE,CAAA;AAAA,IAC3B,SAAS,CAAA,EAAG;AACR,MAAA,IAAI,CAAC,QAAQ,OAAA,EAAS;AAClB,QAAA,OAAA,CAAQ,OAAA,GAAU,IAAA;AAAA,MACtB;AAEA,MAAA,MAAM,CAAA;AAAA,IACV,CAAA,SAAE;AACE,MAAA,IAAI,EAAE,OAAA,CAAQ,KAAA,KAAU,CAAA,EAAG;AACvB,QAAA,IAAI,QAAQ,OAAA,EAAS;AACjB,UAAA,MAAM,IAAA,CAAK,EAAA,CAAG,GAAA,CAAI,UAAU,CAAA;AAAA,QAChC,CAAA,MAAO;AACH,UAAA,MAAM,IAAA,CAAK,EAAA,CAAG,GAAA,CAAI,QAAQ,CAAA;AAAA,QAC9B;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AACJ","file":"index.js","sourcesContent":["import type { Database } from \"sqlite\";\n\nimport { UnitOfWork } from \"@hexaijs/core\";\n\nexport class SqliteUnitOfWork implements UnitOfWork<Database> {\n private static transactions = new WeakMap<\n Database,\n {\n level: number;\n aborted: boolean;\n }\n >();\n\n constructor(private db: Database) {\n if (!SqliteUnitOfWork.transactions.has(db)) {\n SqliteUnitOfWork.transactions.set(db, {\n level: 0,\n aborted: false,\n });\n }\n }\n\n getClient(): Database {\n const current = SqliteUnitOfWork.transactions.get(this.db);\n if (!current || current.level === 0) {\n throw new Error(\"No transaction is active\");\n }\n return this.db;\n }\n\n async wrap<T>(fn: (client: Database) => Promise<T>): Promise<T> {\n const current = SqliteUnitOfWork.transactions.get(this.db)!;\n if (++current.level === 1) {\n await this.db.run(\"BEGIN TRANSACTION\");\n }\n\n try {\n return await fn(this.db);\n } catch (e) {\n if (!current.aborted) {\n current.aborted = true;\n }\n\n throw e;\n } finally {\n if (--current.level === 0) {\n if (current.aborted) {\n await this.db.run(\"ROLLBACK\");\n } else {\n await this.db.run(\"COMMIT\");\n }\n }\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/sqlite-unit-of-work.ts"],"names":[],"mappings":";AAIO,IAAM,gBAAA,GAAN,MAAM,iBAAA,CAAiD;AAAA,EAS1D,YAAoB,EAAA,EAAc;AAAd,IAAA,IAAA,CAAA,EAAA,GAAA,EAAA;AAChB,IAAA,IAAI,CAAC,iBAAA,CAAiB,YAAA,CAAa,GAAA,CAAI,EAAE,CAAA,EAAG;AACxC,MAAA,iBAAA,CAAiB,YAAA,CAAa,IAAI,EAAA,EAAI;AAAA,QAClC,KAAA,EAAO,CAAA;AAAA,QACP,OAAA,EAAS;AAAA,OACZ,CAAA;AAAA,IACL;AAAA,EACJ;AAAA,EAfA,OAAe,YAAA,mBAAe,IAAI,OAAA,EAMhC;AAAA,EAWF,SAAA,GAAsB;AAClB,IAAA,MAAM,OAAA,GAAU,iBAAA,CAAiB,YAAA,CAAa,GAAA,CAAI,KAAK,EAAE,CAAA;AACzD,IAAA,IAAI,CAAC,OAAA,IAAW,OAAA,CAAQ,KAAA,KAAU,CAAA,EAAG;AACjC,MAAA,MAAM,IAAI,MAAM,0BAA0B,CAAA;AAAA,IAC9C;AACA,IAAA,OAAO,IAAA,CAAK,EAAA;AAAA,EAChB;AAAA,EAEA,MAAM,MAAS,EAAA,EAAkC;AAC7C,IAAA,OAAO,IAAA,CAAK,KAAK,EAAE,CAAA;AAAA,EACvB;AAAA,EAEA,MAAM,KAAQ,EAAA,EAAkD;AAC5D,IAAA,MAAM,OAAA,GAAU,iBAAA,CAAiB,YAAA,CAAa,GAAA,CAAI,KAAK,EAAE,CAAA;AACzD,IAAA,IAAI,EAAE,OAAA,CAAQ,KAAA,KAAU,CAAA,EAAG;AACvB,MAAA,MAAM,IAAA,CAAK,EAAA,CAAG,GAAA,CAAI,mBAAmB,CAAA;AAAA,IACzC;AAEA,IAAA,IAAI;AACA,MAAA,OAAO,MAAM,EAAA,CAAG,IAAA,CAAK,EAAE,CAAA;AAAA,IAC3B,SAAS,CAAA,EAAG;AACR,MAAA,IAAI,CAAC,QAAQ,OAAA,EAAS;AAClB,QAAA,OAAA,CAAQ,OAAA,GAAU,IAAA;AAAA,MACtB;AAEA,MAAA,MAAM,CAAA;AAAA,IACV,CAAA,SAAE;AACE,MAAA,IAAI,EAAE,OAAA,CAAQ,KAAA,KAAU,CAAA,EAAG;AACvB,QAAA,IAAI,QAAQ,OAAA,EAAS;AACjB,UAAA,MAAM,IAAA,CAAK,EAAA,CAAG,GAAA,CAAI,UAAU,CAAA;AAAA,QAChC,CAAA,MAAO;AACH,UAAA,MAAM,IAAA,CAAK,EAAA,CAAG,GAAA,CAAI,QAAQ,CAAA;AAAA,QAC9B;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AACJ","file":"index.js","sourcesContent":["import type { Database } from \"sqlite\";\n\nimport { UnitOfWork } from \"@hexaijs/core\";\n\nexport class SqliteUnitOfWork implements UnitOfWork<Database> {\n private static transactions = new WeakMap<\n Database,\n {\n level: number;\n aborted: boolean;\n }\n >();\n\n constructor(private db: Database) {\n if (!SqliteUnitOfWork.transactions.has(db)) {\n SqliteUnitOfWork.transactions.set(db, {\n level: 0,\n aborted: false,\n });\n }\n }\n\n getClient(): Database {\n const current = SqliteUnitOfWork.transactions.get(this.db);\n if (!current || current.level === 0) {\n throw new Error(\"No transaction is active\");\n }\n return this.db;\n }\n\n async scope<T>(fn: () => Promise<T>): Promise<T> {\n return this.wrap(fn);\n }\n\n async wrap<T>(fn: (client: Database) => Promise<T>): Promise<T> {\n const current = SqliteUnitOfWork.transactions.get(this.db)!;\n if (++current.level === 1) {\n await this.db.run(\"BEGIN TRANSACTION\");\n }\n\n try {\n return await fn(this.db);\n } catch (e) {\n if (!current.aborted) {\n current.aborted = true;\n }\n\n throw e;\n } finally {\n if (--current.level === 0) {\n if (current.aborted) {\n await this.db.run(\"ROLLBACK\");\n } else {\n await this.db.run(\"COMMIT\");\n }\n }\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.3.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"description": "SQLite support for hexai",
|
|
9
9
|
"license": "MIT",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@hexaijs/core": "^0.
|
|
53
|
+
"@hexaijs/core": "^0.7.0",
|
|
54
54
|
"sqlite": "^5.1.1",
|
|
55
55
|
"sqlite3": "^5.1.7"
|
|
56
56
|
},
|