TinyBase logoTinyBase β

DatabasePersisterConfig

The DatabasePersisterConfig type describes the configuration of a database-oriented Persister, such as those for SQLite and PostgreSQL.

DpcJson | DpcTabular

There are two modes for persisting a Store with a database:

  • A JSON serialization of the whole Store, which is stored in a single row of a table (normally called tinybase) within the database. This is configured by providing a DpcJson object.
  • A tabular mapping of Table Ids to database table names (and vice-versa). Values are stored in a separate special table (normally called tinybase_values). This is configured by providing a DpcTabular object.

Please see the DpcJson and DpcTabular type documentation for more detail on each. If not specified otherwise, JSON serialization will be used for persistence.

Changes made to the database (outside of this Persister) are picked up immediately if they are made via the same connection or library that it is using. If the database is being changed by another client, the Persister needs to poll for changes. Hence both configuration types also contain an autoLoadIntervalSeconds property which indicates how often it should do that. This defaults to 1 second.

Note that all the nested types within this type have a 'Dpc' prefix, short for 'DatabasePersisterConfig'.

Examples

When applied to a database Persister, this DatabasePersisterConfig will load and save a JSON serialization from and to a table called my_tinybase, polling the database every 2 seconds. See DpcJson for more details on these settings.

import type {DatabasePersisterConfig} from 'tinybase';

export const databasePersisterConfig: DatabasePersisterConfig = {
  mode: 'json',
  storeTableName: 'my_tinybase',
  autoLoadIntervalSeconds: 2,
};

When applied to a database Persister, this DatabasePersisterConfig will load and save tabular data from and to tables specified in the load and save mappings. See DpcTabular for more details on these settings.

import type {DatabasePersisterConfig} from 'tinybase';

export const databasePersisterConfig: DatabasePersisterConfig = {
  mode: 'tabular',
  tables: {
    load: {petsInDb: 'pets', speciesInDb: 'species'},
    save: {pets: 'petsInDb', species: 'speciesInDb'},
  },
};

Since

v4.0.0