TinyBase logoTinyBase β

createMergeableStore

Essential

The createMergeableStore function creates a MergeableStore, and is the main entry point into the mergeable-store module.

createMergeableStore(
  uniqueId?: string,
  getNow?: GetNow,
  maxClockSkewSeconds?: number,
): MergeableStore
TypeDescription
uniqueId?string

An optional unique Id for the MergeableStore.

getNow?GetNow

An optional function that generates millisecond timestamps, since v6.1.0, defaulting to Date.now.

maxClockSkewSeconds?number

An optional number of seconds by which a stamp from elsewhere may be ahead of this MergeableStore's clock, since v10.1.0, defaulting to 300.

returnsMergeableStore

A reference to the new MergeableStore.

There are three optional parameters which are only for testing and advanced usage.

The first is a uniqueId for the MergeableStore, used to distinguish conflicting changes made in the same millisecond by two different MergeableStore objects as its hash is added to the end of the HLC timestamps. Generally this can be omitted unless you have a need for deterministic HLCs, such as in a testing scenario. Otherwise, TinyBase will assign a unique Id to the Store at the time of creation.

Since v6.1.0, the second is a function that can be used to replace the way the timestamp is generated for HLCs (by default JavaScript's Date.now() method).

Since v10.1.0, the third is the number of seconds by which a stamp made elsewhere may be ahead of this MergeableStore's clock, defaulting to 300. Changes stamped further ahead than that are left out when the rest of what they arrive with is applied, so that one device with a fast clock cannot pull every other's clock along with it. Pass Infinity to accept them all. Content that a Persister kept is always loaded, however far ahead it is.

Examples

This example creates a MergeableStore.

import {createMergeableStore} from 'tinybase';

const store = createMergeableStore('store1');

console.log(store.getContent());
// -> [{}, {}]
console.log(store.getMergeableContent());
// -> [[{}, '', 0], [{}, '', 0]]

This example creates a MergeableStore with some initial data:

import {createMergeableStore} from 'tinybase';

const store = createMergeableStore('store1').setTables({
  pets: {fido: {species: 'dog'}},
});

console.log(store.getContent());
// -> [{pets: {fido: {species: 'dog'}}}, {}]
console.log(store.getMergeableContent());
// ->
[
  [
    {
      pets: [
        {
          fido: [
            {species: ['dog', 'Nn1JUF-----FnHIC', 290599168]},
            '',
            2682656941,
          ],
        },
        '',
        2102515304,
      ],
    },
    '',
    3506229770,
  ],
  [{}, '', 0],
];

This example creates a MergeableStore with some initial data and a TablesSchema:

import {createMergeableStore} from 'tinybase';

const store = createMergeableStore('store1')
  .setTables({pets: {fido: {species: 'dog'}}})
  .setTablesSchema({
    pets: {
      species: {type: 'string'},
      sold: {type: 'boolean', default: false},
    },
  });

console.log(store.getContent());
// -> [{pets: {fido: {sold: false, species: 'dog'}}}, {}]
console.log(store.getMergeableContent());
// ->
[
  [
    {
      pets: [
        {
          fido: [
            {
              sold: [false, '', 3177411910],
              species: ['dog', 'Nn1JUF----1FnHIC', 2817056260],
            },
            '',
            3078067457,
          ],
        },
        '',
        1137907280,
      ],
    },
    '',
    401719767,
  ],
  [{}, '', 0],
];

Since

v5.0.0