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