Store
Table of contents
Public class
A store of records.
class Store<R extends UnknownRecord = UnknownRecord, Props = unknown> {}
packages/store/src/lib/Store.ts
Constructor
Public constructor
Constructs a new instance of the Store
class
Name | Description |
---|---|
|
|
Properties
allRecords
Public property
Get an array of all values in the store.
allRecords: () => R[]
clear
Public property
Removes all records from the store.
clear: () => void
createComputedCache
Public property
Create a computed cache.
createComputedCache: <T, V extends R = R>(
name: string,
derive: (record: V) => T | undefined,
isEqual?: ((a: V, b: V) => boolean) | undefined
) => ComputedCache<T, V>
Name | Description |
---|---|
| The name of the derivation cache. |
| A function used to derive the value of the cache. |
createSelectedComputedCache
Public property
Create a computed cache from a selector
createSelectedComputedCache: <T, J, V extends R = R>(
name: string,
selector: (record: V) => T | undefined,
derive: (input: T) => J | undefined
) => ComputedCache<J, V>
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. |
get
Public property
Get the value of a store record by its id.
get: <K extends IdOf<R>>(id: K) => RecFromId<K> | undefined
Name | Description |
---|---|
| The id of the record to get. |
has
Public property
Get whether the record store has a id.
has: <K extends IdOf<R>>(id: K) => boolean
Name | Description |
---|---|
| The id of the record to check. |
history
Public readonly property
An atom containing the store's history.
readonly history: Atom<number, RecordsDiff<R>>
id
Public readonly property
The random id of the store.
readonly id: string
listen
Public property
Add a new listener to the store.
listen: (
onHistory: StoreListener<R>,
filters?: Partial<StoreListenerFilters>
) => () => void
Name | Description |
---|---|
| The listener to call when the store updates. |
| Filters to apply to the listener. |
mergeRemoteChanges
Public property
Merge changes from a remote source without triggering listeners.
mergeRemoteChanges: (fn: () => void) => void
Name | Description |
---|---|
| A function that merges the external changes. |
onAfterChange
Public property
A callback fired after each record's change.
onAfterChange?: (prev: R, next: R, source: 'remote' | 'user') => void
Name | Description |
---|---|
| The previous value, if any. |
| The next value. |
onAfterCreate
Public property
A callback fired after a record is created. Use this to perform related updates to other records in the store.
onAfterCreate?: (record: R, source: 'remote' | 'user') => void
Name | Description |
---|---|
| The record to be created |
onAfterDelete
Public property
A callback fired after a record is deleted.
onAfterDelete?: (prev: R, source: 'remote' | 'user') => void
Name | Description |
---|---|
| The record that will be deleted. |
onBeforeChange
Public property
A callback fired before each record's change.
onBeforeChange?: (prev: R, next: R, source: 'remote' | 'user') => R
Name | Description |
---|---|
| The previous value, if any. |
| The next value. |
onBeforeCreate
Public property
A callback fired after each record's change.
onBeforeCreate?: (next: R, source: 'remote' | 'user') => R
Name | Description |
---|---|
| The previous value, if any. |
| The next value. |
onBeforeDelete
Public property
A callback fired before a record is deleted.
onBeforeDelete?: (prev: R, source: 'remote' | 'user') => false | void
Name | Description |
---|---|
| The record that will be deleted. |
props
Public readonly property
readonly props: Props
put
Public property
Add some records to the store. It's an error if they already exist.
put: (records: R[], phaseOverride?: 'initialize') => void
Name | Description |
---|---|
| The records to add. |
query
Public readonly property
A StoreQueries instance for this store.
readonly query: StoreQueries<R>
remove
Public property
Remove some records from the store via their ids.
remove: (ids: IdOf<R>[]) => void
Name | Description |
---|---|
| The ids of the records to remove. |
schema
Public readonly property
readonly schema: StoreSchema<R, Props>
scopedTypes
Public readonly property
readonly scopedTypes: {
readonly [K in RecordScope]: ReadonlySet<R['typeName']>
}
serialize
Public property
Creates a JSON payload from the record store.
serialize: (scope?: 'all' | RecordScope) => SerializedStore<R>
Name | Description |
---|---|
| The scope of records to serialize. Defaults to 'document'. |
unsafeGetWithoutCapture
Public property
Get the value of a store record by its id without updating its epoch.
unsafeGetWithoutCapture: <K extends IdOf<R>>(
id: K
) => RecFromId<K> | undefined
Name | Description |
---|---|
| The id of the record to get. |
update
Public property
Update a record. To update multiple records at once, use the update
method of the TypedStore
class.
update: <K extends IdOf<R>>(
id: K,
updater: (record: RecFromId<K>) => RecFromId<K>
) => void
Name | Description |
---|---|
| The id of the record to update. |
| A function that updates the record. |
Methods
applyDiff()
Public method
applyDiff(
diff: RecordsDiff<R>,
{
runCallbacks,
ignoreEphemeralKeys,
}?: {
ignoreEphemeralKeys?: boolean
runCallbacks?: boolean
}
): void
Name | Description |
---|---|
|
|
|
|
void
extractingChanges()
Public method
Run fn
and return a RecordsDiff of the changes that occurred as a result.
extractingChanges(fn: () => void): RecordsDiff<R>
Name | Description |
---|---|
|
|
RecordsDiff<R>
filterChangesByScope()
Public method
Filters out non-document changes from a diff. Returns null if there are no changes left.
filterChangesByScope(
change: RecordsDiff<R>,
scope: RecordScope
): {
added: { [K in IdOf<R>]: R }
removed: { [K in IdOf<R>]: R }
updated: { [K_1 in IdOf<R>]: [from: R, to: R] }
} | null
Name | Description |
---|---|
|
the records diff |
|
|
{
added: { [K in IdOf<R>]: R }
removed: { [K in IdOf<R>]: R }
updated: { [K_1 in IdOf<R>]: [from: R, to: R] }
} | null
getSnapshot()
Public method
Get a serialized snapshot of the store and its schema.
const snapshot = store.getSnapshot()
store.loadSnapshot(snapshot)
getSnapshot(scope?: 'all' | RecordScope): StoreSnapshot<R>
Name | Description |
---|---|
|
The scope of records to serialize. Defaults to 'document'. |
StoreSnapshot<R>
loadSnapshot()
Public method
Load a serialized snapshot.
const snapshot = store.getSnapshot()
store.loadSnapshot(snapshot)
loadSnapshot(snapshot: StoreSnapshot<R>): void
Name | Description |
---|---|
|
The snapshot to load. |
void
migrateSnapshot()
Public method
Migrate a serialized snapshot of the store and its schema.
const snapshot = store.getSnapshot()
store.migrateSnapshot(snapshot)
migrateSnapshot(snapshot: StoreSnapshot<R>): StoreSnapshot<R>
Name | Description |
---|---|
|
The snapshot to load. |
StoreSnapshot<R>
validate()
Public method
validate(
phase: 'createRecord' | 'initialize' | 'tests' | 'updateRecord'
): void
Name | Description |
---|---|
|
|
void