Skip to content

v9.7.0

Latest

Choose a tag to compare

@jamesgpearce jamesgpearce released this 03 Sep 04:40

SQLite, via node:sqlite

The new persister-sqlite-node module provides the SqliteNodePersister, which binds to a local SQLite database with the node:sqlite module built into Node.js:

import {DatabaseSync} from 'node:sqlite';
import {createStore} from 'tinybase';
import {createSqliteNodePersister} from 'tinybase/persisters/persister-sqlite-node';

const nodeDb = new DatabaseSync(':memory:');
const nodeStore = createStore().setTables({pets: {fido: {species: 'dog'}}});
const nodePersister = createSqliteNodePersister(
  nodeStore,
  nodeDb,
  'my_tinybase',
);

await nodePersister.save();
console.log(nodeDb.prepare('SELECT * FROM my_tinybase;').all());
// -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]

await nodePersister.destroy();
nodeDb.close();

Alone amongst the SQLite Persisters, this one needs no third-party dependency at all, since the module ships with Node.js itself. Both JSON and tabular modes are supported, as is persisting a MergeableStore in JSON mode.

Since node:sqlite does not signal when the database changes, automatic loading polls, just as it does for the BetterSqlite3Persister. And note that the module is still marked as experimental in Node.js, so its API may change.