TinyBase logoTinyBase

setDefaultContent

The setDefaultContent method sets initial content of a MergeableStore.

setDefaultContent(content: Content | () => Content): MergeableStore
TypeDescription
contentContent | () => Content

An array containing the tabular and keyed-value data to be set, or a function that returns the array.

returnsMergeableStore

A reference to the MergeableStore.

This differs from the setMergeableContent method in that all of the metadata is initialized with a empty HLC timestamp - meaning that any changes applied to it will 'win', yet ensuring that at least default, initial data exists.

The method is generally intended to be used internally within TinyBase itself and the return type is assumed to be opaque to applications that use it.

Since v5.4.2, this method can also take a function that returns the content.

Example

This example creates a new MergeableStore with default data, and demonstrates that it is overwritten with another MergeableStore's data on merge, even if the other MergeableStore was provisioned earlier.

import {createMergeableStore} from 'tinybase';

const store1 = createMergeableStore('store1');
store1.setValues({employees: 3});

const store2 = createMergeableStore('store2');
store2.setDefaultContent([{}, {employees: 4}]);
console.log(store2.getMergeableContent());
// -> [[{}, "", 0], [{"employees": [4, "", 2414055963]}, "", 3035768673]]

store2.merge(store1);
console.log(store2.getContent());
// -> [{}, {employees: 3}]

Since

v5.0.0