Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Index<Item, Trait>

An index on an object store.

An index is a way of organizing stored items to be queried later. You set up an index to keep track of particular attributes of your items (such as a .id property). The attribute that you track is known as the trait. You can then query indexes to find items based on their traits.

Type parameters

  • Item: Storable

    The type of the item stored on the Store that this indexes is connected to.

  • Trait: Indexable

    The type of the traits being indexed by.

Hierarchy

  • Index

Index

Accessors

explode

  • If explode is true, then items' values for this index are expected to be arrays. Each value in an array will be added to the index, instead of the array being added as a whole.

    For instance, say we're storing type User = { id: number, likedPostIds: Array<number> } objects. We may have an index called liked which is intended to organize users by what posts they've liked. If the index is not exploding, then the user { id: 1, likedPostIds: [1, 2, 3] } will match a query for [1, 2, 3] but not a query for 1, for 2, or for 3. But if the index is exploding, then this same user will match a query for each of 1, 2, and 3, but not a query for [1, 2, 3].

    Returns Awaitable<boolean>

kind

  • Indexes come in two flavors: path indexes and derived indexes.

    With a path index, items are indexed on existing properties. For example, if we're storing array objects, we may index by .length. The path is stored in Index.traitPath.

    With a derived index, items are indexed on calculated values. For example, storing an array object, we may want to index by whether or not the array has a duplicate value. Then we would index by the function (item: Array) => item.length !== new Set(item).size (or a more efficient alternative). This function is stored in Index.traitGetter.

    Returns Awaitable<"path" | "derived">

name

traitGetter

  • get traitGetter(): Awaitable<undefined | ((item: Item) => Trait)>
  • If this.kind === 'derived', return the trait computing function.

    Returns Awaitable<undefined | ((item: Item) => Trait)>

traitPath

  • get traitPath(): Awaitable<undefined | string>

unique

Methods

exists

  • exists(trait: Trait): Promise<boolean>
  • Test if there are any items with the given trait

    Parameters

    • trait: Trait

    Returns Promise<boolean>

getAll

  • getAll(trait: Trait): Promise<Array<Item>>
  • Retrieve all items matching a given trait.

    Parameters

    • trait: Trait

    Returns Promise<Array<Item>>

getOne

  • getOne(trait: Trait): Promise<Item>
  • Get an item by trait.

    Usable on unique indexes only.

    Throws if no item is found.

    Parameters

    • trait: Trait

    Returns Promise<Item>

getOneOr

  • getOneOr<T>(trait: Trait, alternative: T): Promise<Item | T>
  • Get an item by trait, or return something else if the item isn't found.

    Usable on unique indexes only.

    Type parameters

    • T

    Parameters

    • trait: Trait
    • alternative: T

    Returns Promise<Item | T>

select

selectOne

updateOrAdd

  • updateOrAdd(item: Item): Promise<void>
  • Update an item if it exists, or add a new one if it doesn't.

    Allowed on unique indexes only.

    Parameters

    • item: Item

    Returns Promise<void>

updateTraitGetter

  • updateTraitGetter(newGetter: (item: Item) => Trait): Promise<void>
  • Updates the trait getter on a derived index.

    Only usable during a migration.

    Parameters

    • newGetter: (item: Item) => Trait
        • (item: Item): Trait
        • Parameters

          • item: Item

          Returns Trait

    Returns Promise<void>

updateTraitPath

  • updateTraitPath(newPath: string): Promise<void>
  • Updates the trait path on a path index.

    Only usable during a migration.

    Parameters

    • newPath: string

    Returns Promise<void>

Generated using TypeDoc