createPowerSyncPersister
The createPowerSyncPersister
function creates a PowerSyncPersister
object that can persist the Store
to a local SQLite database that is automatically synced using the PowerSync service.
createPowerSyncPersister(
store: Store,
powerSync: AbstractPowerSyncDatabase,
configOrStoreTableName?: string | DatabasePersisterConfig,
onSqlCommand?: (sql: string, params?: any[]) => void,
onIgnoredError?: (error: any) => void,
): PowerSyncPersister
Type | Description | |
---|---|---|
store | Store | The |
powerSync | AbstractPowerSyncDatabase | The PowerSync instance. |
configOrStoreTableName? | string | DatabasePersisterConfig | A |
onSqlCommand? | (sql: string, params?: any[]) => void | An optional handler called every time the |
onIgnoredError? | (error: any) => void | An optional handler for the errors that the |
returns | PowerSyncPersister | A reference to the new |
A PowerSyncPersister
only supports regular Store
objects, and cannot be used to persist the metadata of a MergeableStore
.
As well as providing a reference to the Store
to persist, you must provide a powerSync
parameter which identifies the PowerSync instance.
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 PowerSyncPersister
object and persists the Store
to a local PowerSync instance. It makes a change to the database directly and then reloads it back into the Store
.
import {createPowerSyncPersister} from 'tinybase/persisters/persister-powersync';
import {createStore} from 'tinybase';
import {usePowerSync} from '@powersync/react';
const ps = usePowerSync();
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const persister = createPowerSyncPersister(store, ps, 'my_tinybase');
await persister.save();
// Store will be saved to the database.
console.log(
await new Promise((resolve) =>
ps.execute('SELECT * FROM my_tinybase;', (_, rows) => resolve(rows)),
),
);
// -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
await new Promise((resolve) =>
ps.execute(
'UPDATE my_tinybase SET store = ' +
`'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
resolve,
),
);
await persister.load();
console.log(store.getTables());
// -> {pets: {felix: {species: 'cat'}}}
persister.destroy();
This example creates a PowerSyncPersister
object and persists the Store
to a local PowerSync instance with tabular mapping.
import {createPowerSyncPersister} from 'tinybase/persisters/persister-powersync';
import {createStore} from 'tinybase';
import {usePowerSync} from '@powersync/react';
const ps = usePowerSync();
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const persister = createPowerSyncPersister(store, ps, {
mode: 'tabular',
tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
});
await persister.save();
console.log(
await new Promise((resolve) =>
ps.execute('SELECT * FROM pets;', (_, rows) => resolve(rows)),
),
);
// -> [{_id: 'fido', species: 'dog'}]
await new Promise((resolve) =>
ps.execute(
`INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
resolve,
),
);
await persister.load();
console.log(store.getTables());
// -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
persister.destroy();
Since
v4.8.0