Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

source-id-bindings.ts

29 lines938 Bytes

1import { getPreparedLiveQuerySources } from '@tanstack/db'
2import type { DbClient } from '@tanstack/db'
3​
4/**
5 * Reports a descriptor source that no DbClient bound, naming the provider a
6 * React tree needs. Returns `undefined` when a client can bind descriptors.
7 */
8export function getUnboundDescriptorReporter(
9 client: DbClient | undefined,
10 hookName: string,
11): ((alias: string) => never) | undefined {
12 if (client) return undefined
13 return (alias) => {
14 throw new Error(
15 `[${hookName}] Collection descriptor "${alias}" requires a DbClient when the query is consumed. Wrap this component in <DbProvider client={client}> or use a concrete Collection.`,
16 )
17 }
19​
20export function getPreparedSources(
21 preparedValue: unknown,
22 client: DbClient | undefined,
23 hookName: string,
24): Array<{ id: string }> {
25 return getPreparedLiveQuerySources(
26 preparedValue,
27 getUnboundDescriptorReporter(client, hookName),
28 )