Skip to content

v5.2.0

Compare
Choose a tag to compare
@jamesgpearce jamesgpearce released this 03 Sep 15:40
· 92 commits to main since this release

This release introduces new Persisters for... PostgreSQL! TinyBase now has two new Persister modules:

Conceptually, things behave in the same way as they do for the various SQLite persisters. Simply use the createPostgresPersister function (or the similar createPglitePersister function) to persist your TinyBase data:

import postgres from 'postgres';
import {createPostgresPersister} from 'tinybase/persisters/persister-postgres';
import {createStore} from 'tinybase';

// Create a TinyBase Store.
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});

// Create a postgres connection and Persister.
const sql = postgres('postgres://localhost:5432/tinybase');
const pgPersister = await createPostgresPersister(store, sql, 'my_tinybase');

// Save Store to the database.
await pgPersister.save();

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

And, as per usual, you can update the database and have TinyBase automatically reflect those changes:

// If separately the database gets updated...
const json = '[{"pets":{"felix":{"species":"cat"}}},{}]';
await sql`UPDATE my_tinybase SET store = ${json} WHERE _id = '_';`;

// ... then changes are loaded back. Reactive auto-load is also supported!
await pgPersister.load();
console.log(store.getTables());
// -> {pets: {felix: {species: 'cat'}}}

// As always, don't forget to tidy up.
pgPersister.destroy();
await sql.end();

Note that these two Persister objects support both the json and tabular modes for saving TinyBase data into the database. See the DatabasePersisterConfig type for more details. (Note however that, like the SQLite Persisters, only the json mode is supported for MergeableStore instances, due to their additional CRDT metadata.)

Please provide feedback on this new release on GitHub!