Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

SQLite Wasm conveniently wrapped as an ES Module.

NotificationsYou must be signed in to change notification settings

sqlite/sqlite-wasm

Repository files navigation

SQLite Wasm conveniently wrapped as an ES Module.

Bug reports

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.

Node.js support

Warning

Node.js is currently only supported for in-memory databases withoutpersistence.

Installation

npm install @sqlite.org/sqlite-wasm

Usage

There are three ways to use SQLite Wasm:

Only the worker versions allow you to use the origin private file system (OPFS)storage back-end.

In a wrapped worker (with OPFS if available):

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.

In a worker (with OPFS if available):

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.

In the main thread (without OPFS):

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.

Usage with vite

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.

Demo

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.

Projects using this package

See the list ofnpm dependentsfor this package.

Deploying a new version

(These steps can only be executed by maintainers.)

  1. Update the version number inpackage.json reflecting the currentSQLite version number and add a buildidentifier suffix like-build1. The complete version number should readsomething like3.41.2-build1.
  2. Runnpm run build to build the ES Module. This downloads the latest SQLiteWasm binary and builds the ES Module.
  3. Runnpm run deploy to commit the changes, push to GitHub, and publish thenew version to npm.

License

Apache 2.0.

Acknowledgements

This project is based onSQLite Wasm, which itconveniently wraps as an ES Module and publishes to npm as@sqlite.org/sqlite-wasm.


[8]ページ先頭

©2009-2025 Movatter.jp