setCell
The setCell method sets the value of a single Cell in the Store.
setCell(
tableId: string,
rowId: string,
cellId: string,
cell: Cell | MapCell,
): this| Type | Description | |
|---|---|---|
tableId | string | |
rowId | string | |
cellId | string | |
cell | Cell | MapCell | The value of the |
| returns | this | A reference to the |
This method will cause listeners to be called for any Table, Row, Cell, or Id changes resulting from it.
If the Cell value is invalid (either because of its type, or because it does not match a TablesSchema associated with the Store), will be ignored silently.
As well as string, number, or boolean Cell types, this method can also take a MapCell function that takes the current Cell value as a parameter and maps it. This is useful if you want to efficiently increment a value without fetching it first, for example.
The method returns a reference to the Store so that subsequent operations can be chained in a fluent style.
Examples
This example sets the value of a single Cell.
import {createStore} from 'tinybase';
const store = createStore().setCell('pets', 'fido', 'species', 'dog');
console.log(store.getTables());
// -> {pets: {fido: {species: 'dog'}}}
This example sets the data of a single Cell by mapping the existing value.
import {createStore} from 'tinybase';
const increment = (cell) => cell + 1;
const store = createStore().setTables({pets: {fido: {visits: 1}}});
store.setCell('pets', 'fido', 'visits', increment);
console.log(store.getCell('pets', 'fido', 'visits'));
// -> 2
This example attempts to set the data of an existing Store with an invalid Cell value.
import {createStore} from 'tinybase';
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
store.setCell('pets', 'fido', 'bug', []);
console.log(store.getTables());
// -> {pets: {fido: {species: 'dog'}}}
Since
v1.0.0