-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqlite.worker.js
59 lines (51 loc) · 1.4 KB
/
sqlite.worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
importScripts("sqlite3.js");
let db = null;
async function createDatabase() {
const sqlite3 = await sqlite3InitModule();
// TODO: Parameterize storage location, and storage type
db = new sqlite3.oo1.DB("file:database.db?vfs=opfs", "c");
}
function handleMessage() {
const data = this.data;
switch (data && data.action) {
case "exec":
if (!data["sql"]) {
throw new Error("exec: Missing query string");
}
return postMessage({
id: data.id,
results: { values: db.exec({ sql: data.sql, bind: data.params, returnValue: "resultRows" }) },
})
case "begin_transaction":
return postMessage({
id: data.id,
results: db.exec("BEGIN TRANSACTION;"),
})
case "end_transaction":
return postMessage({
id: data.id,
results: db.exec("END TRANSACTION;"),
})
case "rollback_transaction":
return postMessage({
id: data.id,
results: db.exec("ROLLBACK TRANSACTION;"),
})
default:
throw new Error(`Unsupported action: ${data && data.action}`);
}
}
function handleError(err) {
return postMessage({
id: this.data.id,
error: err,
});
}
if (typeof importScripts === "function") {
db = null;
const sqlModuleReady = createDatabase();
self.onmessage = (event) => {
return sqlModuleReady.then(handleMessage.bind(event))
.catch(handleError.bind(event));
}
}