Skip to content

Latest commit

 

History

History

README.md

@alt-javascript/jsdbc-sqljs

Language npm version License: MIT CI

JSDBC driver for SQLite via sql.js (WebAssembly). Runs the same SQL in Node.js and the browser — no native dependencies, no build step.

Part of the @alt-javascript/jsdbc monorepo.

Install

npm install @alt-javascript/jsdbc-core @alt-javascript/jsdbc-sqljs

Usage

import { SingleConnectionDataSource } from '@alt-javascript/jsdbc-core';
import '@alt-javascript/jsdbc-sqljs'; // self-registers with DriverManager

const ds = new SingleConnectionDataSource({ url: 'jsdbc:sqljs:memory' });
const conn = await ds.getConnection();

const stmt = await conn.createStatement();
await stmt.executeUpdate('CREATE TABLE notes (id INTEGER PRIMARY KEY, text TEXT)');

const ps = await conn.prepareStatement('INSERT INTO notes (text) VALUES (?)');
ps.setParameter(1, 'Hello from the browser');
await ps.executeUpdate();

const query = await conn.prepareStatement('SELECT * FROM notes');
const rs = await query.executeQuery();
console.log(rs.getRows()); // [{id: 1, text: 'Hello from the browser'}]

rs.close();
// Don't close conn — SingleConnectionDataSource keeps it alive
await ds.destroy(); // when truly done

URL Scheme

jsdbc:sqljs:memory

Currently supports in-memory databases only. Each connection creates a new empty database — use SingleConnectionDataSource to share a single database instance across operations.

Browser Usage

<script type="module">
  import { SingleConnectionDataSource } from '@alt-javascript/jsdbc-core';
  import '@alt-javascript/jsdbc-sqljs';

  const ds = new SingleConnectionDataSource({ url: 'jsdbc:sqljs:memory' });
  const conn = await ds.getConnection();
  // Same API as Node.js
</script>

The sql.js WebAssembly binary (~1MB) is loaded automatically.

When to Use

  • Browser applications needing client-side SQL
  • Isomorphic code that must run identically in Node.js and the browser
  • Testing without native dependencies — CI environments with no C++ compiler
  • Offline-first apps with in-memory data

For Node.js-only applications where performance matters, use @alt-javascript/jsdbc-sqlite (better-sqlite3) instead.

License

MIT