createStorePersister

stable

Create a persister for qortex-store

Create a persistence adapter that connects qortex-store to a qortex-db instance. Enables automatic hydration on store creation and automatic persistence on every state change.

Subpath Export

Import from 'qortex-db/store' to ensure better tree-shaking. The main 'qortex-db' package only exports the core DB driver.

Signature

import { createStorePersister } from 'qortex-db/store';
function createStorePersister<T>(
db: DB,
config?: StorePersisterConfig<T>
): StorePersister<T>

Parameters

db

DBRequired

A qortex-db instance created via createDB().

config

StorePersisterConfigDefault: {}

Optional configuration.

config.burstKey

stringDefault: '1'

Cache-bust key. When changed, stale persisted data is discarded.

config.storageKey

stringDefault: 'qortex-store-snapshot'

Key used inside the DB to store the snapshot.

config.debounceTime

numberDefault: 100

Debounce delay (ms) before writing to DB.

config.select

(state: T) => anyDefault: undefined

Function to select only a specific slice of state to be persisted.

Returns

Returns

StorePersister

An object compatible with qortex-store's StorePersister interface.

Examples

Basic Usage

Enable automatic persistence for a store

import { createDB } from 'qortex-db';
import { createStorePersister } from 'qortex-db/store';
import { createStore } from 'qortex-store';
const db = createDB('myapp');
const store = createStore(
(set) => ({ theme: 'light', count: 0 }),
{
persister: createStorePersister(db, {
storageKey: 'settings',
select: (s) => ({ theme: s.theme }) // Only persist theme
})
}
);