2
2
3
3
SQLite Wasm conveniently wrapped as an ES Module.
4
4
5
- > ** Warning**
5
+ ## Bug reports
6
+
7
+ > [ !Warning]
6
8
>
7
9
> This project wraps the code of
8
10
> [ SQLite Wasm] ( https://sqlite.org/wasm/doc/trunk/index.md ) with _ no_ changes,
@@ -32,7 +34,7 @@ storage back-end.
32
34
33
35
### In a wrapped worker (with OPFS if available):
34
36
35
- > ** Warning**
37
+ > [ ! Warning]
36
38
>
37
39
> For this to work, you need to set the following headers on your server:
38
40
>
@@ -43,35 +45,31 @@ storage back-end.
43
45
``` js
44
46
import { sqlite3Worker1Promiser } from ' @sqlite.org/sqlite-wasm' ;
45
47
46
- const log = ( ... args ) => console .log ( ... args) ;
47
- const error = ( ... args ) => console .error ( ... args) ;
48
+ const log = console .log ;
49
+ const error = console .error ;
48
50
49
- ( async () => {
51
+ const initializeSQLite = async () => {
50
52
try {
51
53
log (' Loading and initializing SQLite3 module...' );
52
54
53
55
const promiser = await new Promise ((resolve ) => {
54
56
const _promiser = sqlite3Worker1Promiser ({
55
- onready : () => {
56
- resolve (_promiser);
57
- },
57
+ onready : () => resolve (_promiser),
58
58
});
59
59
});
60
60
61
61
log (' Done initializing. Running demo...' );
62
62
63
- let response;
64
-
65
- response = await promiser (' config-get' , {});
66
- log (' Running SQLite3 version' , response .result .version .libVersion );
63
+ const configResponse = await promiser (' config-get' , {});
64
+ log (' Running SQLite3 version' , configResponse .result .version .libVersion );
67
65
68
- response = await promiser (' open' , {
66
+ const openResponse = await promiser (' open' , {
69
67
filename: ' file:mydb.sqlite3?vfs=opfs' ,
70
68
});
71
- const { dbId } = response ;
69
+ const { dbId } = openResponse ;
72
70
log (
73
71
' OPFS is available, created persisted database at' ,
74
- response .result .filename .replace (/ ^ file:(. *? )\? vfs=opfs$ / , ' $1' ),
72
+ openResponse .result .filename .replace (/ ^ file:(. *? )\? vfs=opfs$ / , ' $1' ),
75
73
);
76
74
// Your SQLite code here.
77
75
} catch (err) {
@@ -80,15 +78,17 @@ const error = (...args) => console.error(...args);
80
78
}
81
79
error (err .name , err .message );
82
80
}
83
- })();
81
+ };
82
+
83
+ initializeSQLite ();
84
84
```
85
85
86
86
The ` promiser ` object above implements the
87
87
[ Worker1 API] ( https://sqlite.org/wasm/doc/trunk/api-worker1.md#worker1-methods ) .
88
88
89
89
### In a worker (with OPFS if available):
90
90
91
- > ** Warning**
91
+ > [ ! Warning]
92
92
>
93
93
> For this to work, you need to set the following headers on your server:
94
94
>
@@ -105,34 +105,35 @@ const worker = new Worker('worker.js', { type: 'module' });
105
105
// In `worker.js`.
106
106
import sqlite3InitModule from ' @sqlite.org/sqlite-wasm' ;
107
107
108
- const log = ( ... args ) => console .log ( ... args) ;
109
- const error = ( ... args ) => console .error ( ... args) ;
108
+ const log = console .log ;
109
+ const error = console .error ;
110
110
111
- const start = function (sqlite3 ) {
111
+ const start = (sqlite3 ) => {
112
112
log (' Running SQLite3 version' , sqlite3 .version .libVersion );
113
- let db;
114
- if (' opfs' in sqlite3) {
115
- db = new sqlite3.oo1.OpfsDb (' /mydb.sqlite3' );
116
- log (' OPFS is available, created persisted database at' , db .filename );
117
- } else {
118
- db = new sqlite3.oo1.DB (' /mydb.sqlite3' , ' ct' );
119
- log (' OPFS is not available, created transient database' , db .filename );
120
- }
113
+ const db =
114
+ ' opfs' in sqlite3
115
+ ? new sqlite3.oo1.OpfsDb (' /mydb.sqlite3' )
116
+ : new sqlite3.oo1.DB (' /mydb.sqlite3' , ' ct' );
117
+ log (
118
+ ' opfs' in sqlite3
119
+ ? ` OPFS is available, created persisted database at ${ db .filename } `
120
+ : ` OPFS is not available, created transient database ${ db .filename } ` ,
121
+ );
121
122
// Your SQLite code here.
122
123
};
123
124
124
- log (' Loading and initializing SQLite3 module...' );
125
- sqlite3InitModule ({
126
- print: log,
127
- printErr: error,
128
- }).then ((sqlite3 ) => {
129
- log (' Done initializing. Running demo...' );
125
+ const initializeSQLite = async () => {
130
126
try {
127
+ log (' Loading and initializing SQLite3 module...' );
128
+ const sqlite3 = await sqlite3InitModule ({ print: log, printErr: error });
129
+ log (' Done initializing. Running demo...' );
131
130
start (sqlite3);
132
131
} catch (err) {
133
- error (err .name , err .message );
132
+ error (' Initialization error: ' , err .name , err .message );
134
133
}
135
- });
134
+ };
135
+
136
+ initializeSQLite ();
136
137
```
137
138
138
139
The ` db ` object above implements the
@@ -143,27 +144,30 @@ The `db` object above implements the
143
144
``` js
144
145
import sqlite3InitModule from ' @sqlite.org/sqlite-wasm' ;
145
146
146
- const log = ( ... args ) => console .log ( ... args) ;
147
- const error = ( ... args ) => console .error ( ... args) ;
147
+ const log = console .log ;
148
+ const error = console .error ;
148
149
149
- const start = function (sqlite3 ) {
150
+ const start = (sqlite3 ) => {
150
151
log (' Running SQLite3 version' , sqlite3 .version .libVersion );
151
152
const db = new sqlite3.oo1.DB (' /mydb.sqlite3' , ' ct' );
152
153
// Your SQLite code here.
153
154
};
154
155
155
- log (' Loading and initializing SQLite3 module...' );
156
- sqlite3InitModule ({
157
- print: log,
158
- printErr: error,
159
- }).then ((sqlite3 ) => {
156
+ const initializeSQLite = async () => {
160
157
try {
158
+ log (' Loading and initializing SQLite3 module...' );
159
+ const sqlite3 = await sqlite3InitModule ({
160
+ print: log,
161
+ printErr: error,
162
+ });
161
163
log (' Done initializing. Running demo...' );
162
164
start (sqlite3);
163
165
} catch (err) {
164
- error (err .name , err .message );
166
+ error (' Initialization error: ' , err .name , err .message );
165
167
}
166
- });
168
+ };
169
+
170
+ initializeSQLite ();
167
171
```
168
172
169
173
The ` db ` object above implements the
0 commit comments