22
33SQLite Wasm conveniently wrapped as an ES Module.
44
5- > ** Warning**
5+ ## Bug reports
6+
7+ > [ !Warning]
68>
79> This project wraps the code of
810> [ SQLite Wasm] ( https://sqlite.org/wasm/doc/trunk/index.md ) with _ no_ changes,
@@ -32,7 +34,7 @@ storage back-end.
3234
3335### In a wrapped worker (with OPFS if available):
3436
35- > ** Warning**
37+ > [ ! Warning]
3638>
3739> For this to work, you need to set the following headers on your server:
3840>
@@ -43,35 +45,31 @@ storage back-end.
4345``` js
4446import { sqlite3Worker1Promiser } from ' @sqlite.org/sqlite-wasm' ;
4547
46- const log = ( ... args ) => console .log ( ... args) ;
47- const error = ( ... args ) => console .error ( ... args) ;
48+ const log = console .log ;
49+ const error = console .error ;
4850
49- ( async () => {
51+ const initializeSQLite = async () => {
5052 try {
5153 log (' Loading and initializing SQLite3 module...' );
5254
5355 const promiser = await new Promise ((resolve ) => {
5456 const _promiser = sqlite3Worker1Promiser ({
55- onready : () => {
56- resolve (_promiser);
57- },
57+ onready : () => resolve (_promiser),
5858 });
5959 });
6060
6161 log (' Done initializing. Running demo...' );
6262
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 );
6765
68- response = await promiser (' open' , {
66+ const openResponse = await promiser (' open' , {
6967 filename: ' file:mydb.sqlite3?vfs=opfs' ,
7068 });
71- const { dbId } = response ;
69+ const { dbId } = openResponse ;
7270 log (
7371 ' 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' ),
7573 );
7674 // Your SQLite code here.
7775 } catch (err) {
@@ -80,15 +78,17 @@ const error = (...args) => console.error(...args);
8078 }
8179 error (err .name , err .message );
8280 }
83- })();
81+ };
82+
83+ initializeSQLite ();
8484```
8585
8686The ` promiser ` object above implements the
8787[ Worker1 API] ( https://sqlite.org/wasm/doc/trunk/api-worker1.md#worker1-methods ) .
8888
8989### In a worker (with OPFS if available):
9090
91- > ** Warning**
91+ > [ ! Warning]
9292>
9393> For this to work, you need to set the following headers on your server:
9494>
@@ -105,34 +105,35 @@ const worker = new Worker('worker.js', { type: 'module' });
105105// In `worker.js`.
106106import sqlite3InitModule from ' @sqlite.org/sqlite-wasm' ;
107107
108- const log = ( ... args ) => console .log ( ... args) ;
109- const error = ( ... args ) => console .error ( ... args) ;
108+ const log = console .log ;
109+ const error = console .error ;
110110
111- const start = function (sqlite3 ) {
111+ const start = (sqlite3 ) => {
112112 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+ );
121122 // Your SQLite code here.
122123};
123124
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 () => {
130126 try {
127+ log (' Loading and initializing SQLite3 module...' );
128+ const sqlite3 = await sqlite3InitModule ({ print: log, printErr: error });
129+ log (' Done initializing. Running demo...' );
131130 start (sqlite3);
132131 } catch (err) {
133- error (err .name , err .message );
132+ error (' Initialization error: ' , err .name , err .message );
134133 }
135- });
134+ };
135+
136+ initializeSQLite ();
136137```
137138
138139The ` db ` object above implements the
@@ -143,27 +144,30 @@ The `db` object above implements the
143144``` js
144145import sqlite3InitModule from ' @sqlite.org/sqlite-wasm' ;
145146
146- const log = ( ... args ) => console .log ( ... args) ;
147- const error = ( ... args ) => console .error ( ... args) ;
147+ const log = console .log ;
148+ const error = console .error ;
148149
149- const start = function (sqlite3 ) {
150+ const start = (sqlite3 ) => {
150151 log (' Running SQLite3 version' , sqlite3 .version .libVersion );
151152 const db = new sqlite3.oo1.DB (' /mydb.sqlite3' , ' ct' );
152153 // Your SQLite code here.
153154};
154155
155- log (' Loading and initializing SQLite3 module...' );
156- sqlite3InitModule ({
157- print: log,
158- printErr: error,
159- }).then ((sqlite3 ) => {
156+ const initializeSQLite = async () => {
160157 try {
158+ log (' Loading and initializing SQLite3 module...' );
159+ const sqlite3 = await sqlite3InitModule ({
160+ print: log,
161+ printErr: error,
162+ });
161163 log (' Done initializing. Running demo...' );
162164 start (sqlite3);
163165 } catch (err) {
164- error (err .name , err .message );
166+ error (' Initialization error: ' , err .name , err .message );
165167 }
166- });
168+ };
169+
170+ initializeSQLite ();
167171```
168172
169173The ` db ` object above implements the
0 commit comments