Store
Public Class
Table of Contents
A store of records.
Signature
class Store<R extends UnknownRecord = UnknownRecord, Props = unknown> {}
References
Constructor
Public Constructor
Constructs a new instance of the Store
class
Parameters
Name | Description |
---|---|
|
|
References
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
Name | Description |
---|---|
| The name of the derivation cache. |
| 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
createSelectedComputedCache
Public Property
Create a computed cache from a selector
Parameters
Name | Description |
---|---|
| The name of the derivation cache. |
| A function that returns a subset of the original shape |
| 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
get
Public Property
Get the value of a store record by its id.
Parameters
Name | Description |
---|---|
| The id of the record to get. |
Signature
get: <K extends IdOf<R>>(id: K) => RecFromId<K> | undefined
References
has
Public Property
Get whether the record store has a id.
Parameters
Name | Description |
---|---|
| The id of the record to check. |
Signature
has: <K extends IdOf<R>>(id: K) => boolean
References
history
Public Readonly Property
An atom containing the store's history.
Signature
readonly history: Atom<number, RecordsDiff<R>>
References
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
Name | Description |
---|---|
| The listener to call when the store updates. |
| 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
Name | Description |
---|---|
| A function that merges the external changes. |
Signature
mergeRemoteChanges: (fn: () => void) => void
onAfterChange
Public Property
A callback fired after each record's change.
Parameters
Name | Description |
---|---|
| The previous value, if any. |
| 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
Name | Description |
---|---|
| The record to be created |
Signature
onAfterCreate?: (record: R) => void
onAfterDelete
Public Property
A callback fired after a record is deleted.
Parameters
Name | Description |
---|---|
| The record that will be deleted. |
Signature
onAfterDelete?: (prev: R) => void
onBeforeDelete
Public Property
A callback fired before a record is deleted.
Parameters
Name | Description |
---|---|
| 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
Name | Description |
---|---|
| 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
remove
Public Property
Remove some records from the store via their ids.
Parameters
Name | Description |
---|---|
| The ids of the records to remove. |
Signature
remove: (ids: IdOf<R>[]) => void
References
schema
Public Readonly Property
Signature
readonly schema: StoreSchema<R, Props>
References
scopedTypes
Public Readonly Property
Signature
readonly scopedTypes: {
readonly [K in RecordScope]: ReadonlySet<R['typeName']>
}
References
serialize
Public Property
Creates a JSON payload from the record store.
Parameters
Name | Description |
---|---|
| The scope of records to serialize. Defaults to 'document'. |
Signature
serialize: (scope?: 'all' | RecordScope) => StoreSnapshot<R>
References
unsafeGetWithoutCapture
Public Property
Get the value of a store record by its id without updating its epoch.
Parameters
Name | Description |
---|---|
| The id of the record to get. |
Signature
unsafeGetWithoutCapture: <K extends IdOf<R>>(
id: K
) => RecFromId<K> | undefined
References
update
Public Property
Update a record. To update multiple records at once, use the update
method of the TypedStore
class.
Parameters
Name | Description |
---|---|
| The id of the record to update. |
| A function that updates the record. |
Signature
update: <K extends IdOf<R>>(
id: K,
updater: (record: RecFromId<K>) => RecFromId<K>
) => void
References
Methods
_flushHistory()
Public Method
Parameters
None
Returns
void
applyDiff()
Public Method
Parameters
Name | Description |
---|---|
|
|
(optional) |
|
Returns
void
References
extractingChanges()
Public Method
Parameters
Name | Description |
---|---|
|
|
Returns
RecordsDiff<R>
References
filterChangesByScope()
Public Method
Filters out non-document changes from a diff. Returns null if there are no changes left.
Parameters
Name | Description |
---|---|
|
the records diff |
|
|
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
Name | Description |
---|---|
(optional) |
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
Name | Description |
---|---|
|
The snapshot to load. |
Returns
void
References
StoreSnapshot, SerializedSchema
validate()
Public Method
Parameters
Name | Description |
---|---|
|
|
Returns
void