SQLite client for JavaScript.
See sqlitempi.com for a general overview.
yarn add sqlite-mpi-client-js
- Or:
npm install --save sqlite-mpi-client-js
- Or:
You also need to install an IO provider.
See ./test/all
for detailed usage.
// Note: Example is for Node.js.
const {smpi} = require("sqlite-mpi-client-js");
const iop = require("smpi-iop-node-ffi");
const c = smpi.newClient(iop);
const f = c.newFileRef("/tmp/db-file.sqlite");
-
There is only one active write transaction at a time per SQLite file.
- This applies at an OS level, for all processes.
-
Write tx requests will queue when there is an active outstanding write tx.
- Queued write tx requests will resolve in First In First Out (FIFO) manner.
- When the promise returned from
f.getWriteTx()
resolves, thewtx
is active.
const wtx = await f.getWriteTx();
// `q` can be a read or write with write txs.
await wtx.q("SELECT 1+1");
await wtx.read("SELECT 1+1");
await wtx.write("INSERT ...");
await wtx.commit();
// await wtx.rollback();
- Read transactions are concurrent; you can have many read transactions active and reading from the same database file.
const rtx = await f.getReadTx();
// `q` must be a read.
await rtx.q("SELECT 1+1");
await rtx.read("SELECT 1+1");
// No writes.
// await tx.write("INSERT ...");
await rtx.commit();
// await rtx.rollback();
const wtx = await f.getWriteTx();
// Index based.
await wtx.write("INSERT INTO t1 (b) VALUES (?), (?)", [1, 2]);
await wtx.write("INSERT INTO t1 (b) VALUES (?1), (?2)", [3, 4]);
// Key based.
await wtx.write("INSERT INTO t1 (b) VALUES (:x), (:y)", {x: 1, y: 2});
await wtx.write("INSERT INTO t1 (b) VALUES (@x), (@y)", {x: 1, y: 2});
await wtx.commit();
- SQLite
WAL
mode is always on; you can not turn it off. - Reads are concurrent, writes queue.