22
33SQLite Wasm conveniently wrapped as an ES Module.
44
5- > ** Warning**
5+ > [ ! Warning]
66>
77> This project wraps the code of
88> [ SQLite Wasm] ( https://sqlite.org/wasm/doc/trunk/index.md ) with _ no_ changes,
@@ -32,7 +32,7 @@ storage back-end.
3232
3333### In a wrapped worker (with OPFS if available):
3434
35- > ** Warning**
35+ > [ ! Warning]
3636>
3737> For this to work, you need to set the following headers on your server:
3838>
@@ -43,35 +43,29 @@ storage back-end.
4343``` js
4444import { sqlite3Worker1Promiser } from ' @sqlite.org/sqlite-wasm' ;
4545
46- const log = ( ... args ) => console .log ( ... args) ;
47- const error = ( ... args ) => console .error ( ... args) ;
46+ const log = console .log ;
47+ const error = console .error ;
4848
49- ( async () => {
49+ const initializeSQLite = async () => {
5050 try {
5151 log (' Loading and initializing SQLite3 module...' );
5252
5353 const promiser = await new Promise ((resolve ) => {
54- const _promiser = sqlite3Worker1Promiser ({
55- onready : () => {
56- resolve (_promiser);
57- },
58- });
54+ const _promiser = sqlite3Worker1Promiser ({ onready : () => resolve (_promiser) });
5955 });
6056
6157 log (' Done initializing. Running demo...' );
6258
63- let response;
59+ const configResponse = await promiser (' config-get' , {});
60+ log (' Running SQLite3 version' , configResponse .result .version .libVersion );
6461
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' , {
6963 filename: ' file:mydb.sqlite3?vfs=opfs' ,
7064 });
71- const { dbId } = response ;
65+ const { dbId } = openResponse ;
7266 log (
7367 ' 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' )
7569 );
7670 // Your SQLite code here.
7771 } catch (err) {
@@ -80,15 +74,17 @@ const error = (...args) => console.error(...args);
8074 }
8175 error (err .name , err .message );
8276 }
83- })();
77+ };
78+
79+ initializeSQLite ();
8480```
8581
8682The ` promiser ` object above implements the
8783[ Worker1 API] ( https://sqlite.org/wasm/doc/trunk/api-worker1.md#worker1-methods ) .
8884
8985### In a worker (with OPFS if available):
9086
91- > ** Warning**
87+ > [ ! Warning]
9288>
9389> For this to work, you need to set the following headers on your server:
9490>
@@ -105,34 +101,34 @@ const worker = new Worker('worker.js', { type: 'module' });
105101// In `worker.js`.
106102import sqlite3InitModule from ' @sqlite.org/sqlite-wasm' ;
107103
108- const log = ( ... args ) => console .log ( ... args) ;
109- const error = ( ... args ) => console .error ( ... args) ;
104+ const log = console .log ;
105+ const error = console .error ;
110106
111- const start = function (sqlite3 ) {
107+ const start = (sqlite3 ) => {
112108 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+ );
121117 // Your SQLite code here.
122118};
123119
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 () => {
130121 try {
122+ log (' Loading and initializing SQLite3 module...' );
123+ const sqlite3 = await sqlite3InitModule ({ print: log, printErr: error });
124+ log (' Done initializing. Running demo...' );
131125 start (sqlite3);
132126 } catch (err) {
133- error (err .name , err .message );
127+ error (' Initialization error: ' , err .name , err .message );
134128 }
135- });
129+ };
130+
131+ initializeSQLite ();
136132```
137133
138134The ` db ` object above implements the
@@ -143,27 +139,30 @@ The `db` object above implements the
143139``` js
144140import sqlite3InitModule from ' @sqlite.org/sqlite-wasm' ;
145141
146- const log = ( ... args ) => console .log ( ... args) ;
147- const error = ( ... args ) => console .error ( ... args) ;
142+ const log = console .log ;
143+ const error = console .error ;
148144
149- const start = function (sqlite3 ) {
145+ const start = (sqlite3 ) => {
150146 log (' Running SQLite3 version' , sqlite3 .version .libVersion );
151147 const db = new sqlite3.oo1.DB (' /mydb.sqlite3' , ' ct' );
152148 // Your SQLite code here.
153149};
154150
155- log (' Loading and initializing SQLite3 module...' );
156- sqlite3InitModule ({
157- print: log,
158- printErr: error,
159- }).then ((sqlite3 ) => {
151+ const initializeSQLite = async () => {
160152 try {
153+ log (' Loading and initializing SQLite3 module...' );
154+ const sqlite3 = await sqlite3InitModule ({
155+ print: log,
156+ printErr: error,
157+ });
161158 log (' Done initializing. Running demo...' );
162159 start (sqlite3);
163160 } catch (err) {
164- error (err .name , err .message );
161+ error (' Initialization error: ' , err .name , err .message );
165162 }
166- });
163+ };
164+
165+ initializeSQLite ();
167166```
168167
169168The ` db ` object above implements the
0 commit comments