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