import { Collection, Query } from '@noy-db/hub'; import { QueryClient } from '@tanstack/query-core'; /** * **@noy-db/in-tanstack-query** — TanStack Query adapter for noy-db. * * Three framework-agnostic pieces: * * 1. **{@link collectionQueryOptions}** — returns a * `{ queryKey, queryFn }` pair ready to hand to `useQuery()` in * any TanStack-supported framework (React, Vue, Solid, Svelte). * * 2. **{@link collectionMutationOptions}** — same shape for * `useMutation()`, with `put`/`delete` actions against a * `Collection`. * * 3. **{@link bindInvalidation}** — subscribes to a `Collection`'s * change stream and invalidates the matching query keys on every * mutation. Drop-in replacement for ad-hoc `invalidateQueries` * calls scattered through mutation callbacks. * * Keeping these as option-factories (not custom hooks) means the * same code works across every TanStack framework binding — no * framework-specific exports needed. * * @packageDocumentation */ /** The canonical TanStack queryKey shape for a collection scope. */ type CollectionQueryKey = readonly ['noy-db', string, string] | readonly ['noy-db', string, string, string] | readonly ['noy-db', string, string, ...unknown[]]; declare function collectionQueryKey(vault: string, collection: string, ...extra: unknown[]): CollectionQueryKey; /** * Build `{ queryKey, queryFn }` for a Collection list query. Pass the * returned object directly into `useQuery(options)` — works across * every TanStack framework binding. */ declare function collectionListOptions(vault: string, collectionName: string, getCollection: () => Collection): { queryKey: CollectionQueryKey; queryFn: () => Promise; }; /** * Build `{ queryKey, queryFn }` for a single-record query. Returns * `null` when the record is absent. */ declare function collectionGetOptions(vault: string, collectionName: string, id: string, getCollection: () => Collection): { queryKey: CollectionQueryKey; queryFn: () => Promise; }; /** * Build `{ queryKey, queryFn }` for an arbitrary query builder. The * builder receives the collection's `query()` chain and returns a * terminal result (`.toArray()`, `.count()`, `.aggregate({...})`, …). */ declare function collectionQueryOptions(vault: string, collectionName: string, getCollection: () => Collection, builder: (q: Query) => Promise | R, keyTag?: string): { queryKey: CollectionQueryKey; queryFn: () => Promise; }; /** * Build `{ mutationFn }` for a record put. Caller's `onSuccess` is * where you'd usually call `invalidateQueries(collectionQueryKey(...))` * — or let {@link bindInvalidation} do it automatically. */ declare function collectionPutOptions(getCollection: () => Collection): { mutationFn: (args: { id: string; record: T; }) => Promise; }; /** Build `{ mutationFn }` for a record delete. */ declare function collectionDeleteOptions(getCollection: () => Collection): { mutationFn: (args: { id: string; }) => Promise; }; /** * Subscribe to a Collection's change stream and invalidate the * corresponding query scope on every mutation. Returns an unsubscribe * function — call on cleanup (framework-specific). * * ```ts * const stop = bindInvalidation(queryClient, 'acme', 'invoices', invoices) * // … later, e.g. in React's useEffect cleanup: * stop() * ``` */ declare function bindInvalidation(queryClient: QueryClient, vault: string, collectionName: string, collection: Collection): () => void; export { type CollectionQueryKey, bindInvalidation, collectionDeleteOptions, collectionGetOptions, collectionListOptions, collectionPutOptions, collectionQueryKey, collectionQueryOptions };