Table of contents

Public class

A store of records.

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

UnknownRecord

Source

packages/store/src/lib/Store.ts


Constructor

Public constructor

Constructs a new instance of the Store class

Parameters
NameDescription

config

{
  schema: StoreSchema<R, Props>
  initialData?: SerializedStore<R>
  id?: string
  props: Props
}
References

StoreSchema, SerializedStore


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.

Signature
createComputedCache: <Result, Record extends R = R>(
  name: string,
  derive: (record: Record) => Result | undefined,
  isEqual?: ((a: Record, b: Record) => boolean) | undefined
) => ComputedCache<Result, Record>
Parameters
NameDescription

name

The name of the derivation cache.

derive

A function used to derive the value of the cache.

References

ComputedCache


createSelectedComputedCache

Public property

Create a computed cache from a selector

Signature
createSelectedComputedCache: <Selection, Result, Record extends R = R>(
  name: string,
  selector: (record: Record) => Selection | undefined,
  derive: (input: Selection) => Result | undefined
) => ComputedCache<Result, Record>
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.

References

ComputedCache


get

Public property

Get the value of a store record by its id.

Signature
get: <K extends IdOf<R>>(id: K) => RecFromId<K> | undefined
Parameters
NameDescription

id

The id of the record to get.

References

IdOf


has

Public property

Get whether the record store has a id.

Signature
has: <K extends IdOf<R>>(id: K) => boolean
Parameters
NameDescription

id

The id of the record to check.

References

IdOf


history

Public readonly property

An atom containing the store's history.

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

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.

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

onHistory

The listener to call when the store updates.

filters

Filters to apply to the listener.

References

StoreListener


mergeRemoteChanges

Public property

Merge changes from a remote source without triggering listeners.

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

fn

A function that merges the external changes.


onAfterChange

Public property

A callback fired after each record's change.

Signature
onAfterChange?: (prev: R, next: R, source: 'remote' | 'user') => void
Parameters
NameDescription

prev

The previous value, if any.

next

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.

Signature
onAfterCreate?: (record: R, source: 'remote' | 'user') => void
Parameters
NameDescription

record

The record to be created


onAfterDelete

Public property

A callback fired after a record is deleted.

Signature
onAfterDelete?: (prev: R, source: 'remote' | 'user') => void
Parameters
NameDescription

prev

The record that will be deleted.


onBeforeChange

Public property

A callback fired before each record's change.

Signature
onBeforeChange?: (prev: R, next: R, source: 'remote' | 'user') => R
Parameters
NameDescription

prev

The previous value, if any.

next

The next value.


onBeforeCreate

Public property

A callback fired after each record's change.

Signature
onBeforeCreate?: (next: R, source: 'remote' | 'user') => R
Parameters
NameDescription

prev

The previous value, if any.

next

The next value.


onBeforeDelete

Public property

A callback fired before a record is deleted.

Signature
onBeforeDelete?: (prev: R, source: 'remote' | 'user') => false | void
Parameters
NameDescription

prev

The record that will be deleted.


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.

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

records

The records to add.


query

Public readonly property

A StoreQueries instance for this store.

Signature
readonly query: StoreQueries<R>

remove

Public property

Remove some records from the store via their ids.

Signature
remove: (ids: IdOf<R>[]) => void
Parameters
NameDescription

ids

The ids of the records to remove.

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']>
}

serialize

Public property

Creates a JSON payload from the record store.

Signature
serialize: (scope?: 'all' | RecordScope) => SerializedStore<R>
Parameters
NameDescription

scope

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

References

SerializedStore


unsafeGetWithoutCapture

Public property

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

Signature
unsafeGetWithoutCapture: <K extends IdOf<R>>(
  id: K
) => RecFromId<K> | undefined
Parameters
NameDescription

id

The id of the record to get.

References

IdOf


update

Public property

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

Signature
update: <K extends IdOf<R>>(
  id: K,
  updater: (record: RecFromId<K>) => RecFromId<K>
) => void
Parameters
NameDescription

id

The id of the record to update.

updater

A function that updates the record.

References

IdOf


Methods


applyDiff()

Public method

Signature
applyDiff(
  diff: RecordsDiff<R>,
  {
    runCallbacks,
    ignoreEphemeralKeys,
  }?: {
    ignoreEphemeralKeys?: boolean
    runCallbacks?: boolean
  }
): void
Parameters
NameDescription

diff

RecordsDiff<R>

{ runCallbacks, ignoreEphemeralKeys, }

{
  ignoreEphemeralKeys?: boolean
  runCallbacks?: boolean
}
Returns
void
References

RecordsDiff


extractingChanges()

Public method

Run fn and return a RecordsDiff of the changes that occurred as a result.

Signature
extractingChanges(fn: () => void): RecordsDiff<R>
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.

Signature
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
Parameters
NameDescription

change

RecordsDiff<R>

the records diff

scope

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

RecordsDiff, IdOf


getSnapshot()

Public method

Get a serialized snapshot of the store and its schema.

const snapshot = store.getSnapshot()
store.loadSnapshot(snapshot)
Signature
getSnapshot(scope?: 'all' | RecordScope): StoreSnapshot<R>
Parameters
NameDescription

scope

'all' | RecordScope

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

Returns
StoreSnapshot<R>
References

StoreSnapshot


loadSnapshot()

Public method

Load a serialized snapshot.

const snapshot = store.getSnapshot()
store.loadSnapshot(snapshot)
Signature
loadSnapshot(snapshot: StoreSnapshot<R>): void
Parameters
NameDescription

snapshot

StoreSnapshot<R>

The snapshot to load.

Returns
void
References

StoreSnapshot


migrateSnapshot()

Public method

Migrate a serialized snapshot of the store and its schema.

const snapshot = store.getSnapshot()
store.migrateSnapshot(snapshot)
Signature
migrateSnapshot(snapshot: StoreSnapshot<R>): StoreSnapshot<R>
Parameters
NameDescription

snapshot

StoreSnapshot<R>

The snapshot to load.

Returns
StoreSnapshot<R>
References

StoreSnapshot


validate()

Public method

Signature
validate(
  phase: 'createRecord' | 'initialize' | 'tests' | 'updateRecord'
): void
Parameters
NameDescription

phase

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

StandaloneDependsOnStoreError