TinyBase logoTinyBase β

createMergeableStore

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

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

An optional unique Id for the MergeableStore.

getNow?GetNow

An optional function that generates millisecond timestamps, since v6.1.0.

returnsMergeableStore

A reference to the new MergeableStore.

There are two 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).

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, 'Nn1JUF----2FnHIC', 2603026204],
              species: ['dog', 'Nn1JUF----1FnHIC', 2817056260],
            },
            '',
            2859424112,
          ],
        },
        '',
        1640515891,
      ],
    },
    '',
    2077041985,
  ],
  [{}, '', 0],
];

Since

v5.0.0