@@ -20,41 +20,71 @@ 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-if-available )
26+ (🏆 preferred option)
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+ log (
73+ ' OPFS is available, created persisted database at' ,
74+ response .result .filename .replace (/ ^ file:(. *? )\? vfs=opfs$ / , ' $1' ),
75+ );
76+ // Your SQLite code here.
5077 } catch (err) {
78+ if (! (err instanceof Error )) {
79+ err = new Error (err .result .message );
80+ }
5181 error (err .name , err .message );
5282 }
53- });
83+ })() ;
5484```
5585
56- The ` db ` object above implements the
57- [ Object Oriented API # 1 ] ( https://sqlite.org/wasm/doc/trunk/api-oo1 .md ) .
86+ The ` promiser ` object above implements the
87+ [ Worker1 API] ( https://sqlite.org/wasm/doc/trunk/api-worker1 .md#worker1-methods ) .
5888
5989### In a worker (with OPFS if available):
6090
@@ -105,6 +135,40 @@ sqlite3InitModule({
105135});
106136```
107137
138+ The ` db ` object above implements the
139+ [ Object Oriented API #1 ] ( https://sqlite.org/wasm/doc/trunk/api-oo1.md ) .
140+
141+ ### In the main thread (without OPFS):
142+
143+ ``` js
144+ import sqlite3InitModule from ' @sqlite.org/sqlite-wasm' ;
145+
146+ const log = (... args ) => console .log (... args);
147+ const error = (... args ) => console .error (... args);
148+
149+ const start = function (sqlite3 ) {
150+ log (' Running SQLite3 version' , sqlite3 .version .libVersion );
151+ const db = new sqlite3.oo1.DB (' /mydb.sqlite3' , ' ct' );
152+ // Your SQLite code here.
153+ };
154+
155+ log (' Loading and initializing SQLite3 module...' );
156+ sqlite3InitModule ({
157+ print: log,
158+ printErr: error,
159+ }).then ((sqlite3 ) => {
160+ try {
161+ log (' Done initializing. Running demo...' );
162+ start (sqlite3);
163+ } catch (err) {
164+ error (err .name , err .message );
165+ }
166+ });
167+ ```
168+
169+ The ` db ` object above implements the
170+ [ Object Oriented API #1 ] ( https://sqlite.org/wasm/doc/trunk/api-oo1.md ) .
171+
108172## Usage with vite
109173
110174If you are using [ vite] ( https://vitejs.dev/ ) , you need to add the following
0 commit comments