@vanillaspa/sqlite-database 1.2.3 → 1.2.4
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/package.json +1 -1
- package/sqliteWorker.js +16 -15
package/package.json
CHANGED
package/sqliteWorker.js
CHANGED
|
@@ -2,6 +2,7 @@ import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
|
2
2
|
|
|
3
3
|
let db = null;
|
|
4
4
|
let sqlite3 = null;
|
|
5
|
+
let port;
|
|
5
6
|
|
|
6
7
|
async function getInstance() {
|
|
7
8
|
if (!sqlite3) {
|
|
@@ -10,7 +11,7 @@ async function getInstance() {
|
|
|
10
11
|
return sqlite3;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
function reply(
|
|
14
|
+
function reply(result) {
|
|
14
15
|
port.postMessage({ type: 'application/json', result });
|
|
15
16
|
port.close();
|
|
16
17
|
}
|
|
@@ -20,7 +21,7 @@ function replyError(message) {
|
|
|
20
21
|
port.close();
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
function handleSQLiteError(
|
|
24
|
+
function handleSQLiteError(sql, e) {
|
|
24
25
|
if (e.message.includes('SQLITE_CANTOPEN')) {
|
|
25
26
|
console.info("Info: No SQLite database available. Upload a new database or reload the page.");
|
|
26
27
|
} else if (e.message.includes('SQLITE_CONSTRAINT_UNIQUE')) {
|
|
@@ -33,15 +34,15 @@ function handleSQLiteError(port, sql, e) {
|
|
|
33
34
|
|
|
34
35
|
onmessage = async function ({ data, ports }) {
|
|
35
36
|
const { action } = data;
|
|
36
|
-
|
|
37
|
+
port = ports[0] ?? null;
|
|
37
38
|
|
|
38
39
|
switch (action) {
|
|
39
40
|
case 'closeDB': {
|
|
40
41
|
try {
|
|
41
42
|
closeDB();
|
|
42
|
-
reply(
|
|
43
|
+
reply(null);
|
|
43
44
|
} catch (e) {
|
|
44
|
-
replyError(
|
|
45
|
+
replyError(e.message);
|
|
45
46
|
}
|
|
46
47
|
break;
|
|
47
48
|
}
|
|
@@ -50,9 +51,9 @@ onmessage = async function ({ data, ports }) {
|
|
|
50
51
|
try {
|
|
51
52
|
const { newDB, message } = await createDatabase(name)
|
|
52
53
|
db = newDB;
|
|
53
|
-
reply(
|
|
54
|
+
reply(message);
|
|
54
55
|
} catch (e) {
|
|
55
|
-
replyError(
|
|
56
|
+
replyError(e.message)
|
|
56
57
|
}
|
|
57
58
|
break;
|
|
58
59
|
}
|
|
@@ -60,9 +61,9 @@ onmessage = async function ({ data, ports }) {
|
|
|
60
61
|
try {
|
|
61
62
|
const byteArray = sqlite3.capi.sqlite3_js_db_export(db);
|
|
62
63
|
const blob = new Blob([byteArray.buffer], { type: "application/vnd.sqlite3" });
|
|
63
|
-
reply(
|
|
64
|
+
reply(blob);
|
|
64
65
|
} catch (e) {
|
|
65
|
-
replyError(
|
|
66
|
+
replyError(e.message);
|
|
66
67
|
}
|
|
67
68
|
break;
|
|
68
69
|
}
|
|
@@ -70,9 +71,9 @@ onmessage = async function ({ data, ports }) {
|
|
|
70
71
|
const { sql } = data;
|
|
71
72
|
try {
|
|
72
73
|
const result = db.exec({ sql, returnValue: "resultRows" });
|
|
73
|
-
reply(
|
|
74
|
+
reply(result);
|
|
74
75
|
} catch (e) {
|
|
75
|
-
handleSQLiteError(
|
|
76
|
+
handleSQLiteError(sql, e)
|
|
76
77
|
}
|
|
77
78
|
break;
|
|
78
79
|
}
|
|
@@ -88,9 +89,9 @@ onmessage = async function ({ data, ports }) {
|
|
|
88
89
|
const row = stmt.get([]);
|
|
89
90
|
result.push(Object.fromEntries(columns.map((columnName, index) => [columnName, row[index]])));
|
|
90
91
|
}
|
|
91
|
-
reply(
|
|
92
|
+
reply(result);
|
|
92
93
|
} catch (e) {
|
|
93
|
-
handleSQLiteError(
|
|
94
|
+
handleSQLiteError(sql, e);
|
|
94
95
|
} finally {
|
|
95
96
|
stmt?.finalize();
|
|
96
97
|
}
|
|
@@ -100,9 +101,9 @@ onmessage = async function ({ data, ports }) {
|
|
|
100
101
|
const { name, arrayBuffer } = data;
|
|
101
102
|
try {
|
|
102
103
|
const message = await uploadDatabase(name, arrayBuffer)
|
|
103
|
-
reply(
|
|
104
|
+
reply(message);
|
|
104
105
|
} catch (e) {
|
|
105
|
-
replyError(
|
|
106
|
+
replyError(e.message);
|
|
106
107
|
}
|
|
107
108
|
break;
|
|
108
109
|
}
|