TinyBase logoTinyBase

merge

The merge method is a convenience method that applies the mergeable content from two MergeableStores to each other in order to bring them to the same state.

merge(mergeableStore: MergeableStore): MergeableStore
TypeDescription
mergeableStoreMergeableStore

A reference to the other MergeableStore to merge with this one.

returnsMergeableStore

A reference to this MergeableStore.

This method is symmetrical: applying store1 to store2 will have exactly the same effect as applying store2 to store1.

Example

This example merges two MergeableStore objects together. Note how the final part of the timestamps on each Cell give you a clue that the data comes from changes made to different MergeableStore objects.

import {createMergeableStore} from 'tinybase';

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

const store2 = createMergeableStore('store2');
store2.setTables({pets: {felix: {species: 'cat', color: 'tan'}}});

store1.merge(store2);

console.log(store1.getContent());
// ->
[
  {
    pets: {
      felix: {color: 'tan', species: 'cat'},
      fido: {color: 'brown', species: 'dog'},
    },
  },
  {},
];

console.log(store2.getContent());
// ->
[
  {
    pets: {
      felix: {color: 'tan', species: 'cat'},
      fido: {color: 'brown', species: 'dog'},
    },
  },
  {},
];
console.log(store2.getMergeableContent());
// ->
[
  [
    {
      pets: [
        {
          felix: [
            {
              color: ['tan', 'Nn1JUF----0CnH-J', 2576658292],
              species: ['cat', 'Nn1JUF-----CnH-J', 3409607562],
            },
            '',
            4146239216,
          ],
          fido: [
            {
              color: ['brown', 'Nn1JUF----0FnHIC', 1240535355],
              species: ['dog', 'Nn1JUF-----FnHIC', 290599168],
            },
            '',
            3989065420,
          ],
        },
        '',
        4155188296,
      ],
    },
    '',
    972931118,
  ],
  [{}, '', 0],
];

Since

v5.0.0