@arcote.tech/arc-adapter-db-sqlite-wasm 0.3.2 → 0.4.1
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.js +4729 -0
- package/dist/worker.js +11158 -0
- package/package.json +4 -1
- package/src/index.ts +0 -6
- package/src/sqlite-wasm-adapter.ts +0 -744
- package/src/types.ts +0 -20
- package/src/worker.ts +0 -124
- package/tsconfig.json +0 -12
package/src/types.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Message types for Worker <-> Main thread communication
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export type WorkerRequest =
|
|
6
|
-
| { type: "init"; dbName: string }
|
|
7
|
-
| { type: "exec"; id: number; sql: string; params?: any[] }
|
|
8
|
-
| {
|
|
9
|
-
type: "execBatch";
|
|
10
|
-
id: number;
|
|
11
|
-
queries: Array<{ sql: string; params?: any[] }>;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type WorkerResponse =
|
|
15
|
-
| { type: "init-success" }
|
|
16
|
-
| { type: "init-error"; error: string }
|
|
17
|
-
| { type: "exec-success"; id: number; result: any[] }
|
|
18
|
-
| { type: "exec-error"; id: number; error: string }
|
|
19
|
-
| { type: "execBatch-success"; id: number }
|
|
20
|
-
| { type: "execBatch-error"; id: number; error: string };
|
package/src/worker.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SQLite WASM Worker
|
|
3
|
-
*
|
|
4
|
-
* This worker runs SQLite in a Web Worker using OPFS for persistence.
|
|
5
|
-
* Communication happens via postMessage.
|
|
6
|
-
*
|
|
7
|
-
* Usage in app:
|
|
8
|
-
* ```typescript
|
|
9
|
-
* import SQLiteWorker from '@arcote.tech/arc-adapter-db-sqlite-wasm/worker?worker'
|
|
10
|
-
* const worker = new SQLiteWorker()
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
|
|
15
|
-
import type { WorkerRequest, WorkerResponse } from "./types";
|
|
16
|
-
|
|
17
|
-
let db: any = null;
|
|
18
|
-
|
|
19
|
-
const sendResponse = (response: WorkerResponse) => {
|
|
20
|
-
self.postMessage(response);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const handleInit = async (dbName: string) => {
|
|
24
|
-
try {
|
|
25
|
-
const sqlite3 = await sqlite3InitModule({
|
|
26
|
-
print: console.log,
|
|
27
|
-
printErr: console.error,
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
console.log(
|
|
31
|
-
`[SQLite Worker] Running SQLite3 version ${sqlite3.version.libVersion}`,
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
// Use OPFS for persistent storage
|
|
35
|
-
db = new sqlite3.oo1.DB(`file:${dbName}?vfs=opfs`, "ct");
|
|
36
|
-
|
|
37
|
-
sendResponse({ type: "init-success" });
|
|
38
|
-
} catch (error) {
|
|
39
|
-
sendResponse({ type: "init-error", error: (error as Error).message });
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const handleExec = (id: number, sql: string, params?: any[]) => {
|
|
44
|
-
try {
|
|
45
|
-
if (!db) {
|
|
46
|
-
throw new Error("Database not initialized");
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
let result: any[];
|
|
50
|
-
|
|
51
|
-
if (params && params.length > 0) {
|
|
52
|
-
// Pass params as array directly - SQLite WASM handles ? placeholders with arrays
|
|
53
|
-
result = db.exec({
|
|
54
|
-
sql,
|
|
55
|
-
bind: params,
|
|
56
|
-
rowMode: "object",
|
|
57
|
-
returnValue: "resultRows",
|
|
58
|
-
});
|
|
59
|
-
} else {
|
|
60
|
-
result = db.exec({
|
|
61
|
-
sql,
|
|
62
|
-
rowMode: "object",
|
|
63
|
-
returnValue: "resultRows",
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
sendResponse({ type: "exec-success", id, result: result || [] });
|
|
68
|
-
} catch (error) {
|
|
69
|
-
sendResponse({ type: "exec-error", id, error: (error as Error).message });
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const handleExecBatch = (
|
|
74
|
-
id: number,
|
|
75
|
-
queries: Array<{ sql: string; params?: any[] }>,
|
|
76
|
-
) => {
|
|
77
|
-
try {
|
|
78
|
-
if (!db) {
|
|
79
|
-
throw new Error("Database not initialized");
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Execute all queries in a transaction
|
|
83
|
-
db.exec("BEGIN TRANSACTION");
|
|
84
|
-
|
|
85
|
-
try {
|
|
86
|
-
for (const query of queries) {
|
|
87
|
-
if (query.params && query.params.length > 0) {
|
|
88
|
-
// Pass params as array directly
|
|
89
|
-
db.exec({ sql: query.sql, bind: query.params });
|
|
90
|
-
} else {
|
|
91
|
-
db.exec(query.sql);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
db.exec("COMMIT");
|
|
96
|
-
sendResponse({ type: "execBatch-success", id });
|
|
97
|
-
} catch (error) {
|
|
98
|
-
db.exec("ROLLBACK");
|
|
99
|
-
throw error;
|
|
100
|
-
}
|
|
101
|
-
} catch (error) {
|
|
102
|
-
sendResponse({
|
|
103
|
-
type: "execBatch-error",
|
|
104
|
-
id,
|
|
105
|
-
error: (error as Error).message,
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
self.onmessage = (event: MessageEvent<WorkerRequest>) => {
|
|
111
|
-
const request = event.data;
|
|
112
|
-
|
|
113
|
-
switch (request.type) {
|
|
114
|
-
case "init":
|
|
115
|
-
handleInit(request.dbName);
|
|
116
|
-
break;
|
|
117
|
-
case "exec":
|
|
118
|
-
handleExec(request.id, request.sql, request.params);
|
|
119
|
-
break;
|
|
120
|
-
case "execBatch":
|
|
121
|
-
handleExecBatch(request.id, request.queries);
|
|
122
|
-
break;
|
|
123
|
-
}
|
|
124
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "./dist",
|
|
5
|
-
"rootDir": "./src",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"declarationMap": true,
|
|
8
|
-
"lib": ["ES2022", "DOM", "DOM.Iterable", "WebWorker"]
|
|
9
|
-
},
|
|
10
|
-
"include": ["src/**/*.ts"],
|
|
11
|
-
"exclude": ["node_modules", "dist"]
|
|
12
|
-
}
|