Store

Public Class

Table of Contents

A store of records.

Signature
class Store<R extends BaseRecord = BaseRecord, Props = unknown> {}
References

BaseRecord


Constructor

Public Constructor

Constructs a new instance of the Store class

Parameters
NameDescription

config

{
  initialData?: StoreSnapshot<R>
  schema: StoreSchema<R, Props>
  props: Props
}
References

StoreSnapshot, StoreSchema


Properties

allRecords

Public Property

Get an array of all values in the store.

Signature
allRecords: () => R[]

clear

Public Property

Removes all records from the store.

Signature
clear: () => void

createComputedCache

Public Property

Create a computed cache.

Parameters
NameDescription

name

The name of the derivation cache.

derive

A function used to derive the value of the cache.

Signature
createComputedCache: <T, V extends R = R>(
  name: string,
  derive: (record: V) => T | undefined
) => ComputedCache<T, V>
References

ComputedCache


createSelectedComputedCache

Public Property

Create a computed cache from a selector

Parameters
NameDescription

name

The name of the derivation cache.

selector

A function that returns a subset of the original shape

derive

A function used to derive the value of the cache.

Signature
createSelectedComputedCache: <T, J, V extends R = R>(
  name: string,
  selector: (record: V) => T | undefined,
  derive: (input: T) => J | undefined
) => ComputedCache<J, V>
References

ComputedCache


deserialize

Public Property

Opposite of serialize. Replace the store's current records with records as defined by a simple JSON structure into the stores.

Parameters
NameDescription

snapshot

The JSON snapshot to deserialize.

Signature
deserialize: (snapshot: StoreSnapshot<R>) => void
References

StoreSnapshot


get

Public Property

Get the value of a store record by its id.

Parameters
NameDescription

id

The id of the record to get.

Signature
get: <K extends ID<R>>(id: K) => RecFromId<K> | undefined
References

ID, RecFromId


has

Public Property

Get whether the record store has a id.

Parameters
NameDescription

id

The id of the record to check.

Signature
has: <K extends ID<R>>(id: K) => boolean
References

ID


history

Public Readonly Property

An atom containing the store's history.

Signature
readonly history: Atom<number, RecordsDiff<R>>
References

Atom, RecordsDiff


listen

Public Property

Add a new listener to the store.

Parameters
NameDescription

listener

The listener to call when the store updates.

Signature
listen: (listener: StoreListener<R>) => () => void
References

StoreListener


mergeRemoteChanges

Public Property

Merge changes from a remote source without triggering listeners.

Parameters
NameDescription

fn

A function that merges the external changes.

Signature
mergeRemoteChanges: (fn: () => void) => void

onAfterChange

Public Property

A callback fired after each record's change.

Parameters
NameDescription

prev

The previous value, if any.

next

The next value.

Signature
onAfterChange?: (prev: R, next: R) => void

onAfterCreate

Public Property

A callback fired after a record is created. Use this to perform related updates to other records in the store.

Parameters
NameDescription

record

The record to be created

Signature
onAfterCreate?: (record: R) => void

onAfterDelete

Public Property

A callback fired after a record is deleted.

Parameters
NameDescription

prev

The record that will be deleted.

Signature
onAfterDelete?: (prev: R) => void

onBeforeDelete

Public Property

A callback fired before a record is deleted.

Parameters
NameDescription

prev

The record that will be deleted.

Signature
onBeforeDelete?: (prev: R) => void

props

Public Readonly Property

Signature
readonly props: Props

put

Public Property

Add some records to the store. It's an error if they already exist.

Parameters
NameDescription

records

The records to add.

Signature
put: (records: R[], phaseOverride?: 'initialize') => void

query

Public Readonly Property

A StoreQueries instance for this store.

Signature
readonly query: StoreQueries<R>
References

StoreQueries


remove

Public Property

Remove some records from the store via their ids.

Parameters
NameDescription

ids

The ids of the records to remove.

Signature
remove: (ids: ID<R>[]) => void
References

ID


schema

Public Readonly Property

Signature
readonly schema: StoreSchema<R, Props>
References

StoreSchema


serialize

Public Property

Opposite of deserialize. Creates a JSON payload from the record store.

Parameters
NameDescription

filter

A function to filter structs that do not satisfy the predicate.

Signature
serialize: (filter?: ((record: R) => boolean) | undefined) => StoreSnapshot<R>
References

StoreSnapshot


unsafeGetWithoutCapture

Public Property

Get the value of a store record by its id without updating its epoch.

Parameters
NameDescription

id

The id of the record to get.

Signature
unsafeGetWithoutCapture: <K extends ID<R>>(id: K) => RecFromId<K> | undefined
References

ID, RecFromId


update

Public Property

Update a record. To update multiple records at once, use the update method of the TypedStore class.

Parameters
NameDescription

id

The id of the record to update.

updater

A function that updates the record.

Signature
update: <K extends ID<R>>(
  id: K,
  updater: (record: RecFromId<K>) => RecFromId<K>
) => void
References

ID, RecFromId


Methods

_flushHistory()

Public Method

Parameters

None

Returns
void

applyDiff()

Public Method

Parameters
NameDescription

diff

RecordsDiff<R>

runCallbacks

(optional)

boolean
Returns
void
References

RecordsDiff


extractingChanges()

Public Method

Parameters
NameDescription

fn

() => void
Returns
RecordsDiff<R>
References

RecordsDiff


validate()

Public Method

Parameters
NameDescription

phase

  | 'createRecord'
  | 'initialize'
  | 'tests'
  | 'updateRecord'
Returns
void

Edit this page
Last edited on 11 May 2023
squashRecordDiffsStoreError