Skip to main content

createRelationalObjectIndex()

Used to define an object index, to keep track of which objects are on which page. Built to help with pagination.

Basic usage

import { createRelationalObjectIndex } from "@jjmyers/object-relationship-store";

const homeFeed = createRelationalObjectIndex("homeFeed", [post, article, blog], (a, b) => a.id > b.id ? -1 : 1);

const store = createStore({
// ...storeOptions
indexes: [
homeFeed
],
});

Properties

createRelationalObjectIndex(name, relationalCreators, sortingFunction)

name

The name of the index. Will be used when selecting data via store.selectIndex().

createRelationalObjectIndex("homeFeed");

relationalCreators

An array of object types this index is expected to have. Will be able to pass custom selectors when select data from this index using store.selectIndex().

createRelationalObjectIndex("homeFeed", [post, article, blog]);

sortingFunction

Sort the objects in a specific order. When a new object is inserted or deleted, the sortingFunction will make sure that the order of the index is correct.

createRelationalObjectIndex("homeFeed", [...], (a, b) => a.id > b.id ? -1 : 1);

API

Properties

PropertyTypeDefaultDescription
namestringundefinedThe name of a the index.
relationalCreatorsORS.RelationalCreators[]undefinedAn array of relationalCreators (objects) this index is expected to have.
sortingFunctionFunctionundefinedA sorting function. Returns 1 or -1