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.

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:

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.

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.

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