TinyBase logoTinyBase β

DpcTabular

The DpcTabular type describes the configuration of a database-oriented Persister that is operating in tabular mapping mode.

{
  mode: "tabular";
  tables?: {
    load?: DpcTabularLoad;
    save?: DpcTabularSave;
  };
  values?: DpcTabularValues;
  autoLoadIntervalSeconds?: number;
}
TypeDescription
mode"tabular"

The mode to be used for persisting the Store to the database, in this case tabular mapping. See the DpcJson type for the alternative JSON serialization mode.

tables?{ load?: DpcTabularLoad; save?: DpcTabularSave; }

The settings for how the Store Tables are mapped to and from the database.

values?DpcTabularValues

The settings for how the Store Values are mapped to and from the database.

autoLoadIntervalSeconds?number

How often the Persister should poll the database for any changes made to it by other clients, defaulting to 1 second.

This configuration can only be used when the Persister is persisting a regular Store. For those database-oriented Persister types that support MergeableStore data, you will need to use JSON-serialization, es described in the DpcJson section.

It is important to note that both the tabular mapping in ('save') and out ('load') of an underlying database are disabled by default. This is to ensure that if you pass in an existing populated database you don't run the immediate risk of corrupting or losing all your data.

This configuration therefore takes a tables property object (with child load and save property objects) and a values property object. These indicate how you want to load and save Tables and Values respectively. At least one of these two properties are required for the Persister to do anything!

Note that if you are planning to both load from and save to a database, it is important to make sure that the load and save table mappings are symmetrical. For example, consider the following.

import type {DatabasePersisterConfig} from 'tinybase';

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

See the documentation for the DpcTabularLoad, DpcTabularSave, and DpcTabularValues types for more details on how to configure the tabular mapping mode.

Columns in SQLite database have no type, and so in this mode, the table can contain strings and numbers for Cells and Values, just as TinyBase does. Booleans, unfortunately, are stored as 0 or 1 in SQLite, and cannot be distinguished from numbers.

In PostgreSQL databases, all Cell and Value columns are expected to be typed as text, and the strings, booleans, and numbers are all JSON-encoded by the Persister.

The 'Dpc' prefix indicates that this type is used within the DatabasePersisterConfig type.

Example

When applied to a database Persister, this DatabasePersisterConfig will load and save Tables data from and to tables specified in the load and save mappings, and Values data from and to a table called my_tinybase_values.

import type {DatabasePersisterConfig} from 'tinybase';

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

Since

v4.0.0