import { StateCreator } from 'zustand'; import { Collection } from '@noy-db/hub'; /** * **@noy-db/in-zustand** — Zustand adapter for noy-db. * * Factory that creates a Zustand store backed by a noy-db * `Collection`. Reads hydrate from the collection on first access * and auto-refresh on every change event; writes go through the * collection (and are therefore encrypted + replicated by the sync * engine). * * ```ts * import { create } from 'zustand' * import { createNoydbStore } from '@noy-db/in-zustand' * * const useInvoices = create(createNoydbStore(() => db.vault('acme').collection('invoices'))) * ``` * * The returned state slice shape: * * ```ts * { * records: Record * loading: boolean * error: Error | null * put: (id, value) => Promise * remove: (id) => Promise * refresh: () => Promise * } * ``` * * @packageDocumentation */ interface NoydbZustandSlice { /** Current records keyed by id. */ records: Record; /** True during the initial hydration. */ loading: boolean; /** Error from hydration or a mutation. */ error: Error | null; /** Put a record — encrypts + writes + fires the auto-refresh. */ put(id: string, value: T): Promise; /** Delete a record. */ remove(id: string): Promise; /** Re-hydrate from the underlying collection. */ refresh(): Promise; } /** * Produce a Zustand `StateCreator` backed by a noy-db collection. * * The `getCollection` factory is called lazily on first access so * consumers can wire the store while the `Noydb` instance is still * being opened asynchronously — the first `refresh()` waits for the * factory to succeed before hydrating. */ declare function createNoydbStore(getCollection: () => Collection | Promise>): StateCreator>; export { type NoydbZustandSlice, createNoydbStore };