@holo-js/db-sqlite 0.3.1 → 0.3.3
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 +7 -0
- package/dist/index.mjs +31 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,9 @@ type SQLiteTransactionMode = 'deferred' | 'immediate' | 'exclusive';
|
|
|
5
5
|
type SQLiteTransactionOptions = DatabaseOperationOptions & {
|
|
6
6
|
readonly mode?: SQLiteTransactionMode;
|
|
7
7
|
};
|
|
8
|
+
type SQLiteMigrationTransactionState = {
|
|
9
|
+
readonly foreignKeysWereEnabled: boolean;
|
|
10
|
+
};
|
|
8
11
|
interface SQLiteStatementLike {
|
|
9
12
|
all(...params: readonly unknown[]): Record<string, unknown>[];
|
|
10
13
|
run(...params: readonly unknown[]): {
|
|
@@ -26,6 +29,7 @@ declare class SQLiteAdapter implements DriverAdapter {
|
|
|
26
29
|
private database?;
|
|
27
30
|
private connected;
|
|
28
31
|
private transactionTail;
|
|
32
|
+
private readonly transactionScope;
|
|
29
33
|
private readonly filename;
|
|
30
34
|
private readonly createDatabaseInstance;
|
|
31
35
|
constructor(options?: SQLiteAdapterOptions);
|
|
@@ -33,6 +37,9 @@ declare class SQLiteAdapter implements DriverAdapter {
|
|
|
33
37
|
disconnect(): Promise<void>;
|
|
34
38
|
isConnected(): boolean;
|
|
35
39
|
runWithTransactionScope<T>(callback: () => Promise<T>): Promise<T>;
|
|
40
|
+
beforeMigrationTransaction(): Promise<SQLiteMigrationTransactionState>;
|
|
41
|
+
validateMigrationTransaction(): Promise<void>;
|
|
42
|
+
afterMigrationTransaction(state: unknown): Promise<void>;
|
|
36
43
|
query<TRow extends Record<string, unknown> = Record<string, unknown>>(sql: string, bindings?: readonly unknown[]): Promise<DriverQueryResult<TRow>>;
|
|
37
44
|
introspect<TRow extends Record<string, unknown> = Record<string, unknown>>(sql: string, bindings?: readonly unknown[]): Promise<DriverQueryResult<TRow>>;
|
|
38
45
|
execute(sql: string, bindings?: readonly unknown[]): Promise<DriverExecutionResult>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2
3
|
import Database from "better-sqlite3";
|
|
4
|
+
import {
|
|
5
|
+
DatabaseError
|
|
6
|
+
} from "@holo-js/db";
|
|
3
7
|
var TransactionError = class extends Error {
|
|
4
8
|
};
|
|
5
9
|
var SQLiteAdapter = class {
|
|
6
10
|
database;
|
|
7
11
|
connected;
|
|
8
12
|
transactionTail = Promise.resolve();
|
|
13
|
+
transactionScope = new AsyncLocalStorage();
|
|
9
14
|
filename;
|
|
10
15
|
createDatabaseInstance;
|
|
11
16
|
constructor(options = {}) {
|
|
@@ -38,6 +43,9 @@ var SQLiteAdapter = class {
|
|
|
38
43
|
return this.connected;
|
|
39
44
|
}
|
|
40
45
|
async runWithTransactionScope(callback) {
|
|
46
|
+
if (this.transactionScope.getStore()) {
|
|
47
|
+
return callback();
|
|
48
|
+
}
|
|
41
49
|
const previous = this.transactionTail;
|
|
42
50
|
let release;
|
|
43
51
|
const current = previous.then(() => new Promise((resolve) => {
|
|
@@ -46,7 +54,7 @@ var SQLiteAdapter = class {
|
|
|
46
54
|
this.transactionTail = current;
|
|
47
55
|
await previous;
|
|
48
56
|
try {
|
|
49
|
-
return await callback
|
|
57
|
+
return await this.transactionScope.run(true, callback);
|
|
50
58
|
} finally {
|
|
51
59
|
release();
|
|
52
60
|
if (this.transactionTail === current) {
|
|
@@ -54,6 +62,28 @@ var SQLiteAdapter = class {
|
|
|
54
62
|
}
|
|
55
63
|
}
|
|
56
64
|
}
|
|
65
|
+
async beforeMigrationTransaction() {
|
|
66
|
+
const row = this.getDatabase().prepare("PRAGMA foreign_keys").all()[0];
|
|
67
|
+
const foreignKeysWereEnabled = row?.foreign_keys === 1;
|
|
68
|
+
if (foreignKeysWereEnabled) {
|
|
69
|
+
this.getDatabase().exec("PRAGMA foreign_keys = OFF");
|
|
70
|
+
}
|
|
71
|
+
return { foreignKeysWereEnabled };
|
|
72
|
+
}
|
|
73
|
+
async validateMigrationTransaction() {
|
|
74
|
+
const violations = this.getDatabase().prepare("PRAGMA foreign_key_check").all();
|
|
75
|
+
if (violations.length > 0) {
|
|
76
|
+
throw new DatabaseError(
|
|
77
|
+
`SQLite migration left ${violations.length} foreign key violation${violations.length === 1 ? "" : "s"}.`,
|
|
78
|
+
"SQLITE_MIGRATION_FOREIGN_KEY_VIOLATION"
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async afterMigrationTransaction(state) {
|
|
83
|
+
if (typeof state === "object" && state !== null && "foreignKeysWereEnabled" in state && state.foreignKeysWereEnabled === true) {
|
|
84
|
+
this.getDatabase().exec("PRAGMA foreign_keys = ON");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
57
87
|
async query(sql, bindings = []) {
|
|
58
88
|
const statement = this.getDatabase().prepare(sql);
|
|
59
89
|
const rows = statement.all(...bindings);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holo-js/db-sqlite",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Holo-JS Framework - SQLite database adapter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,13 +23,13 @@
|
|
|
23
23
|
"test": "vitest --run"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@holo-js/db": "^0.3.
|
|
26
|
+
"@holo-js/db": "^0.3.3"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"better-sqlite3": "^11.7.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@holo-js/db": "^0.3.
|
|
32
|
+
"@holo-js/db": "^0.3.3",
|
|
33
33
|
"@types/better-sqlite3": "^7.6.12",
|
|
34
34
|
"tsup": "^8.3.5",
|
|
35
35
|
"typescript": "^5.7.2",
|