Indexes
An Indexes
object lets you look up all the Row
objects in a Table
that have a certain Cell
value.
This is useful for creating filtered views of a Table
, or simple search functionality.
Create an Indexes
object easily with the createIndexes
function. From there, you can add new Index
definitions (with the setIndexDefinition
method), query their contents (with the getSliceIds
method and getSliceRowIds
method), and add listeners for when they change (with the addSliceIdsListener
method and addSliceRowIdsListener
method).
This module defaults to indexing Row
objects by one of their Cell
values. However, far more complex indexes can be configured with a custom function.
Example
This example shows a very simple lifecycle of an Indexes
object: from creation, to adding a definition, getting its contents, and then registering and removing a listener for it.
import {createIndexes, createStore} from 'tinybase';
const store = createStore().setTable('pets', {
fido: {species: 'dog'},
felix: {species: 'cat'},
cujo: {species: 'dog'},
});
const indexes = createIndexes(store);
indexes.setIndexDefinition(
'bySpecies', // indexId
'pets', // tableId to index
'species', // cellId to index
);
console.log(indexes.getSliceIds('bySpecies'));
// -> ['dog', 'cat']
console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
// -> ['fido', 'cujo']
const listenerId = indexes.addSliceIdsListener('bySpecies', () => {
console.log(indexes.getSliceIds('bySpecies'));
});
store.setRow('pets', 'lowly', {species: 'worm'});
// -> ['dog', 'cat', 'worm']
indexes.delListener(listenerId);
indexes.destroy();
See also
- Using Indexes guides
- Rolling Dice demos
- Country demo
- Todo App demos
- Word Frequencies demo
Since
v1.0.0
Getter methods
This is the collection of getter methods within the Indexes
interface. There are 7 getter methods in total.
Listener methods
This is the collection of listener methods within the Indexes
interface. There are 4 listener methods in total.
Configuration methods
This is the collection of configuration methods within the Indexes
interface. There are only two configuration methods, delIndexDefinition
and setIndexDefinition
.
Iterator methods
This is the collection of iterator methods within the Indexes
interface. There are only two iterator methods, forEachIndex
and forEachSlice
.
Lifecycle methods
This is the collection of lifecycle methods within the Indexes
interface. There is only one method, destroy
.
Development methods
This is the collection of development methods within the Indexes
interface. There is only one method, getListenerStats
.