@@ -20,42 +20,70 @@ npm install @sqlite.org/sqlite-wasm
2020
2121## Usage
2222
23- There are two ways to use SQLite Wasm:
24- [ in the main thread] ( #in-the-main-thread-without-opfs ) and
25- [ in a worker] ( #in-a-worker-with-opfs-if-available ) . Only the worker version
26- allows you to use the origin private file system (OPFS) storage back-end.
23+ There are three ways to use SQLite Wasm:
2724
28- ### In the main thread (without OPFS):
25+ - [ in the main thread with a wrapped worker] ( #in-a-wrapped-worker-with-opfs )
26+ (preferred option 💡) and
27+ - [ in a worker] ( #in-a-worker-with-opfs-if-available )
28+ - [ in the main thread] ( #in-the-main-thread-without-opfs )
29+
30+ Only the worker versions allow you to use the origin private file system (OPFS)
31+ storage back-end.
32+
33+ ### In a wrapped worker (with OPFS if available):
34+
35+ > ** Warning**
36+ >
37+ > For this to work, you need to set the following headers on your server:
38+ >
39+ > ` Cross-Origin-Opener-Policy: same-origin `
40+ >
41+ > ` Cross-Origin-Embedder-Policy: require-corp `
2942
3043``` js
31- import sqlite3InitModule from ' @sqlite.org/sqlite-wasm' ;
44+ import { sqlite3Worker1Promiser } from ' @sqlite.org/sqlite-wasm' ;
3245
3346const log = (... args ) => console .log (... args);
3447const error = (... args ) => console .error (... args);
3548
36- const start = function (sqlite3 ) {
37- log (' Running SQLite3 version' , sqlite3 .version .libVersion );
38- const db = new sqlite3.oo1.DB (' /mydb.sqlite3' , ' ct' );
39- // Your SQLite code here.
40- };
41-
42- log (' Loading and initializing SQLite3 module...' );
43- sqlite3InitModule ({
44- print: log,
45- printErr: error,
46- }).then ((sqlite3 ) => {
49+ (async () => {
4750 try {
51+ log (' Loading and initializing SQLite3 module...' );
52+
53+ const promiser = await new Promise ((resolve ) => {
54+ const _promiser = sqlite3Worker1Promiser ({
55+ onready : () => {
56+ resolve (_promiser);
57+ },
58+ });
59+ });
60+
4861 log (' Done initializing. Running demo...' );
49- start (sqlite3);
62+
63+ let response;
64+
65+ response = await promiser (' config-get' , {});
66+ log (' Running SQLite3 version' , response .result .version .libVersion );
67+
68+ response = await promiser (' open' , {
69+ filename: ' file:mydb.sqlite3?vfs=opfs' ,
70+ });
71+ const { dbId } = response;
72+ logHtml (
73+ ' ' ,
74+ ' OPFS is available, created persisted database at' ,
75+ response .result .filename .replace (/ ^ file:(. *? )\? vfs=opfs/ , ' $1' ),
76+ );
77+ // Your SQLite code here.
5078 } catch (err) {
79+ if (! (err instanceof Error )) {
80+ err = new Error (err .result .message );
81+ }
5182 error (err .name , err .message );
5283 }
53- });
84+ })() ;
5485```
5586
56- The ` db ` object above implements the
57- [ Object Oriented API #1 ] ( https://sqlite.org/wasm/doc/trunk/api-oo1.md ) .
58-
5987### In a worker (with OPFS if available):
6088
6189> ** Warning**
@@ -105,6 +133,37 @@ sqlite3InitModule({
105133});
106134```
107135
136+ ### In the main thread (without OPFS):
137+
138+ ``` js
139+ import sqlite3InitModule from ' @sqlite.org/sqlite-wasm' ;
140+
141+ const log = (... args ) => console .log (... args);
142+ const error = (... args ) => console .error (... args);
143+
144+ const start = function (sqlite3 ) {
145+ log (' Running SQLite3 version' , sqlite3 .version .libVersion );
146+ const db = new sqlite3.oo1.DB (' /mydb.sqlite3' , ' ct' );
147+ // Your SQLite code here.
148+ };
149+
150+ log (' Loading and initializing SQLite3 module...' );
151+ sqlite3InitModule ({
152+ print: log,
153+ printErr: error,
154+ }).then ((sqlite3 ) => {
155+ try {
156+ log (' Done initializing. Running demo...' );
157+ start (sqlite3);
158+ } catch (err) {
159+ error (err .name , err .message );
160+ }
161+ });
162+ ```
163+
164+ The ` db ` object above implements the
165+ [ Object Oriented API #1 ] ( https://sqlite.org/wasm/doc/trunk/api-oo1.md ) .
166+
108167## Usage with vite
109168
110169If you are using [ vite] ( https://vitejs.dev/ ) , you need to add the following
0 commit comments