TinyBase logoTinyBase

createElectricSqlPersister

The createElectricSqlPersister function creates a Persister object that can persist the Store to a local ElectricSQL database (in an appropriate environment).

createElectricSqlPersister(
  store: Store,
  electricClient: ElectricClient<any>,
  configOrStoreTableName?: string | DatabasePersisterConfig,
  onSqlCommand?: (sql: string, args?: any[]) => void,
  onIgnoredError?: (error: any) => void,
): ElectricSqlPersister
TypeDescription
storeStore

The Store to persist.

electricClientElectricClient<any>

The Electric client that was returned from await electrify(...).

configOrStoreTableName?string | DatabasePersisterConfig

A DatabasePersisterConfig to configure the persistence mode (or a string to set the storeTableName property of the JSON serialization).

onSqlCommand?(sql: string, args?: any[]) => void

An optional handler called every time the Persister executes a SQL command or query. This is suitable for logging persistence behavior in a development environment.

onIgnoredError?(error: any) => void

An optional handler for the errors that the Persister would otherwise ignore when trying to save or load data. This is suitable for debugging persistence issues in a development environment.

returnsElectricSqlPersister

A reference to the new ElectricSqlPersister object.

As well as providing a reference to the Store to persist, you must provide a electricClient parameter which identifies the Electric client.

A database Persister uses one of two modes: either a JSON serialization of the whole Store stored in a single row of a table (the default), or a tabular mapping of Table Ids to database table names and vice-versa).

The third argument is a DatabasePersisterConfig object that configures which of those modes to use, and settings for each. If the third argument is simply a string, it is used as the storeTableName property of the JSON serialization.

See the documentation for the DpcJson and DpcTabular types for more information on how both of those modes can be configured.

Examples

This example creates a ElectricSqlPersister object and persists the Store to a local ElectricSql database as a JSON serialization into the my_tinybase table. It makes a change to the database directly and then reloads it back into the Store.

const electricClient = await electrify(
  await ElectricDatabase.init('electric.db', ''),
  schema,
  {url: ELECTRIC_URL},
);
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const persister = createElectricSqlPersister(
  store,
  electricClient,
  'my_tinybase',
);

await persister.save();
// Store will be saved to the database.

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

await electricClient.db.raw({
  sql:
    'UPDATE my_tinybase SET store = ' +
    `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
});
await persister.load();
console.log(store.getTables());
// -> {pets: {felix: {species: 'cat'}}}

persister.destroy();

This example creates a ElectricSqlPersister object and persists the Store to a local ElectricSql database with tabular mapping.

const electricClient = await electrify(
  await ElectricDatabase.init('electric.db', ''),
  schema,
  {url: ELECTRIC_URL},
);
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const persister = createElectricSqlPersister(store, electricClient, {
  mode: 'tabular',
  tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
});

await persister.save();
console.log(await electricClient.db.raw({sql: 'SELECT * FROM pets;'}));
// -> [{_id: 'fido', species: 'dog'}]

await electricClient.db.raw({
  sql: `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
});
await persister.load();
console.log(store.getTables());
// -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}

persister.destroy();

Since

v4.6.0