- Notifications
You must be signed in to change notification settings - Fork68
sqlite/sqlite-wasm
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
SQLite Wasm conveniently wrapped as an ES Module.
Warning
This project wraps the code ofSQLite Wasm withno changes,apart from added TypeScript types. Please donot file issues or featurerequests regarding the underlying SQLite Wasm code here. Instead, pleasefollow theSQLite bug filing instructions.Filing TypeScript type related issues and feature requests is fine.
Warning
Node.js is currently only supported for in-memory databases withoutpersistence.
npm install @sqlite.org/sqlite-wasm
There are three ways to use SQLite Wasm:
- in the main thread with a wrapped worker(🏆 preferred option)
- in a worker
- in the main thread
Only the worker versions allow you to use the origin private file system (OPFS)storage back-end.
Warning
For this to work, you need to set the following headers on your server:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
import{sqlite3Worker1Promiser}from'@sqlite.org/sqlite-wasm';constlog=console.log;consterror=console.error;constinitializeSQLite=async()=>{try{log('Loading and initializing SQLite3 module...');constpromiser=awaitnewPromise((resolve)=>{const_promiser=sqlite3Worker1Promiser({onready:()=>resolve(_promiser),});});log('Done initializing. Running demo...');constconfigResponse=awaitpromiser('config-get',{});log('Running SQLite3 version',configResponse.result.version.libVersion);constopenResponse=awaitpromiser('open',{filename:'file:mydb.sqlite3?vfs=opfs',});const{ dbId}=openResponse;log('OPFS is available, created persisted database at',openResponse.result.filename.replace(/^file:(.*?)\?vfs=opfs$/,'$1'),);// Your SQLite code here.}catch(err){if(!(errinstanceofError)){err=newError(err.result.message);}error(err.name,err.message);}};initializeSQLite();
Thepromiser object above implements theWorker1 API.
Warning
For this to work, you need to set the following headers on your server:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
// In `main.js`.constworker=newWorker('worker.js',{type:'module'});
// In `worker.js`.importsqlite3InitModulefrom'@sqlite.org/sqlite-wasm';constlog=console.log;consterror=console.error;conststart=(sqlite3)=>{log('Running SQLite3 version',sqlite3.version.libVersion);constdb='opfs'insqlite3 ?newsqlite3.oo1.OpfsDb('/mydb.sqlite3') :newsqlite3.oo1.DB('/mydb.sqlite3','ct');log('opfs'insqlite3 ?`OPFS is available, created persisted database at${db.filename}` :`OPFS is not available, created transient database${db.filename}`,);// Your SQLite code here.};constinitializeSQLite=async()=>{try{log('Loading and initializing SQLite3 module...');constsqlite3=awaitsqlite3InitModule({print:log,printErr:error});log('Done initializing. Running demo...');start(sqlite3);}catch(err){error('Initialization error:',err.name,err.message);}};initializeSQLite();
Thedb object above implements theObject Oriented API #1.
importsqlite3InitModulefrom'@sqlite.org/sqlite-wasm';constlog=console.log;consterror=console.error;conststart=(sqlite3)=>{log('Running SQLite3 version',sqlite3.version.libVersion);constdb=newsqlite3.oo1.DB('/mydb.sqlite3','ct');// Your SQLite code here.};constinitializeSQLite=async()=>{try{log('Loading and initializing SQLite3 module...');constsqlite3=awaitsqlite3InitModule({print:log,printErr:error,});log('Done initializing. Running demo...');start(sqlite3);}catch(err){error('Initialization error:',err.name,err.message);}};initializeSQLite();
Thedb object above implements theObject Oriented API #1.
If you are usingvite, you need to add the followingconfig option invite.config.js:
import{defineConfig}from'vite';exportdefaultdefineConfig({server:{headers:{'Cross-Origin-Opener-Policy':'same-origin','Cross-Origin-Embedder-Policy':'require-corp',},},optimizeDeps:{exclude:['@sqlite.org/sqlite-wasm'],},});
Check out asample projectthat shows this in action.
See thedemo folder forexamples of how to use this in the main thread and in a worker. (Note that theworker variant requires special HTTP headers, so it can't be hosted on GitHubPages.) An example that shows how to use this with vite is available onStackBlitz.
See the list ofnpm dependentsfor this package.
(These steps can only be executed by maintainers.)
- Update the version number in
package.jsonreflecting the currentSQLite version number and add a buildidentifier suffix like-build1. The complete version number should readsomething like3.41.2-build1. - Run
npm run buildto build the ES Module. This downloads the latest SQLiteWasm binary and builds the ES Module. - Run
npm run deployto commit the changes, push to GitHub, and publish thenew version to npm.
Apache 2.0.
This project is based onSQLite Wasm, which itconveniently wraps as an ES Module and publishes to npm as@sqlite.org/sqlite-wasm.
About
SQLite Wasm conveniently wrapped as an ES Module.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.