Store

Public Class

Table of Contents

A store of records.

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

UnknownRecord


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


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 IdOf<R>>(id: K) => RecFromId<K> | undefined
References

IdOf, 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 IdOf<R>>(id: K) => boolean
References

IdOf


history

Public Readonly Property

An atom containing the store's history.

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

Atom, RecordsDiff


id

Public Readonly Property

The random id of the store.

Signature
readonly id: string

listen

Public Property

Add a new listener to the store.

Parameters
NameDescription

onHistory

The listener to call when the store updates.

filters

Filters to apply to the listener.

Signature
listen: (
  onHistory: StoreListener<R>,
  filters?: Partial<StoreListenerFilters>
) => () => void
References

StoreListener, Partial, StoreListenerFilters


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: IdOf<R>[]) => void
References

IdOf


schema

Public Readonly Property

Signature
readonly schema: StoreSchema<R, Props>
References

StoreSchema


scopedTypes

Public Readonly Property

Signature
readonly scopedTypes: {
  readonly [K in RecordScope]: ReadonlySet<R['typeName']>
}
References

RecordScope, ReadonlySet


serialize

Public Property

Creates a JSON payload from the record store.

Parameters
NameDescription

scope

The scope of records to serialize. Defaults to 'document'.

Signature
serialize: (scope?: 'all' | RecordScope) => StoreSnapshot<R>
References

RecordScope, 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 IdOf<R>>(
  id: K
) => RecFromId<K> | undefined
References

IdOf, 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 IdOf<R>>(
  id: K,
  updater: (record: RecFromId<K>) => RecFromId<K>
) => void
References

IdOf, 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


filterChangesByScope()

Public Method

Filters out non-document changes from a diff. Returns null if there are no changes left.

Parameters
NameDescription

change

RecordsDiff<R>

the records diff

scope

RecordScope
Returns
{
  added: { [K in IdOf<R>]: R }
  updated: { [K_1 in IdOf<R>]: [from: R, to: R] }
  removed: { [K in IdOf<R>]: R }
} | null
References

RecordsDiff, RecordScope, IdOf


getSnapshot()

Public Method

Get a serialized snapshot of the store and its schema.

const snapshot = store.getSnapshot()
store.loadSnapshot(snapshot)
Parameters
NameDescription

scope

(optional)

'all' | RecordScope

The scope of records to serialize. Defaults to 'document'.

Returns
{
  store: StoreSnapshot<R>
  schema: SerializedSchema
}
References

RecordScope, StoreSnapshot, SerializedSchema


loadSnapshot()

Public Method

Load a serialized snapshot.

const snapshot = store.getSnapshot()
store.loadSnapshot(snapshot)
Parameters
NameDescription

snapshot

{
  store: StoreSnapshot<R>
  schema: SerializedSchema
}

The snapshot to load.

Returns
void
References

StoreSnapshot, SerializedSchema


validate()

Public Method

Parameters
NameDescription

phase

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

Edit this page
Last edited on 15 June 2023
squashRecordDiffsStoreError