Registry / database / sqlite-worker

sqlite-worker

JSON →
library0.9.0jsnpmunverified

sqlite-worker is a persistent SQLite database library for Web pages, Web Workers, and Service Workers (v0.9.0). It wraps sql.js and sqlite-tag to provide a tagged template literal API for queries, with automatic persistence via IndexedDB. Unlike sql.js alone, it manages the database file lifecycle and offers both main-thread (init) and worker-based (SQLiteWorker) execution. The library requires manual dist folder path configuration in Worker contexts due to a browser bug. Release cadence is irregular; latest version is 0.9.0.

npm install sqlite-worker
INSTALL
IMPORT
SIG · SQLITE-WORKER
S
sqlite-worker
databasejavascriptv0.9.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SQLiteWorker
import { SQLiteWorker } from 'sqlite-worker'
import SQLiteWorker from 'sqlite-worker'
Named export, not default. Only works in main thread with ESM; in Workers use importScripts.
init
import { init } from 'sqlite-worker'
const { init } = require('sqlite-worker')
init runs SQLite in main thread. Named export, ESM only.
sqliteWorker (global)
importScripts('./dist/sw.js'); const { sqliteWorker } = self;
import { sqliteWorker } from 'sqlite-worker'
In Service Workers, use importScripts on dist/sw.js; symbol attached to self (globalThis). Not a module export.

Shows two initialization methods (worker vs main thread) and basic CRUD with tagged template literals.

import { SQLiteWorker } from 'sqlite-worker'; import { init } from 'sqlite-worker'; // Option 1: Use a Web Worker (recommended) const db = await SQLiteWorker({ name: 'my-app-db' }); await db.query`CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value TEXT)`; await db.query`INSERT INTO test (value) VALUES (${'hello'})`; const rows = await db.all`SELECT * FROM test`; console.log(rows); // Option 2: Run on main thread (slightly faster) const db2 = await init({ name: 'my-app-db' }); await db2.query`CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value TEXT)`; console.log(await db2.all`SELECT * FROM test`); // Or use tagged template for safe table/column names const { raw } = db; const table = raw`my_table`; await db.query`SELECT * FROM ${table}`;
Debug
Known issues
gotchaIn Web Workers and Service Workers, the 'dist' folder path must be specified manually to resolve sql.js WASM files. Fails silently if omitted.
fix
Provide dist option: SQLiteWorker({ dist: './dist/', name: 'my-db' })
affects: >=0.0.0
gotchaimportScripts('./dist/sw.js') must be used in Service Workers instead of ES import. The module attaches a global 'sqliteWorker' function.
fix
Use importScripts and call the global sqliteWorker function, not the ES import.
affects: >=0.0.0
deprecatedThe 'transaction' export might change in future versions; raw transactions are available via query.
fix
Use query for standalone queries; for transactions, use the transaction function returned by init/SQLiteWorker.
affects: >=0.9.0
Errors
Common errors & fixes
TypeError: Failed to resolve module specifier 'sqlite-worker'
Missing dist folder / wrong import method in Worker context
fix
In Workers, use importScripts('./dist/sw.js') or provide dist option to SQLiteWorker.
Error: Cannot find module 'sql.js'
sql.js not installed as a dependency; sqlite-worker requires it as a peer dependency
fix
Run: npm install sql.js sqlite-worker
Upgrade
Version history
0.9.0latest on npm
Audit
Dependencies
sql.jsrequiredSQLite compiled to WebAssembly for SQL execution
sqlite-tagrequiredTagged template helper for SQL query construction
Agent activity
14 hits · last 30 days
node
10
Meta
2
Amazon
1
OpenAI (training)
1
Resources
sqlite-worker — npm install sqlite-worker · libregistry