TinyBase logoTinyBase

setSchema

The setSchema method lets you specify the TablesSchema and ValuesSchema of the Store.

setSchema(
  tablesSchema: TablesSchema,
  valuesSchema?: ValuesSchema,
): Store
TypeDescription
tablesSchemaTablesSchema

The TablesSchema to be set for the Store.

valuesSchema?ValuesSchema

The ValuesSchema to be set for the Store.

returnsStore

A reference to the Store.

Note that this may result in a change to data in the Store, as defaults are applied or as invalid Table, Row, Cell, or Value objects are removed. These changes will fire any listeners to that data, as expected.

From v3.0 onwards, this method takes two arguments. The first is the TablesSchema object, the second the ValuesSchema. In previous versions (before the existence of the ValuesSchema data structure), only the first was present. For backwards compatibility the new second parameter is optional.

Examples

This example sets the TablesSchema and ValuesSchema of a Store after it has been created.

const store = createStore().setSchema(
  {
    pets: {
      species: {type: 'string'},
      sold: {type: 'boolean', default: false},
    },
  },
  {open: {type: 'boolean', default: false}},
);
store.addRow('pets', {species: 'dog', color: 'brown', sold: 'maybe'});
store.setValue('open', 'maybe');

console.log(store.getTables());
// -> {pets: {0: {species: 'dog', sold: false}}}
console.log(store.getValues());
// -> {open: false}

This example sets just the TablesSchema of a Store after it has been created.

const store = createStore().setSchema({
  pets: {
    species: {type: 'string'},
    sold: {type: 'boolean', default: false},
  },
});
store.addRow('pets', {species: 'dog', color: 'brown', sold: 'maybe'});

console.log(store.getTables());
// -> {pets: {0: {species: 'dog', sold: false}}}