@holo-js/db-sqlite 0.3.21 → 0.3.22

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/dist/index.d.ts CHANGED
@@ -28,7 +28,7 @@ interface SQLiteAdapterOptions {
28
28
  declare class SQLiteAdapter implements DriverAdapter {
29
29
  private database?;
30
30
  private connected;
31
- private transactionTail;
31
+ private readonly transactionKey;
32
32
  private readonly transactionScope;
33
33
  private readonly filename;
34
34
  private readonly createDatabaseInstance;
package/dist/index.mjs CHANGED
@@ -1,15 +1,17 @@
1
1
  // src/index.ts
2
2
  import { AsyncLocalStorage } from "async_hooks";
3
+ import { resolve } from "path";
3
4
  import Database from "better-sqlite3";
4
5
  import {
5
6
  DatabaseError
6
7
  } from "@holo-js/db";
7
8
  var TransactionError = class extends Error {
8
9
  };
10
+ var transactionTails = /* @__PURE__ */ new Map();
9
11
  var SQLiteAdapter = class {
10
12
  database;
11
13
  connected;
12
- transactionTail = Promise.resolve();
14
+ transactionKey;
13
15
  transactionScope = new AsyncLocalStorage();
14
16
  filename;
15
17
  createDatabaseInstance;
@@ -17,7 +19,17 @@ var SQLiteAdapter = class {
17
19
  this.database = options.database;
18
20
  this.connected = !!options.database;
19
21
  this.filename = options.filename ?? ":memory:";
20
- this.createDatabaseInstance = options.createDatabase ?? ((filename) => new Database(filename));
22
+ this.transactionKey = options.database || this.filename === ":memory:" ? this : resolve(this.filename);
23
+ this.createDatabaseInstance = options.createDatabase ?? ((filename) => {
24
+ const database = new Database(filename);
25
+ try {
26
+ database.exec("PRAGMA journal_mode = WAL");
27
+ return database;
28
+ } catch (error) {
29
+ database.close();
30
+ throw error;
31
+ }
32
+ });
21
33
  }
22
34
  async initialize() {
23
35
  if (this.connected) {
@@ -46,20 +58,18 @@ var SQLiteAdapter = class {
46
58
  if (this.transactionScope.getStore()) {
47
59
  return callback();
48
60
  }
49
- const previous = this.transactionTail;
61
+ const previous = transactionTails.get(this.transactionKey) ?? Promise.resolve();
50
62
  let release;
51
- const current = previous.then(() => new Promise((resolve) => {
52
- release = resolve;
63
+ const current = previous.then(() => new Promise((resolve2) => {
64
+ release = resolve2;
53
65
  }));
54
- this.transactionTail = current;
66
+ transactionTails.set(this.transactionKey, current);
55
67
  await previous;
56
68
  try {
57
69
  return await this.transactionScope.run(true, callback);
58
70
  } finally {
59
71
  release();
60
- if (this.transactionTail === current) {
61
- this.transactionTail = Promise.resolve();
62
- }
72
+ if (transactionTails.get(this.transactionKey) === current) transactionTails.delete(this.transactionKey);
63
73
  }
64
74
  }
65
75
  async beforeMigrationTransaction() {
@@ -96,12 +106,14 @@ var SQLiteAdapter = class {
96
106
  return this.query(sql, bindings);
97
107
  }
98
108
  async execute(sql, bindings = []) {
99
- const statement = this.getDatabase().prepare(sql);
100
- const result = statement.run(...bindings);
101
- return {
102
- affectedRows: result.changes,
103
- lastInsertId: typeof result.lastInsertRowid === "bigint" ? Number(result.lastInsertRowid) : result.lastInsertRowid
104
- };
109
+ return this.runWithTransactionScope(async () => {
110
+ const statement = this.getDatabase().prepare(sql);
111
+ const result = statement.run(...bindings);
112
+ return {
113
+ affectedRows: result.changes,
114
+ lastInsertId: typeof result.lastInsertRowid === "bigint" ? Number(result.lastInsertRowid) : result.lastInsertRowid
115
+ };
116
+ });
105
117
  }
106
118
  async beginTransaction(options) {
107
119
  this.getDatabase().exec(this.resolveBeginStatement(options?.mode));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holo-js/db-sqlite",
3
- "version": "0.3.21",
3
+ "version": "0.3.22",
4
4
  "description": "Holo-JS Framework - SQLite database adapter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -26,13 +26,13 @@
26
26
  "node": "^22.12.0 || ^24.0.0 || ^26.0.0"
27
27
  },
28
28
  "peerDependencies": {
29
- "@holo-js/db": "^0.3.21"
29
+ "@holo-js/db": "^0.3.22"
30
30
  },
31
31
  "dependencies": {
32
32
  "better-sqlite3": "^13.0.3"
33
33
  },
34
34
  "devDependencies": {
35
- "@holo-js/db": "^0.3.21",
35
+ "@holo-js/db": "^0.3.22",
36
36
  "@types/better-sqlite3": "^7.6.12",
37
37
  "tsup": "^8.3.5",
38
38
  "typescript": "^5.7.2",