@tanstack/query-db-collection 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/query.cjs +12 -17
- package/dist/cjs/query.cjs.map +1 -1
- package/dist/cjs/serialization.cjs +94 -0
- package/dist/cjs/serialization.cjs.map +1 -0
- package/dist/cjs/serialization.d.cts +6 -0
- package/dist/esm/query.js +12 -17
- package/dist/esm/query.js.map +1 -1
- package/dist/esm/serialization.d.ts +6 -0
- package/dist/esm/serialization.js +94 -0
- package/dist/esm/serialization.js.map +1 -0
- package/package.json +3 -3
- package/src/query.ts +16 -24
- package/src/serialization.ts +128 -0
package/dist/cjs/query.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const queryCore = require("@tanstack/query-core");
|
|
4
|
-
const db = require("@tanstack/db");
|
|
5
4
|
const errors = require("./errors.cjs");
|
|
6
5
|
const manualSync = require("./manual-sync.cjs");
|
|
6
|
+
const serialization = require("./serialization.cjs");
|
|
7
7
|
class QueryCollectionUtilsImpl {
|
|
8
8
|
constructor(state, refetch, writeUtils) {
|
|
9
9
|
this.state = state;
|
|
@@ -123,7 +123,15 @@ function queryCollectionOptions(config) {
|
|
|
123
123
|
const { begin, write, commit, markReady, collection } = params;
|
|
124
124
|
let syncStarted = false;
|
|
125
125
|
const createQueryFromOpts = (opts = {}, queryFunction = queryFn) => {
|
|
126
|
-
|
|
126
|
+
let key;
|
|
127
|
+
if (typeof queryKey === `function`) {
|
|
128
|
+
key = queryKey(opts);
|
|
129
|
+
} else if (syncMode === `on-demand`) {
|
|
130
|
+
const serialized = serialization.serializeLoadSubsetOptions(opts);
|
|
131
|
+
key = serialized !== void 0 ? [...queryKey, serialized] : queryKey;
|
|
132
|
+
} else {
|
|
133
|
+
key = queryKey;
|
|
134
|
+
}
|
|
127
135
|
const hashedQueryKey = queryCore.hashKey(key);
|
|
128
136
|
const extendedMeta = { ...meta, loadSubsetOptions: opts };
|
|
129
137
|
if (state.observers.has(hashedQueryKey)) {
|
|
@@ -251,16 +259,6 @@ function queryCollectionOptions(config) {
|
|
|
251
259
|
};
|
|
252
260
|
return handleQueryResult;
|
|
253
261
|
};
|
|
254
|
-
const createLocalQuery = (opts) => {
|
|
255
|
-
const queryFn2 = ({ meta: meta2 }) => {
|
|
256
|
-
const inserts = collection.currentStateAsChanges(
|
|
257
|
-
meta2.loadSubsetOptions
|
|
258
|
-
);
|
|
259
|
-
const data = inserts.map(({ value }) => value);
|
|
260
|
-
return Promise.resolve(data);
|
|
261
|
-
};
|
|
262
|
-
createQueryFromOpts(opts, queryFn2);
|
|
263
|
-
};
|
|
264
262
|
const isSubscribed = (hashedQueryKey) => {
|
|
265
263
|
return unsubscribes.has(hashedQueryKey);
|
|
266
264
|
};
|
|
@@ -351,12 +349,9 @@ function queryCollectionOptions(config) {
|
|
|
351
349
|
})
|
|
352
350
|
);
|
|
353
351
|
};
|
|
354
|
-
const loadSubsetDedupe = syncMode === `eager` ? void 0 :
|
|
355
|
-
loadSubset: createQueryFromOpts,
|
|
356
|
-
onDeduplicate: createLocalQuery
|
|
357
|
-
});
|
|
352
|
+
const loadSubsetDedupe = syncMode === `eager` ? void 0 : createQueryFromOpts;
|
|
358
353
|
return {
|
|
359
|
-
loadSubset: loadSubsetDedupe
|
|
354
|
+
loadSubset: loadSubsetDedupe,
|
|
360
355
|
cleanup
|
|
361
356
|
};
|
|
362
357
|
};
|
package/dist/cjs/query.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.cjs","sources":["../../src/query.ts"],"sourcesContent":["import { QueryObserver, hashKey } from \"@tanstack/query-core\"\nimport { DeduplicatedLoadSubset } from \"@tanstack/db\"\nimport {\n GetKeyRequiredError,\n QueryClientRequiredError,\n QueryFnRequiredError,\n QueryKeyRequiredError,\n} from \"./errors\"\nimport { createWriteUtils } from \"./manual-sync\"\nimport type {\n BaseCollectionConfig,\n ChangeMessage,\n CollectionConfig,\n DeleteMutationFnParams,\n InsertMutationFnParams,\n LoadSubsetOptions,\n SyncConfig,\n UpdateMutationFnParams,\n UtilsRecord,\n} from \"@tanstack/db\"\nimport type {\n FetchStatus,\n QueryClient,\n QueryFunctionContext,\n QueryKey,\n QueryObserverOptions,\n QueryObserverResult,\n} from \"@tanstack/query-core\"\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\"\n\n// Re-export for external use\nexport type { SyncOperation } from \"./manual-sync\"\n\n// Schema output type inference helper (matches electric.ts pattern)\ntype InferSchemaOutput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferOutput<T> extends object\n ? StandardSchemaV1.InferOutput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\n// Schema input type inference helper (matches electric.ts pattern)\ntype InferSchemaInput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferInput<T> extends object\n ? StandardSchemaV1.InferInput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\ntype TQueryKeyBuilder<TQueryKey> = (opts: LoadSubsetOptions) => TQueryKey\n\n/**\n * Configuration options for creating a Query Collection\n * @template T - The explicit type of items stored in the collection\n * @template TQueryFn - The queryFn type\n * @template TError - The type of errors that can occur during queries\n * @template TQueryKey - The type of the query key\n * @template TKey - The type of the item keys\n * @template TSchema - The schema type for validation\n */\nexport interface QueryCollectionConfig<\n T extends object = object,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any> = (\n context: QueryFunctionContext<any>\n ) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TSchema extends StandardSchemaV1 = never,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n> extends BaseCollectionConfig<T, TKey, TSchema> {\n /** The query key used by TanStack Query to identify this query */\n queryKey: TQueryKey | TQueryKeyBuilder<TQueryKey>\n /** Function that fetches data from the server. Must return the complete collection state */\n queryFn: TQueryFn extends (\n context: QueryFunctionContext<TQueryKey>\n ) => Promise<Array<any>>\n ? (context: QueryFunctionContext<TQueryKey>) => Promise<Array<T>>\n : TQueryFn\n /* Function that extracts array items from wrapped API responses (e.g metadata, pagination) */\n select?: (data: TQueryData) => Array<T>\n /** The TanStack Query client instance */\n queryClient: QueryClient\n\n // Query-specific options\n /** Whether the query should automatically run (default: true) */\n enabled?: boolean\n refetchInterval?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`refetchInterval`]\n retry?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`retry`]\n retryDelay?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`retryDelay`]\n staleTime?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`staleTime`]\n\n /**\n * Metadata to pass to the query.\n * Available in queryFn via context.meta\n *\n * @example\n * // Using meta for error context\n * queryFn: async (context) => {\n * try {\n * return await api.getTodos(userId)\n * } catch (error) {\n * // Use meta for better error messages\n * throw new Error(\n * context.meta?.errorMessage || 'Failed to load todos'\n * )\n * }\n * },\n * meta: {\n * errorMessage: `Failed to load todos for user ${userId}`\n * }\n */\n meta?: Record<string, unknown>\n}\n\n/**\n * Type for the refetch utility function\n * Returns the QueryObserverResult from TanStack Query\n */\nexport type RefetchFn = (opts?: {\n throwOnError?: boolean\n}) => Promise<QueryObserverResult<any, any> | void>\n\n/**\n * Utility methods available on Query Collections for direct writes and manual operations.\n * Direct writes bypass the normal query/mutation flow and write directly to the synced data store.\n * @template TItem - The type of items stored in the collection\n * @template TKey - The type of the item keys\n * @template TInsertInput - The type accepted for insert operations\n * @template TError - The type of errors that can occur during queries\n */\nexport interface QueryCollectionUtils<\n TItem extends object = Record<string, unknown>,\n TKey extends string | number = string | number,\n TInsertInput extends object = TItem,\n TError = unknown,\n> extends UtilsRecord {\n /** Manually trigger a refetch of the query */\n refetch: RefetchFn\n /** Insert one or more items directly into the synced data store without triggering a query refetch or optimistic update */\n writeInsert: (data: TInsertInput | Array<TInsertInput>) => void\n /** Update one or more items directly in the synced data store without triggering a query refetch or optimistic update */\n writeUpdate: (updates: Partial<TItem> | Array<Partial<TItem>>) => void\n /** Delete one or more items directly from the synced data store without triggering a query refetch or optimistic update */\n writeDelete: (keys: TKey | Array<TKey>) => void\n /** Insert or update one or more items directly in the synced data store without triggering a query refetch or optimistic update */\n writeUpsert: (data: Partial<TItem> | Array<Partial<TItem>>) => void\n /** Execute multiple write operations as a single atomic batch to the synced data store */\n writeBatch: (callback: () => void) => void\n\n // Query Observer State (getters)\n /** Get the last error encountered by the query (if any); reset on success */\n lastError: TError | undefined\n /** Check if the collection is in an error state */\n isError: boolean\n /**\n * Get the number of consecutive sync failures.\n * Incremented only when query fails completely (not per retry attempt); reset on success.\n */\n errorCount: number\n /** Check if query is currently fetching (initial or background) */\n isFetching: boolean\n /** Check if query is refetching in background (not initial fetch) */\n isRefetching: boolean\n /** Check if query is loading for the first time (no data yet) */\n isLoading: boolean\n /** Get timestamp of last successful data update (in milliseconds) */\n dataUpdatedAt: number\n /** Get current fetch status */\n fetchStatus: `fetching` | `paused` | `idle`\n\n /**\n * Clear the error state and trigger a refetch of the query\n * @returns Promise that resolves when the refetch completes successfully\n * @throws Error if the refetch fails\n */\n clearError: () => Promise<void>\n}\n\n/**\n * Internal state object for tracking query observer and errors\n */\ninterface QueryCollectionState {\n lastError: any\n errorCount: number\n lastErrorUpdatedAt: number\n observers: Map<\n string,\n QueryObserver<Array<any>, any, Array<any>, Array<any>, any>\n >\n}\n\n/**\n * Implementation class for QueryCollectionUtils with explicit dependency injection\n * for better testability and architectural clarity\n */\nclass QueryCollectionUtilsImpl {\n private state: QueryCollectionState\n private refetchFn: RefetchFn\n\n // Write methods\n public refetch: RefetchFn\n public writeInsert: any\n public writeUpdate: any\n public writeDelete: any\n public writeUpsert: any\n public writeBatch: any\n\n constructor(\n state: QueryCollectionState,\n refetch: RefetchFn,\n writeUtils: ReturnType<typeof createWriteUtils>\n ) {\n this.state = state\n this.refetchFn = refetch\n\n // Initialize methods to use passed dependencies\n this.refetch = refetch\n this.writeInsert = writeUtils.writeInsert\n this.writeUpdate = writeUtils.writeUpdate\n this.writeDelete = writeUtils.writeDelete\n this.writeUpsert = writeUtils.writeUpsert\n this.writeBatch = writeUtils.writeBatch\n }\n\n public async clearError() {\n this.state.lastError = undefined\n this.state.errorCount = 0\n this.state.lastErrorUpdatedAt = 0\n await this.refetchFn({ throwOnError: true })\n }\n\n // Getters for error state\n public get lastError() {\n return this.state.lastError\n }\n\n public get isError() {\n return !!this.state.lastError\n }\n\n public get errorCount() {\n return this.state.errorCount\n }\n\n // Getters for QueryObserver state\n public get isFetching() {\n // check if any observer is fetching\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isFetching\n )\n }\n\n public get isRefetching() {\n // check if any observer is refetching\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isRefetching\n )\n }\n\n public get isLoading() {\n // check if any observer is loading\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isLoading\n )\n }\n\n public get dataUpdatedAt() {\n // compute the max dataUpdatedAt of all observers\n return Math.max(\n 0,\n ...Array.from(this.state.observers.values()).map(\n (observer) => observer.getCurrentResult().dataUpdatedAt\n )\n )\n }\n\n public get fetchStatus(): Array<FetchStatus> {\n return Array.from(this.state.observers.values()).map(\n (observer) => observer.getCurrentResult().fetchStatus\n )\n }\n}\n\n/**\n * Creates query collection options for use with a standard Collection.\n * This integrates TanStack Query with TanStack DB for automatic synchronization.\n *\n * Supports automatic type inference following the priority order:\n * 1. Schema inference (highest priority)\n * 2. QueryFn return type inference (second priority)\n *\n * @template T - Type of the schema if a schema is provided otherwise it is the type of the values returned by the queryFn\n * @template TError - The type of errors that can occur during queries\n * @template TQueryKey - The type of the query key\n * @template TKey - The type of the item keys\n * @param config - Configuration options for the Query collection\n * @returns Collection options with utilities for direct writes and manual operations\n *\n * @example\n * // Type inferred from queryFn return type (NEW!)\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => {\n * const response = await fetch('/api/todos')\n * return response.json() as Todo[] // Type automatically inferred!\n * },\n * queryClient,\n * getKey: (item) => item.id, // item is typed as Todo\n * })\n * )\n *\n * @example\n * // Explicit type\n * const todosCollection = createCollection<Todo>(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * queryClient,\n * getKey: (item) => item.id,\n * })\n * )\n *\n * @example\n * // Schema inference\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * queryClient,\n * schema: todoSchema, // Type inferred from schema\n * getKey: (item) => item.id,\n * })\n * )\n *\n * @example\n * // With persistence handlers\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: fetchTodos,\n * queryClient,\n * getKey: (item) => item.id,\n * onInsert: async ({ transaction }) => {\n * await api.createTodos(transaction.mutations.map(m => m.modified))\n * },\n * onUpdate: async ({ transaction }) => {\n * await api.updateTodos(transaction.mutations)\n * },\n * onDelete: async ({ transaction }) => {\n * await api.deleteTodos(transaction.mutations.map(m => m.key))\n * }\n * })\n * )\n *\n * @example\n * // The select option extracts the items array from a response with metadata\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * select: (data) => data.items, // Extract the array of items\n * queryClient,\n * schema: todoSchema,\n * getKey: (item) => item.id,\n * })\n * )\n */\n// Overload for when schema is provided and select present\nexport function queryCollectionOptions<\n T extends StandardSchemaV1,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n>(\n config: QueryCollectionConfig<\n InferSchemaOutput<T>,\n TQueryFn,\n TError,\n TQueryKey,\n TKey,\n T\n > & {\n schema: T\n select: (data: TQueryData) => Array<InferSchemaInput<T>>\n }\n): CollectionConfig<\n InferSchemaOutput<T>,\n TKey,\n T,\n QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>\n> & {\n schema: T\n utils: QueryCollectionUtils<\n InferSchemaOutput<T>,\n TKey,\n InferSchemaInput<T>,\n TError\n >\n}\n\n// Overload for when no schema is provided and select present\nexport function queryCollectionOptions<\n T extends object,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any> = (\n context: QueryFunctionContext<any>\n ) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n>(\n config: QueryCollectionConfig<\n T,\n TQueryFn,\n TError,\n TQueryKey,\n TKey,\n never,\n TQueryData\n > & {\n schema?: never // prohibit schema\n select: (data: TQueryData) => Array<T>\n }\n): CollectionConfig<\n T,\n TKey,\n never,\n QueryCollectionUtils<T, TKey, T, TError>\n> & {\n schema?: never // no schema in the result\n utils: QueryCollectionUtils<T, TKey, T, TError>\n}\n\n// Overload for when schema is provided\nexport function queryCollectionOptions<\n T extends StandardSchemaV1,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n>(\n config: QueryCollectionConfig<\n InferSchemaOutput<T>,\n (\n context: QueryFunctionContext<any>\n ) => Promise<Array<InferSchemaOutput<T>>>,\n TError,\n TQueryKey,\n TKey,\n T\n > & {\n schema: T\n }\n): CollectionConfig<\n InferSchemaOutput<T>,\n TKey,\n T,\n QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>\n> & {\n schema: T\n utils: QueryCollectionUtils<\n InferSchemaOutput<T>,\n TKey,\n InferSchemaInput<T>,\n TError\n >\n}\n\n// Overload for when no schema is provided\nexport function queryCollectionOptions<\n T extends object,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n>(\n config: QueryCollectionConfig<\n T,\n (context: QueryFunctionContext<any>) => Promise<Array<T>>,\n TError,\n TQueryKey,\n TKey\n > & {\n schema?: never // prohibit schema\n }\n): CollectionConfig<\n T,\n TKey,\n never,\n QueryCollectionUtils<T, TKey, T, TError>\n> & {\n schema?: never // no schema in the result\n utils: QueryCollectionUtils<T, TKey, T, TError>\n}\n\nexport function queryCollectionOptions(\n config: QueryCollectionConfig<Record<string, unknown>>\n): CollectionConfig<\n Record<string, unknown>,\n string | number,\n never,\n QueryCollectionUtils\n> & {\n utils: QueryCollectionUtils\n} {\n const {\n queryKey,\n queryFn,\n select,\n queryClient,\n enabled,\n refetchInterval,\n retry,\n retryDelay,\n staleTime,\n getKey,\n onInsert,\n onUpdate,\n onDelete,\n meta,\n ...baseCollectionConfig\n } = config\n\n // Default to eager sync mode if not provided\n const syncMode = baseCollectionConfig.syncMode ?? `eager`\n\n // Validate required parameters\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryKey) {\n throw new QueryKeyRequiredError()\n }\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryFn) {\n throw new QueryFnRequiredError()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryClient) {\n throw new QueryClientRequiredError()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!getKey) {\n throw new GetKeyRequiredError()\n }\n\n /** State object to hold error tracking and observer reference */\n const state: QueryCollectionState = {\n lastError: undefined as any,\n errorCount: 0,\n lastErrorUpdatedAt: 0,\n observers: new Map<\n string,\n QueryObserver<Array<any>, any, Array<any>, Array<any>, any>\n >(),\n }\n\n // hashedQueryKey → queryKey\n const hashToQueryKey = new Map<string, QueryKey>()\n\n // queryKey → Set<RowKey>\n const queryToRows = new Map<string, Set<string | number>>()\n\n // RowKey → Set<queryKey>\n const rowToQueries = new Map<string | number, Set<string>>()\n\n // queryKey → QueryObserver's unsubscribe function\n const unsubscribes = new Map<string, () => void>()\n\n // Helper function to add a row to the internal state\n const addRow = (rowKey: string | number, hashedQueryKey: string) => {\n const rowToQueriesSet = rowToQueries.get(rowKey) || new Set()\n rowToQueriesSet.add(hashedQueryKey)\n rowToQueries.set(rowKey, rowToQueriesSet)\n\n const queryToRowsSet = queryToRows.get(hashedQueryKey) || new Set()\n queryToRowsSet.add(rowKey)\n queryToRows.set(hashedQueryKey, queryToRowsSet)\n }\n\n // Helper function to remove a row from the internal state\n const removeRow = (rowKey: string | number, hashedQuerKey: string) => {\n const rowToQueriesSet = rowToQueries.get(rowKey) || new Set()\n rowToQueriesSet.delete(hashedQuerKey)\n rowToQueries.set(rowKey, rowToQueriesSet)\n\n const queryToRowsSet = queryToRows.get(hashedQuerKey) || new Set()\n queryToRowsSet.delete(rowKey)\n queryToRows.set(hashedQuerKey, queryToRowsSet)\n\n return rowToQueriesSet.size === 0\n }\n\n const internalSync: SyncConfig<any>[`sync`] = (params) => {\n const { begin, write, commit, markReady, collection } = params\n\n // Track whether sync has been started\n let syncStarted = false\n\n const createQueryFromOpts = (\n opts: LoadSubsetOptions = {},\n queryFunction: typeof queryFn = queryFn\n ): true | Promise<void> => {\n // Push the predicates down to the queryKey and queryFn\n const key = typeof queryKey === `function` ? queryKey(opts) : queryKey\n const hashedQueryKey = hashKey(key)\n const extendedMeta = { ...meta, loadSubsetOptions: opts }\n\n if (state.observers.has(hashedQueryKey)) {\n // We already have a query for this queryKey\n // Get the current result and return based on its state\n const observer = state.observers.get(hashedQueryKey)!\n const currentResult = observer.getCurrentResult()\n\n if (currentResult.isSuccess) {\n // Data is already available, return true synchronously\n return true\n } else if (currentResult.isError) {\n // Error already occurred, reject immediately\n return Promise.reject(currentResult.error)\n } else {\n // Query is still loading, wait for the first result\n return new Promise<void>((resolve, reject) => {\n const unsubscribe = observer.subscribe((result) => {\n if (result.isSuccess) {\n unsubscribe()\n resolve()\n } else if (result.isError) {\n unsubscribe()\n reject(result.error)\n }\n })\n })\n }\n }\n\n const observerOptions: QueryObserverOptions<\n Array<any>,\n any,\n Array<any>,\n Array<any>,\n any\n > = {\n queryKey: key,\n queryFn: queryFunction,\n meta: extendedMeta,\n structuralSharing: true,\n notifyOnChangeProps: `all`,\n\n // Only include options that are explicitly defined to allow QueryClient defaultOptions to be used\n ...(enabled !== undefined && { enabled }),\n ...(refetchInterval !== undefined && { refetchInterval }),\n ...(retry !== undefined && { retry }),\n ...(retryDelay !== undefined && { retryDelay }),\n ...(staleTime !== undefined && { staleTime }),\n }\n\n const localObserver = new QueryObserver<\n Array<any>,\n any,\n Array<any>,\n Array<any>,\n any\n >(queryClient, observerOptions)\n\n hashToQueryKey.set(hashedQueryKey, key)\n state.observers.set(hashedQueryKey, localObserver)\n\n // Create a promise that resolves when the query result is first available\n const readyPromise = new Promise<void>((resolve, reject) => {\n const unsubscribe = localObserver.subscribe((result) => {\n if (result.isSuccess) {\n unsubscribe()\n resolve()\n } else if (result.isError) {\n unsubscribe()\n reject(result.error)\n }\n })\n })\n\n // If sync has started or there are subscribers to the collection, subscribe to the query straight away\n // This creates the main subscription that handles data updates\n if (syncStarted || collection.subscriberCount > 0) {\n subscribeToQuery(localObserver, hashedQueryKey)\n }\n\n // Tell tanstack query to GC the query when the subscription is unsubscribed\n // The subscription is unsubscribed when the live query is GCed.\n const subscription = opts.subscription\n subscription?.once(`unsubscribed`, () => {\n queryClient.removeQueries({ queryKey: key, exact: true })\n })\n\n return readyPromise\n }\n\n type UpdateHandler = Parameters<QueryObserver[`subscribe`]>[0]\n\n const makeQueryResultHandler = (queryKey: QueryKey) => {\n const hashedQueryKey = hashKey(queryKey)\n const handleQueryResult: UpdateHandler = (result) => {\n if (result.isSuccess) {\n // Clear error state\n state.lastError = undefined\n state.errorCount = 0\n\n const rawData = result.data\n const newItemsArray = select ? select(rawData) : rawData\n\n if (\n !Array.isArray(newItemsArray) ||\n newItemsArray.some((item) => typeof item !== `object`)\n ) {\n const errorMessage = select\n ? `@tanstack/query-db-collection: select() must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`\n : `@tanstack/query-db-collection: queryFn must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`\n\n console.error(errorMessage)\n return\n }\n\n const currentSyncedItems: Map<string | number, any> = new Map(\n collection._state.syncedData.entries()\n )\n const newItemsMap = new Map<string | number, any>()\n newItemsArray.forEach((item) => {\n const key = getKey(item)\n newItemsMap.set(key, item)\n })\n\n begin()\n\n // Helper function for shallow equality check of objects\n const shallowEqual = (\n obj1: Record<string, any>,\n obj2: Record<string, any>\n ): boolean => {\n // Get all keys from both objects\n const keys1 = Object.keys(obj1)\n const keys2 = Object.keys(obj2)\n\n // If number of keys is different, objects are not equal\n if (keys1.length !== keys2.length) return false\n\n // Check if all keys in obj1 have the same values in obj2\n return keys1.every((key) => {\n // Skip comparing functions and complex objects deeply\n if (typeof obj1[key] === `function`) return true\n return obj1[key] === obj2[key]\n })\n }\n\n currentSyncedItems.forEach((oldItem, key) => {\n const newItem = newItemsMap.get(key)\n if (!newItem) {\n const needToRemove = removeRow(key, hashedQueryKey) // returns true if the row is no longer referenced by any queries\n if (needToRemove) {\n write({ type: `delete`, value: oldItem })\n }\n } else if (\n !shallowEqual(\n oldItem as Record<string, any>,\n newItem as Record<string, any>\n )\n ) {\n // Only update if there are actual differences in the properties\n write({ type: `update`, value: newItem })\n }\n })\n\n newItemsMap.forEach((newItem, key) => {\n addRow(key, hashedQueryKey)\n if (!currentSyncedItems.has(key)) {\n write({ type: `insert`, value: newItem })\n }\n })\n\n commit()\n\n // Mark collection as ready after first successful query result\n markReady()\n } else if (result.isError) {\n if (result.errorUpdatedAt !== state.lastErrorUpdatedAt) {\n state.lastError = result.error\n state.errorCount++\n state.lastErrorUpdatedAt = result.errorUpdatedAt\n }\n\n console.error(\n `[QueryCollection] Error observing query ${String(queryKey)}:`,\n result.error\n )\n\n // Mark collection as ready even on error to avoid blocking apps\n markReady()\n }\n }\n return handleQueryResult\n }\n\n // This function is called when a loadSubset call is deduplicated\n // meaning that we have all the data locally available to answer the query\n // so we execute the query locally\n const createLocalQuery = (opts: LoadSubsetOptions) => {\n const queryFn = ({ meta }: QueryFunctionContext<any>) => {\n const inserts = collection.currentStateAsChanges(\n meta!.loadSubsetOptions as LoadSubsetOptions\n )!\n const data = inserts.map(({ value }) => value)\n return Promise.resolve(data)\n }\n\n createQueryFromOpts(opts, queryFn)\n }\n\n const isSubscribed = (hashedQueryKey: string) => {\n return unsubscribes.has(hashedQueryKey)\n }\n\n const subscribeToQuery = (\n observer: QueryObserver<Array<any>, any, Array<any>, Array<any>, any>,\n hashedQueryKey: string\n ) => {\n if (!isSubscribed(hashedQueryKey)) {\n const queryKey = hashToQueryKey.get(hashedQueryKey)!\n const handleQueryResult = makeQueryResultHandler(queryKey)\n const unsubscribeFn = observer.subscribe(handleQueryResult)\n unsubscribes.set(hashedQueryKey, unsubscribeFn)\n }\n }\n\n const subscribeToQueries = () => {\n state.observers.forEach(subscribeToQuery)\n }\n\n const unsubscribeFromQueries = () => {\n unsubscribes.forEach((unsubscribeFn) => {\n unsubscribeFn()\n })\n unsubscribes.clear()\n }\n\n // Mark that sync has started\n syncStarted = true\n\n // Set up event listener for subscriber changes\n const unsubscribeFromCollectionEvents = collection.on(\n `subscribers:change`,\n ({ subscriberCount }) => {\n if (subscriberCount > 0) {\n subscribeToQueries()\n } else if (subscriberCount === 0) {\n unsubscribeFromQueries()\n }\n }\n )\n\n // If syncMode is eager, create the initial query without any predicates\n if (syncMode === `eager`) {\n // Catch any errors to prevent unhandled rejections\n const initialResult = createQueryFromOpts({})\n if (initialResult instanceof Promise) {\n initialResult.catch(() => {\n // Errors are already handled by the query result handler\n })\n }\n } else {\n // In on-demand mode, mark ready immediately since there's no initial query\n markReady()\n }\n\n // Always subscribe when sync starts (this could be from preload(), startSync config, or first subscriber)\n // We'll dynamically unsubscribe/resubscribe based on subscriber count to maintain staleTime behavior\n subscribeToQueries()\n\n // Ensure we process any existing query data (QueryObserver doesn't invoke its callback automatically with initial state)\n state.observers.forEach((observer, hashedQueryKey) => {\n const queryKey = hashToQueryKey.get(hashedQueryKey)!\n const handleQueryResult = makeQueryResultHandler(queryKey)\n handleQueryResult(observer.getCurrentResult())\n })\n\n // Subscribe to the query client's cache to handle queries that are GCed by tanstack query\n const unsubscribeQueryCache = queryClient\n .getQueryCache()\n .subscribe((event) => {\n const hashedKey = event.query.queryHash\n if (event.type === `removed`) {\n cleanupQuery(hashedKey)\n }\n })\n\n function cleanupQuery(hashedQueryKey: string) {\n // Unsubscribe from the query's observer\n unsubscribes.get(hashedQueryKey)?.()\n\n // Get all the rows that are in the result of this query\n const rowKeys = queryToRows.get(hashedQueryKey) ?? new Set()\n\n // Remove the query from these rows\n rowKeys.forEach((rowKey) => {\n const queries = rowToQueries.get(rowKey) // set of queries that reference this row\n if (queries && queries.size > 0) {\n queries.delete(hashedQueryKey)\n if (queries.size === 0) {\n // Reference count dropped to 0, we can GC the row\n rowToQueries.delete(rowKey)\n\n if (collection.has(rowKey)) {\n begin()\n write({ type: `delete`, value: collection.get(rowKey) })\n commit()\n }\n }\n }\n })\n\n // Remove the query from the internal state\n unsubscribes.delete(hashedQueryKey)\n state.observers.delete(hashedQueryKey)\n queryToRows.delete(hashedQueryKey)\n hashToQueryKey.delete(hashedQueryKey)\n }\n\n const cleanup = async () => {\n unsubscribeFromCollectionEvents()\n unsubscribeFromQueries()\n\n const queryKeys = [...hashToQueryKey.values()]\n\n hashToQueryKey.clear()\n queryToRows.clear()\n rowToQueries.clear()\n state.observers.clear()\n unsubscribeQueryCache()\n\n await Promise.all(\n queryKeys.map(async (queryKey) => {\n await queryClient.cancelQueries({ queryKey })\n queryClient.removeQueries({ queryKey })\n })\n )\n }\n\n // Create deduplicated loadSubset wrapper for non-eager modes\n // This prevents redundant snapshot requests when multiple concurrent\n // live queries request overlapping or subset predicates\n const loadSubsetDedupe =\n syncMode === `eager`\n ? undefined\n : new DeduplicatedLoadSubset({\n loadSubset: createQueryFromOpts,\n onDeduplicate: createLocalQuery,\n })\n\n return {\n loadSubset: loadSubsetDedupe?.loadSubset,\n cleanup,\n }\n }\n\n /**\n * Refetch the query data\n *\n * Uses queryObserver.refetch() because:\n * - Bypasses `enabled: false` to support manual/imperative refetch patterns (e.g., button-triggered fetch)\n * - Ensures clearError() works even when enabled: false\n * - Always refetches THIS specific collection (exact targeting via observer)\n * - Respects retry, retryDelay, and other observer options\n *\n * This matches TanStack Query's hook behavior where refetch() bypasses enabled: false.\n * See: https://tanstack.com/query/latest/docs/framework/react/guides/disabling-queries\n *\n * Used by both:\n * - utils.refetch() - for explicit user-triggered refetches\n * - Internal handlers (onInsert/onUpdate/onDelete) - after mutations to get fresh data\n *\n * @returns Promise that resolves when the refetch is complete, with QueryObserverResult\n */\n const refetch: RefetchFn = async (opts) => {\n const queryKeys = [...hashToQueryKey.values()]\n const refetchPromises = queryKeys.map((queryKey) => {\n const queryObserver = state.observers.get(hashKey(queryKey))!\n return queryObserver.refetch({\n throwOnError: opts?.throwOnError,\n })\n })\n\n await Promise.all(refetchPromises)\n }\n\n // Create write context for manual write operations\n let writeContext: {\n collection: any\n queryClient: QueryClient\n queryKey: Array<unknown>\n getKey: (item: any) => string | number\n begin: () => void\n write: (message: Omit<ChangeMessage<any>, `key`>) => void\n commit: () => void\n } | null = null\n\n // Enhanced internalSync that captures write functions for manual use\n const enhancedInternalSync: SyncConfig<any>[`sync`] = (params) => {\n const { begin, write, commit, collection } = params\n\n // Store references for manual write operations\n writeContext = {\n collection,\n queryClient,\n queryKey: queryKey as unknown as Array<unknown>,\n getKey: getKey as (item: any) => string | number,\n begin,\n write,\n commit,\n }\n\n // Call the original internalSync logic\n return internalSync(params)\n }\n\n // Create write utils using the manual-sync module\n const writeUtils = createWriteUtils<any, string | number, any>(\n () => writeContext\n )\n\n // Create wrapper handlers for direct persistence operations that handle refetching\n const wrappedOnInsert = onInsert\n ? async (params: InsertMutationFnParams<any>) => {\n const handlerResult = (await onInsert(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnUpdate = onUpdate\n ? async (params: UpdateMutationFnParams<any>) => {\n const handlerResult = (await onUpdate(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnDelete = onDelete\n ? async (params: DeleteMutationFnParams<any>) => {\n const handlerResult = (await onDelete(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n // Create utils instance with state and dependencies passed explicitly\n const utils: any = new QueryCollectionUtilsImpl(state, refetch, writeUtils)\n\n return {\n ...baseCollectionConfig,\n getKey,\n syncMode,\n sync: { sync: enhancedInternalSync },\n onInsert: wrappedOnInsert,\n onUpdate: wrappedOnUpdate,\n onDelete: wrappedOnDelete,\n utils,\n }\n}\n"],"names":["QueryKeyRequiredError","QueryFnRequiredError","QueryClientRequiredError","GetKeyRequiredError","hashKey","QueryObserver","queryKey","queryFn","meta","DeduplicatedLoadSubset","createWriteUtils"],"mappings":";;;;;;AA0NA,MAAM,yBAAyB;AAAA,EAY7B,YACE,OACA,SACA,YACA;AACA,SAAK,QAAQ;AACb,SAAK,YAAY;AAGjB,SAAK,UAAU;AACf,SAAK,cAAc,WAAW;AAC9B,SAAK,cAAc,WAAW;AAC9B,SAAK,cAAc,WAAW;AAC9B,SAAK,cAAc,WAAW;AAC9B,SAAK,aAAa,WAAW;AAAA,EAC/B;AAAA,EAEA,MAAa,aAAa;AACxB,SAAK,MAAM,YAAY;AACvB,SAAK,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;AAEJ,QAAM,kBAAkB,WACpB,OAAO,WAAwC;AAC7C,UAAM,gBAAiB,MAAM,SAAS,MAAM,KAAM,CAAA;AAClD,UAAM,gBACH,cAAwC,YAAY;AAEvD,QAAI,eAAe;AACjB,YAAM,QAAA;AAAA,IACR;AAEA,WAAO;AAAA,EACT,IACA;AAGJ,QAAM,QAAa,IAAI,yBAAyB,OAAO,SAAS,UAAU;AAE1E,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA,MAAM,EAAE,MAAM,qBAAA;AAAA,IACd,UAAU;AAAA,IACV,UAAU;AAAA,IACV,UAAU;AAAA,IACV;AAAA,EAAA;AAEJ;;"}
|
|
1
|
+
{"version":3,"file":"query.cjs","sources":["../../src/query.ts"],"sourcesContent":["import { QueryObserver, hashKey } from \"@tanstack/query-core\"\nimport {\n GetKeyRequiredError,\n QueryClientRequiredError,\n QueryFnRequiredError,\n QueryKeyRequiredError,\n} from \"./errors\"\nimport { createWriteUtils } from \"./manual-sync\"\nimport { serializeLoadSubsetOptions } from \"./serialization\"\nimport type {\n BaseCollectionConfig,\n ChangeMessage,\n CollectionConfig,\n DeleteMutationFnParams,\n InsertMutationFnParams,\n LoadSubsetOptions,\n SyncConfig,\n UpdateMutationFnParams,\n UtilsRecord,\n} from \"@tanstack/db\"\nimport type {\n FetchStatus,\n QueryClient,\n QueryFunctionContext,\n QueryKey,\n QueryObserverOptions,\n QueryObserverResult,\n} from \"@tanstack/query-core\"\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\"\n\n// Re-export for external use\nexport type { SyncOperation } from \"./manual-sync\"\n\n// Schema output type inference helper (matches electric.ts pattern)\ntype InferSchemaOutput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferOutput<T> extends object\n ? StandardSchemaV1.InferOutput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\n// Schema input type inference helper (matches electric.ts pattern)\ntype InferSchemaInput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferInput<T> extends object\n ? StandardSchemaV1.InferInput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\ntype TQueryKeyBuilder<TQueryKey> = (opts: LoadSubsetOptions) => TQueryKey\n\n/**\n * Configuration options for creating a Query Collection\n * @template T - The explicit type of items stored in the collection\n * @template TQueryFn - The queryFn type\n * @template TError - The type of errors that can occur during queries\n * @template TQueryKey - The type of the query key\n * @template TKey - The type of the item keys\n * @template TSchema - The schema type for validation\n */\nexport interface QueryCollectionConfig<\n T extends object = object,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any> = (\n context: QueryFunctionContext<any>\n ) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TSchema extends StandardSchemaV1 = never,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n> extends BaseCollectionConfig<T, TKey, TSchema> {\n /** The query key used by TanStack Query to identify this query */\n queryKey: TQueryKey | TQueryKeyBuilder<TQueryKey>\n /** Function that fetches data from the server. Must return the complete collection state */\n queryFn: TQueryFn extends (\n context: QueryFunctionContext<TQueryKey>\n ) => Promise<Array<any>>\n ? (context: QueryFunctionContext<TQueryKey>) => Promise<Array<T>>\n : TQueryFn\n /* Function that extracts array items from wrapped API responses (e.g metadata, pagination) */\n select?: (data: TQueryData) => Array<T>\n /** The TanStack Query client instance */\n queryClient: QueryClient\n\n // Query-specific options\n /** Whether the query should automatically run (default: true) */\n enabled?: boolean\n refetchInterval?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`refetchInterval`]\n retry?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`retry`]\n retryDelay?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`retryDelay`]\n staleTime?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`staleTime`]\n\n /**\n * Metadata to pass to the query.\n * Available in queryFn via context.meta\n *\n * @example\n * // Using meta for error context\n * queryFn: async (context) => {\n * try {\n * return await api.getTodos(userId)\n * } catch (error) {\n * // Use meta for better error messages\n * throw new Error(\n * context.meta?.errorMessage || 'Failed to load todos'\n * )\n * }\n * },\n * meta: {\n * errorMessage: `Failed to load todos for user ${userId}`\n * }\n */\n meta?: Record<string, unknown>\n}\n\n/**\n * Type for the refetch utility function\n * Returns the QueryObserverResult from TanStack Query\n */\nexport type RefetchFn = (opts?: {\n throwOnError?: boolean\n}) => Promise<QueryObserverResult<any, any> | void>\n\n/**\n * Utility methods available on Query Collections for direct writes and manual operations.\n * Direct writes bypass the normal query/mutation flow and write directly to the synced data store.\n * @template TItem - The type of items stored in the collection\n * @template TKey - The type of the item keys\n * @template TInsertInput - The type accepted for insert operations\n * @template TError - The type of errors that can occur during queries\n */\nexport interface QueryCollectionUtils<\n TItem extends object = Record<string, unknown>,\n TKey extends string | number = string | number,\n TInsertInput extends object = TItem,\n TError = unknown,\n> extends UtilsRecord {\n /** Manually trigger a refetch of the query */\n refetch: RefetchFn\n /** Insert one or more items directly into the synced data store without triggering a query refetch or optimistic update */\n writeInsert: (data: TInsertInput | Array<TInsertInput>) => void\n /** Update one or more items directly in the synced data store without triggering a query refetch or optimistic update */\n writeUpdate: (updates: Partial<TItem> | Array<Partial<TItem>>) => void\n /** Delete one or more items directly from the synced data store without triggering a query refetch or optimistic update */\n writeDelete: (keys: TKey | Array<TKey>) => void\n /** Insert or update one or more items directly in the synced data store without triggering a query refetch or optimistic update */\n writeUpsert: (data: Partial<TItem> | Array<Partial<TItem>>) => void\n /** Execute multiple write operations as a single atomic batch to the synced data store */\n writeBatch: (callback: () => void) => void\n\n // Query Observer State (getters)\n /** Get the last error encountered by the query (if any); reset on success */\n lastError: TError | undefined\n /** Check if the collection is in an error state */\n isError: boolean\n /**\n * Get the number of consecutive sync failures.\n * Incremented only when query fails completely (not per retry attempt); reset on success.\n */\n errorCount: number\n /** Check if query is currently fetching (initial or background) */\n isFetching: boolean\n /** Check if query is refetching in background (not initial fetch) */\n isRefetching: boolean\n /** Check if query is loading for the first time (no data yet) */\n isLoading: boolean\n /** Get timestamp of last successful data update (in milliseconds) */\n dataUpdatedAt: number\n /** Get current fetch status */\n fetchStatus: `fetching` | `paused` | `idle`\n\n /**\n * Clear the error state and trigger a refetch of the query\n * @returns Promise that resolves when the refetch completes successfully\n * @throws Error if the refetch fails\n */\n clearError: () => Promise<void>\n}\n\n/**\n * Internal state object for tracking query observer and errors\n */\ninterface QueryCollectionState {\n lastError: any\n errorCount: number\n lastErrorUpdatedAt: number\n observers: Map<\n string,\n QueryObserver<Array<any>, any, Array<any>, Array<any>, any>\n >\n}\n\n/**\n * Implementation class for QueryCollectionUtils with explicit dependency injection\n * for better testability and architectural clarity\n */\nclass QueryCollectionUtilsImpl {\n private state: QueryCollectionState\n private refetchFn: RefetchFn\n\n // Write methods\n public refetch: RefetchFn\n public writeInsert: any\n public writeUpdate: any\n public writeDelete: any\n public writeUpsert: any\n public writeBatch: any\n\n constructor(\n state: QueryCollectionState,\n refetch: RefetchFn,\n writeUtils: ReturnType<typeof createWriteUtils>\n ) {\n this.state = state\n this.refetchFn = refetch\n\n // Initialize methods to use passed dependencies\n this.refetch = refetch\n this.writeInsert = writeUtils.writeInsert\n this.writeUpdate = writeUtils.writeUpdate\n this.writeDelete = writeUtils.writeDelete\n this.writeUpsert = writeUtils.writeUpsert\n this.writeBatch = writeUtils.writeBatch\n }\n\n public async clearError() {\n this.state.lastError = undefined\n this.state.errorCount = 0\n this.state.lastErrorUpdatedAt = 0\n await this.refetchFn({ throwOnError: true })\n }\n\n // Getters for error state\n public get lastError() {\n return this.state.lastError\n }\n\n public get isError() {\n return !!this.state.lastError\n }\n\n public get errorCount() {\n return this.state.errorCount\n }\n\n // Getters for QueryObserver state\n public get isFetching() {\n // check if any observer is fetching\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isFetching\n )\n }\n\n public get isRefetching() {\n // check if any observer is refetching\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isRefetching\n )\n }\n\n public get isLoading() {\n // check if any observer is loading\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isLoading\n )\n }\n\n public get dataUpdatedAt() {\n // compute the max dataUpdatedAt of all observers\n return Math.max(\n 0,\n ...Array.from(this.state.observers.values()).map(\n (observer) => observer.getCurrentResult().dataUpdatedAt\n )\n )\n }\n\n public get fetchStatus(): Array<FetchStatus> {\n return Array.from(this.state.observers.values()).map(\n (observer) => observer.getCurrentResult().fetchStatus\n )\n }\n}\n\n/**\n * Creates query collection options for use with a standard Collection.\n * This integrates TanStack Query with TanStack DB for automatic synchronization.\n *\n * Supports automatic type inference following the priority order:\n * 1. Schema inference (highest priority)\n * 2. QueryFn return type inference (second priority)\n *\n * @template T - Type of the schema if a schema is provided otherwise it is the type of the values returned by the queryFn\n * @template TError - The type of errors that can occur during queries\n * @template TQueryKey - The type of the query key\n * @template TKey - The type of the item keys\n * @param config - Configuration options for the Query collection\n * @returns Collection options with utilities for direct writes and manual operations\n *\n * @example\n * // Type inferred from queryFn return type (NEW!)\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => {\n * const response = await fetch('/api/todos')\n * return response.json() as Todo[] // Type automatically inferred!\n * },\n * queryClient,\n * getKey: (item) => item.id, // item is typed as Todo\n * })\n * )\n *\n * @example\n * // Explicit type\n * const todosCollection = createCollection<Todo>(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * queryClient,\n * getKey: (item) => item.id,\n * })\n * )\n *\n * @example\n * // Schema inference\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * queryClient,\n * schema: todoSchema, // Type inferred from schema\n * getKey: (item) => item.id,\n * })\n * )\n *\n * @example\n * // With persistence handlers\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: fetchTodos,\n * queryClient,\n * getKey: (item) => item.id,\n * onInsert: async ({ transaction }) => {\n * await api.createTodos(transaction.mutations.map(m => m.modified))\n * },\n * onUpdate: async ({ transaction }) => {\n * await api.updateTodos(transaction.mutations)\n * },\n * onDelete: async ({ transaction }) => {\n * await api.deleteTodos(transaction.mutations.map(m => m.key))\n * }\n * })\n * )\n *\n * @example\n * // The select option extracts the items array from a response with metadata\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * select: (data) => data.items, // Extract the array of items\n * queryClient,\n * schema: todoSchema,\n * getKey: (item) => item.id,\n * })\n * )\n */\n// Overload for when schema is provided and select present\nexport function queryCollectionOptions<\n T extends StandardSchemaV1,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n>(\n config: QueryCollectionConfig<\n InferSchemaOutput<T>,\n TQueryFn,\n TError,\n TQueryKey,\n TKey,\n T\n > & {\n schema: T\n select: (data: TQueryData) => Array<InferSchemaInput<T>>\n }\n): CollectionConfig<\n InferSchemaOutput<T>,\n TKey,\n T,\n QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>\n> & {\n schema: T\n utils: QueryCollectionUtils<\n InferSchemaOutput<T>,\n TKey,\n InferSchemaInput<T>,\n TError\n >\n}\n\n// Overload for when no schema is provided and select present\nexport function queryCollectionOptions<\n T extends object,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any> = (\n context: QueryFunctionContext<any>\n ) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n>(\n config: QueryCollectionConfig<\n T,\n TQueryFn,\n TError,\n TQueryKey,\n TKey,\n never,\n TQueryData\n > & {\n schema?: never // prohibit schema\n select: (data: TQueryData) => Array<T>\n }\n): CollectionConfig<\n T,\n TKey,\n never,\n QueryCollectionUtils<T, TKey, T, TError>\n> & {\n schema?: never // no schema in the result\n utils: QueryCollectionUtils<T, TKey, T, TError>\n}\n\n// Overload for when schema is provided\nexport function queryCollectionOptions<\n T extends StandardSchemaV1,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n>(\n config: QueryCollectionConfig<\n InferSchemaOutput<T>,\n (\n context: QueryFunctionContext<any>\n ) => Promise<Array<InferSchemaOutput<T>>>,\n TError,\n TQueryKey,\n TKey,\n T\n > & {\n schema: T\n }\n): CollectionConfig<\n InferSchemaOutput<T>,\n TKey,\n T,\n QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>\n> & {\n schema: T\n utils: QueryCollectionUtils<\n InferSchemaOutput<T>,\n TKey,\n InferSchemaInput<T>,\n TError\n >\n}\n\n// Overload for when no schema is provided\nexport function queryCollectionOptions<\n T extends object,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n>(\n config: QueryCollectionConfig<\n T,\n (context: QueryFunctionContext<any>) => Promise<Array<T>>,\n TError,\n TQueryKey,\n TKey\n > & {\n schema?: never // prohibit schema\n }\n): CollectionConfig<\n T,\n TKey,\n never,\n QueryCollectionUtils<T, TKey, T, TError>\n> & {\n schema?: never // no schema in the result\n utils: QueryCollectionUtils<T, TKey, T, TError>\n}\n\nexport function queryCollectionOptions(\n config: QueryCollectionConfig<Record<string, unknown>>\n): CollectionConfig<\n Record<string, unknown>,\n string | number,\n never,\n QueryCollectionUtils\n> & {\n utils: QueryCollectionUtils\n} {\n const {\n queryKey,\n queryFn,\n select,\n queryClient,\n enabled,\n refetchInterval,\n retry,\n retryDelay,\n staleTime,\n getKey,\n onInsert,\n onUpdate,\n onDelete,\n meta,\n ...baseCollectionConfig\n } = config\n\n // Default to eager sync mode if not provided\n const syncMode = baseCollectionConfig.syncMode ?? `eager`\n\n // Validate required parameters\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryKey) {\n throw new QueryKeyRequiredError()\n }\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryFn) {\n throw new QueryFnRequiredError()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryClient) {\n throw new QueryClientRequiredError()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!getKey) {\n throw new GetKeyRequiredError()\n }\n\n /** State object to hold error tracking and observer reference */\n const state: QueryCollectionState = {\n lastError: undefined as any,\n errorCount: 0,\n lastErrorUpdatedAt: 0,\n observers: new Map<\n string,\n QueryObserver<Array<any>, any, Array<any>, Array<any>, any>\n >(),\n }\n\n // hashedQueryKey → queryKey\n const hashToQueryKey = new Map<string, QueryKey>()\n\n // queryKey → Set<RowKey>\n const queryToRows = new Map<string, Set<string | number>>()\n\n // RowKey → Set<queryKey>\n const rowToQueries = new Map<string | number, Set<string>>()\n\n // queryKey → QueryObserver's unsubscribe function\n const unsubscribes = new Map<string, () => void>()\n\n // Helper function to add a row to the internal state\n const addRow = (rowKey: string | number, hashedQueryKey: string) => {\n const rowToQueriesSet = rowToQueries.get(rowKey) || new Set()\n rowToQueriesSet.add(hashedQueryKey)\n rowToQueries.set(rowKey, rowToQueriesSet)\n\n const queryToRowsSet = queryToRows.get(hashedQueryKey) || new Set()\n queryToRowsSet.add(rowKey)\n queryToRows.set(hashedQueryKey, queryToRowsSet)\n }\n\n // Helper function to remove a row from the internal state\n const removeRow = (rowKey: string | number, hashedQuerKey: string) => {\n const rowToQueriesSet = rowToQueries.get(rowKey) || new Set()\n rowToQueriesSet.delete(hashedQuerKey)\n rowToQueries.set(rowKey, rowToQueriesSet)\n\n const queryToRowsSet = queryToRows.get(hashedQuerKey) || new Set()\n queryToRowsSet.delete(rowKey)\n queryToRows.set(hashedQuerKey, queryToRowsSet)\n\n return rowToQueriesSet.size === 0\n }\n\n const internalSync: SyncConfig<any>[`sync`] = (params) => {\n const { begin, write, commit, markReady, collection } = params\n\n // Track whether sync has been started\n let syncStarted = false\n\n const createQueryFromOpts = (\n opts: LoadSubsetOptions = {},\n queryFunction: typeof queryFn = queryFn\n ): true | Promise<void> => {\n // Push the predicates down to the queryKey and queryFn\n let key: QueryKey\n if (typeof queryKey === `function`) {\n // Function-based queryKey: use it to build the key from opts\n key = queryKey(opts)\n } else if (syncMode === `on-demand`) {\n // Static queryKey in on-demand mode: automatically append serialized predicates\n // to create separate cache entries for different predicate combinations\n const serialized = serializeLoadSubsetOptions(opts)\n key = serialized !== undefined ? [...queryKey, serialized] : queryKey\n } else {\n // Static queryKey in eager mode: use as-is\n key = queryKey\n }\n const hashedQueryKey = hashKey(key)\n const extendedMeta = { ...meta, loadSubsetOptions: opts }\n\n if (state.observers.has(hashedQueryKey)) {\n // We already have a query for this queryKey\n // Get the current result and return based on its state\n const observer = state.observers.get(hashedQueryKey)!\n const currentResult = observer.getCurrentResult()\n\n if (currentResult.isSuccess) {\n // Data is already available, return true synchronously\n return true\n } else if (currentResult.isError) {\n // Error already occurred, reject immediately\n return Promise.reject(currentResult.error)\n } else {\n // Query is still loading, wait for the first result\n return new Promise<void>((resolve, reject) => {\n const unsubscribe = observer.subscribe((result) => {\n if (result.isSuccess) {\n unsubscribe()\n resolve()\n } else if (result.isError) {\n unsubscribe()\n reject(result.error)\n }\n })\n })\n }\n }\n\n const observerOptions: QueryObserverOptions<\n Array<any>,\n any,\n Array<any>,\n Array<any>,\n any\n > = {\n queryKey: key,\n queryFn: queryFunction,\n meta: extendedMeta,\n structuralSharing: true,\n notifyOnChangeProps: `all`,\n\n // Only include options that are explicitly defined to allow QueryClient defaultOptions to be used\n ...(enabled !== undefined && { enabled }),\n ...(refetchInterval !== undefined && { refetchInterval }),\n ...(retry !== undefined && { retry }),\n ...(retryDelay !== undefined && { retryDelay }),\n ...(staleTime !== undefined && { staleTime }),\n }\n\n const localObserver = new QueryObserver<\n Array<any>,\n any,\n Array<any>,\n Array<any>,\n any\n >(queryClient, observerOptions)\n\n hashToQueryKey.set(hashedQueryKey, key)\n state.observers.set(hashedQueryKey, localObserver)\n\n // Create a promise that resolves when the query result is first available\n const readyPromise = new Promise<void>((resolve, reject) => {\n const unsubscribe = localObserver.subscribe((result) => {\n if (result.isSuccess) {\n unsubscribe()\n resolve()\n } else if (result.isError) {\n unsubscribe()\n reject(result.error)\n }\n })\n })\n\n // If sync has started or there are subscribers to the collection, subscribe to the query straight away\n // This creates the main subscription that handles data updates\n if (syncStarted || collection.subscriberCount > 0) {\n subscribeToQuery(localObserver, hashedQueryKey)\n }\n\n // Tell tanstack query to GC the query when the subscription is unsubscribed\n // The subscription is unsubscribed when the live query is GCed.\n const subscription = opts.subscription\n subscription?.once(`unsubscribed`, () => {\n queryClient.removeQueries({ queryKey: key, exact: true })\n })\n\n return readyPromise\n }\n\n type UpdateHandler = Parameters<QueryObserver[`subscribe`]>[0]\n\n const makeQueryResultHandler = (queryKey: QueryKey) => {\n const hashedQueryKey = hashKey(queryKey)\n const handleQueryResult: UpdateHandler = (result) => {\n if (result.isSuccess) {\n // Clear error state\n state.lastError = undefined\n state.errorCount = 0\n\n const rawData = result.data\n const newItemsArray = select ? select(rawData) : rawData\n\n if (\n !Array.isArray(newItemsArray) ||\n newItemsArray.some((item) => typeof item !== `object`)\n ) {\n const errorMessage = select\n ? `@tanstack/query-db-collection: select() must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`\n : `@tanstack/query-db-collection: queryFn must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`\n\n console.error(errorMessage)\n return\n }\n\n const currentSyncedItems: Map<string | number, any> = new Map(\n collection._state.syncedData.entries()\n )\n const newItemsMap = new Map<string | number, any>()\n newItemsArray.forEach((item) => {\n const key = getKey(item)\n newItemsMap.set(key, item)\n })\n\n begin()\n\n // Helper function for shallow equality check of objects\n const shallowEqual = (\n obj1: Record<string, any>,\n obj2: Record<string, any>\n ): boolean => {\n // Get all keys from both objects\n const keys1 = Object.keys(obj1)\n const keys2 = Object.keys(obj2)\n\n // If number of keys is different, objects are not equal\n if (keys1.length !== keys2.length) return false\n\n // Check if all keys in obj1 have the same values in obj2\n return keys1.every((key) => {\n // Skip comparing functions and complex objects deeply\n if (typeof obj1[key] === `function`) return true\n return obj1[key] === obj2[key]\n })\n }\n\n currentSyncedItems.forEach((oldItem, key) => {\n const newItem = newItemsMap.get(key)\n if (!newItem) {\n const needToRemove = removeRow(key, hashedQueryKey) // returns true if the row is no longer referenced by any queries\n if (needToRemove) {\n write({ type: `delete`, value: oldItem })\n }\n } else if (\n !shallowEqual(\n oldItem as Record<string, any>,\n newItem as Record<string, any>\n )\n ) {\n // Only update if there are actual differences in the properties\n write({ type: `update`, value: newItem })\n }\n })\n\n newItemsMap.forEach((newItem, key) => {\n addRow(key, hashedQueryKey)\n if (!currentSyncedItems.has(key)) {\n write({ type: `insert`, value: newItem })\n }\n })\n\n commit()\n\n // Mark collection as ready after first successful query result\n markReady()\n } else if (result.isError) {\n if (result.errorUpdatedAt !== state.lastErrorUpdatedAt) {\n state.lastError = result.error\n state.errorCount++\n state.lastErrorUpdatedAt = result.errorUpdatedAt\n }\n\n console.error(\n `[QueryCollection] Error observing query ${String(queryKey)}:`,\n result.error\n )\n\n // Mark collection as ready even on error to avoid blocking apps\n markReady()\n }\n }\n return handleQueryResult\n }\n\n const isSubscribed = (hashedQueryKey: string) => {\n return unsubscribes.has(hashedQueryKey)\n }\n\n const subscribeToQuery = (\n observer: QueryObserver<Array<any>, any, Array<any>, Array<any>, any>,\n hashedQueryKey: string\n ) => {\n if (!isSubscribed(hashedQueryKey)) {\n const queryKey = hashToQueryKey.get(hashedQueryKey)!\n const handleQueryResult = makeQueryResultHandler(queryKey)\n const unsubscribeFn = observer.subscribe(handleQueryResult)\n unsubscribes.set(hashedQueryKey, unsubscribeFn)\n }\n }\n\n const subscribeToQueries = () => {\n state.observers.forEach(subscribeToQuery)\n }\n\n const unsubscribeFromQueries = () => {\n unsubscribes.forEach((unsubscribeFn) => {\n unsubscribeFn()\n })\n unsubscribes.clear()\n }\n\n // Mark that sync has started\n syncStarted = true\n\n // Set up event listener for subscriber changes\n const unsubscribeFromCollectionEvents = collection.on(\n `subscribers:change`,\n ({ subscriberCount }) => {\n if (subscriberCount > 0) {\n subscribeToQueries()\n } else if (subscriberCount === 0) {\n unsubscribeFromQueries()\n }\n }\n )\n\n // If syncMode is eager, create the initial query without any predicates\n if (syncMode === `eager`) {\n // Catch any errors to prevent unhandled rejections\n const initialResult = createQueryFromOpts({})\n if (initialResult instanceof Promise) {\n initialResult.catch(() => {\n // Errors are already handled by the query result handler\n })\n }\n } else {\n // In on-demand mode, mark ready immediately since there's no initial query\n markReady()\n }\n\n // Always subscribe when sync starts (this could be from preload(), startSync config, or first subscriber)\n // We'll dynamically unsubscribe/resubscribe based on subscriber count to maintain staleTime behavior\n subscribeToQueries()\n\n // Ensure we process any existing query data (QueryObserver doesn't invoke its callback automatically with initial state)\n state.observers.forEach((observer, hashedQueryKey) => {\n const queryKey = hashToQueryKey.get(hashedQueryKey)!\n const handleQueryResult = makeQueryResultHandler(queryKey)\n handleQueryResult(observer.getCurrentResult())\n })\n\n // Subscribe to the query client's cache to handle queries that are GCed by tanstack query\n const unsubscribeQueryCache = queryClient\n .getQueryCache()\n .subscribe((event) => {\n const hashedKey = event.query.queryHash\n if (event.type === `removed`) {\n cleanupQuery(hashedKey)\n }\n })\n\n function cleanupQuery(hashedQueryKey: string) {\n // Unsubscribe from the query's observer\n unsubscribes.get(hashedQueryKey)?.()\n\n // Get all the rows that are in the result of this query\n const rowKeys = queryToRows.get(hashedQueryKey) ?? new Set()\n\n // Remove the query from these rows\n rowKeys.forEach((rowKey) => {\n const queries = rowToQueries.get(rowKey) // set of queries that reference this row\n if (queries && queries.size > 0) {\n queries.delete(hashedQueryKey)\n if (queries.size === 0) {\n // Reference count dropped to 0, we can GC the row\n rowToQueries.delete(rowKey)\n\n if (collection.has(rowKey)) {\n begin()\n write({ type: `delete`, value: collection.get(rowKey) })\n commit()\n }\n }\n }\n })\n\n // Remove the query from the internal state\n unsubscribes.delete(hashedQueryKey)\n state.observers.delete(hashedQueryKey)\n queryToRows.delete(hashedQueryKey)\n hashToQueryKey.delete(hashedQueryKey)\n }\n\n const cleanup = async () => {\n unsubscribeFromCollectionEvents()\n unsubscribeFromQueries()\n\n const queryKeys = [...hashToQueryKey.values()]\n\n hashToQueryKey.clear()\n queryToRows.clear()\n rowToQueries.clear()\n state.observers.clear()\n unsubscribeQueryCache()\n\n await Promise.all(\n queryKeys.map(async (queryKey) => {\n await queryClient.cancelQueries({ queryKey })\n queryClient.removeQueries({ queryKey })\n })\n )\n }\n\n // Create deduplicated loadSubset wrapper for non-eager modes\n // This prevents redundant snapshot requests when multiple concurrent\n // live queries request overlapping or subset predicates\n const loadSubsetDedupe =\n syncMode === `eager` ? undefined : createQueryFromOpts\n\n return {\n loadSubset: loadSubsetDedupe,\n cleanup,\n }\n }\n\n /**\n * Refetch the query data\n *\n * Uses queryObserver.refetch() because:\n * - Bypasses `enabled: false` to support manual/imperative refetch patterns (e.g., button-triggered fetch)\n * - Ensures clearError() works even when enabled: false\n * - Always refetches THIS specific collection (exact targeting via observer)\n * - Respects retry, retryDelay, and other observer options\n *\n * This matches TanStack Query's hook behavior where refetch() bypasses enabled: false.\n * See: https://tanstack.com/query/latest/docs/framework/react/guides/disabling-queries\n *\n * Used by both:\n * - utils.refetch() - for explicit user-triggered refetches\n * - Internal handlers (onInsert/onUpdate/onDelete) - after mutations to get fresh data\n *\n * @returns Promise that resolves when the refetch is complete, with QueryObserverResult\n */\n const refetch: RefetchFn = async (opts) => {\n const queryKeys = [...hashToQueryKey.values()]\n const refetchPromises = queryKeys.map((queryKey) => {\n const queryObserver = state.observers.get(hashKey(queryKey))!\n return queryObserver.refetch({\n throwOnError: opts?.throwOnError,\n })\n })\n\n await Promise.all(refetchPromises)\n }\n\n // Create write context for manual write operations\n let writeContext: {\n collection: any\n queryClient: QueryClient\n queryKey: Array<unknown>\n getKey: (item: any) => string | number\n begin: () => void\n write: (message: Omit<ChangeMessage<any>, `key`>) => void\n commit: () => void\n } | null = null\n\n // Enhanced internalSync that captures write functions for manual use\n const enhancedInternalSync: SyncConfig<any>[`sync`] = (params) => {\n const { begin, write, commit, collection } = params\n\n // Store references for manual write operations\n writeContext = {\n collection,\n queryClient,\n queryKey: queryKey as unknown as Array<unknown>,\n getKey: getKey as (item: any) => string | number,\n begin,\n write,\n commit,\n }\n\n // Call the original internalSync logic\n return internalSync(params)\n }\n\n // Create write utils using the manual-sync module\n const writeUtils = createWriteUtils<any, string | number, any>(\n () => writeContext\n )\n\n // Create wrapper handlers for direct persistence operations that handle refetching\n const wrappedOnInsert = onInsert\n ? async (params: InsertMutationFnParams<any>) => {\n const handlerResult = (await onInsert(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnUpdate = onUpdate\n ? async (params: UpdateMutationFnParams<any>) => {\n const handlerResult = (await onUpdate(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnDelete = onDelete\n ? async (params: DeleteMutationFnParams<any>) => {\n const handlerResult = (await onDelete(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n // Create utils instance with state and dependencies passed explicitly\n const utils: any = new QueryCollectionUtilsImpl(state, refetch, writeUtils)\n\n return {\n ...baseCollectionConfig,\n getKey,\n syncMode,\n sync: { sync: enhancedInternalSync },\n onInsert: wrappedOnInsert,\n onUpdate: wrappedOnUpdate,\n onDelete: wrappedOnDelete,\n utils,\n }\n}\n"],"names":["QueryKeyRequiredError","QueryFnRequiredError","QueryClientRequiredError","GetKeyRequiredError","serializeLoadSubsetOptions","hashKey","QueryObserver","queryKey","createWriteUtils"],"mappings":";;;;;;AA0NA,MAAM,yBAAyB;AAAA,EAY7B,YACE,OACA,SACA,YACA;AACA,SAAK,QAAQ;AACb,SAAK,YAAY;AAGjB,SAAK,UAAU;AACf,SAAK,cAAc,WAAW;AAC9B,SAAK,cAAc,WAAW;AAC9B,SAAK,cAAc,WAAW;AAC9B,SAAK,cAAc,WAAW;AAC9B,SAAK,aAAa,WAAW;AAAA,EAC/B;AAAA,EAEA,MAAa,aAAa;AACxB,SAAK,MAAM,YAAY;AACvB,SAAK,MAAM,aAAa;AACxB,SAAK,MAAM,qBAAqB;AAChC,UAAM,KAAK,UAAU,EAAE,cAAc,MAAM;AAAA,EAC7C;AAAA;AAAA,EAGA,IAAW,YAAY;AACrB,WAAO,KAAK,MAAM;AAAA,EACpB;AAAA,EAEA,IAAW,UAAU;AACnB,WAAO,CAAC,CAAC,KAAK,MAAM;AAAA,EACtB;AAAA,EAEA,IAAW,aAAa;AACtB,WAAO,KAAK,MAAM;AAAA,EACpB;AAAA;AAAA,EAGA,IAAW,aAAa;AAEtB,WAAO,MAAM,KAAK,KAAK,MAAM,UAAU,OAAA,CAAQ,EAAE;AAAA,MAC/C,CAAC,aAAa,SAAS,mBAAmB;AAAA,IAAA;AAAA,EAE9C;AAAA,EAEA,IAAW,eAAe;AAExB,WAAO,MAAM,KAAK,KAAK,MAAM,UAAU,OAAA,CAAQ,EAAE;AAAA,MAC/C,CAAC,aAAa,SAAS,mBAAmB;AAAA,IAAA;AAAA,EAE9C;AAAA,EAEA,IAAW,YAAY;AAErB,WAAO,MAAM,KAAK,KAAK,MAAM,UAAU,OAAA,CAAQ,EAAE;AAAA,MAC/C,CAAC,aAAa,SAAS,mBAAmB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AS,MAAM,KAAM,CAAA;AAClD,UAAM,gBACH,cAAwC,YAAY;AAEvD,QAAI,eAAe;AACjB,YAAM,QAAA;AAAA,IACR;AAEA,WAAO;AAAA,EACT,IACA;AAEJ,QAAM,kBAAkB,WACpB,OAAO,WAAwC;AAC7C,UAAM,gBAAiB,MAAM,SAAS,MAAM,KAAM,CAAA;AAClD,UAAM,gBACH,cAAwC,YAAY;AAEvD,QAAI,eAAe;AACjB,YAAM,QAAA;AAAA,IACR;AAEA,WAAO;AAAA,EACT,IACA;AAEJ,QAAM,kBAAkB,WACpB,OAAO,WAAwC;AAC7C,UAAM,gBAAiB,MAAM,SAAS,MAAM,KAAM,CAAA;AAClD,UAAM,gBACH,cAAwC,YAAY;AAEvD,QAAI,eAAe;AACjB,YAAM,QAAA;AAAA,IACR;AAEA,WAAO;AAAA,EACT,IACA;AAGJ,QAAM,QAAa,IAAI,yBAAyB,OAAO,SAAS,UAAU;AAE1E,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA,MAAM,EAAE,MAAM,qBAAA;AAAA,IACd,UAAU;AAAA,IACV,UAAU;AAAA,IACV,UAAU;AAAA,IACV;AAAA,EAAA;AAEJ;;"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
function serializeLoadSubsetOptions(options) {
|
|
4
|
+
if (!options) {
|
|
5
|
+
return void 0;
|
|
6
|
+
}
|
|
7
|
+
const result = {};
|
|
8
|
+
if (options.where) {
|
|
9
|
+
result.where = serializeExpression(options.where);
|
|
10
|
+
}
|
|
11
|
+
if (options.orderBy?.length) {
|
|
12
|
+
result.orderBy = options.orderBy.map((clause) => {
|
|
13
|
+
const baseOrderBy = {
|
|
14
|
+
expression: serializeExpression(clause.expression),
|
|
15
|
+
direction: clause.compareOptions.direction,
|
|
16
|
+
nulls: clause.compareOptions.nulls,
|
|
17
|
+
stringSort: clause.compareOptions.stringSort
|
|
18
|
+
};
|
|
19
|
+
if (clause.compareOptions.stringSort === `locale`) {
|
|
20
|
+
return {
|
|
21
|
+
...baseOrderBy,
|
|
22
|
+
locale: clause.compareOptions.locale,
|
|
23
|
+
localeOptions: clause.compareOptions.localeOptions
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return baseOrderBy;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (options.limit !== void 0) {
|
|
30
|
+
result.limit = options.limit;
|
|
31
|
+
}
|
|
32
|
+
return Object.keys(result).length === 0 ? void 0 : JSON.stringify(result);
|
|
33
|
+
}
|
|
34
|
+
function serializeExpression(expr) {
|
|
35
|
+
if (!expr) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
switch (expr.type) {
|
|
39
|
+
case `val`:
|
|
40
|
+
return {
|
|
41
|
+
type: `val`,
|
|
42
|
+
value: serializeValue(expr.value)
|
|
43
|
+
};
|
|
44
|
+
case `ref`:
|
|
45
|
+
return {
|
|
46
|
+
type: `ref`,
|
|
47
|
+
path: [...expr.path]
|
|
48
|
+
};
|
|
49
|
+
case `func`:
|
|
50
|
+
return {
|
|
51
|
+
type: `func`,
|
|
52
|
+
name: expr.name,
|
|
53
|
+
args: expr.args.map((arg) => serializeExpression(arg))
|
|
54
|
+
};
|
|
55
|
+
default:
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function serializeValue(value) {
|
|
60
|
+
if (value === void 0) {
|
|
61
|
+
return { __type: `undefined` };
|
|
62
|
+
}
|
|
63
|
+
if (typeof value === `number`) {
|
|
64
|
+
if (Number.isNaN(value)) {
|
|
65
|
+
return { __type: `nan` };
|
|
66
|
+
}
|
|
67
|
+
if (value === Number.POSITIVE_INFINITY) {
|
|
68
|
+
return { __type: `infinity`, sign: 1 };
|
|
69
|
+
}
|
|
70
|
+
if (value === Number.NEGATIVE_INFINITY) {
|
|
71
|
+
return { __type: `infinity`, sign: -1 };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (value === null || typeof value === `string` || typeof value === `number` || typeof value === `boolean`) {
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
if (value instanceof Date) {
|
|
78
|
+
return { __type: `date`, value: value.toJSON() };
|
|
79
|
+
}
|
|
80
|
+
if (Array.isArray(value)) {
|
|
81
|
+
return value.map((item) => serializeValue(item));
|
|
82
|
+
}
|
|
83
|
+
if (typeof value === `object`) {
|
|
84
|
+
return Object.fromEntries(
|
|
85
|
+
Object.entries(value).map(([key, val]) => [
|
|
86
|
+
key,
|
|
87
|
+
serializeValue(val)
|
|
88
|
+
])
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
exports.serializeLoadSubsetOptions = serializeLoadSubsetOptions;
|
|
94
|
+
//# sourceMappingURL=serialization.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialization.cjs","sources":["../../src/serialization.ts"],"sourcesContent":["import type { IR, LoadSubsetOptions } from \"@tanstack/db\"\n\n/**\n * Serializes LoadSubsetOptions into a stable, hashable format for query keys\n * @internal\n */\nexport function serializeLoadSubsetOptions(\n options: LoadSubsetOptions | undefined\n): string | undefined {\n if (!options) {\n return undefined\n }\n\n const result: Record<string, unknown> = {}\n\n if (options.where) {\n result.where = serializeExpression(options.where)\n }\n\n if (options.orderBy?.length) {\n result.orderBy = options.orderBy.map((clause) => {\n const baseOrderBy = {\n expression: serializeExpression(clause.expression),\n direction: clause.compareOptions.direction,\n nulls: clause.compareOptions.nulls,\n stringSort: clause.compareOptions.stringSort,\n }\n\n // Handle locale-specific options when stringSort is 'locale'\n if (clause.compareOptions.stringSort === `locale`) {\n return {\n ...baseOrderBy,\n locale: clause.compareOptions.locale,\n localeOptions: clause.compareOptions.localeOptions,\n }\n }\n\n return baseOrderBy\n })\n }\n\n if (options.limit !== undefined) {\n result.limit = options.limit\n }\n\n return Object.keys(result).length === 0 ? undefined : JSON.stringify(result)\n}\n\n/**\n * Recursively serializes an IR expression for stable hashing\n * @internal\n */\nfunction serializeExpression(expr: IR.BasicExpression | undefined): unknown {\n if (!expr) {\n return null\n }\n\n switch (expr.type) {\n case `val`:\n return {\n type: `val`,\n value: serializeValue(expr.value),\n }\n case `ref`:\n return {\n type: `ref`,\n path: [...expr.path],\n }\n case `func`:\n return {\n type: `func`,\n name: expr.name,\n args: expr.args.map((arg) => serializeExpression(arg)),\n }\n default:\n return null\n }\n}\n\n/**\n * Serializes special JavaScript values (undefined, NaN, Infinity, Date)\n * @internal\n */\nfunction serializeValue(value: unknown): unknown {\n if (value === undefined) {\n return { __type: `undefined` }\n }\n\n if (typeof value === `number`) {\n if (Number.isNaN(value)) {\n return { __type: `nan` }\n }\n if (value === Number.POSITIVE_INFINITY) {\n return { __type: `infinity`, sign: 1 }\n }\n if (value === Number.NEGATIVE_INFINITY) {\n return { __type: `infinity`, sign: -1 }\n }\n }\n\n if (\n value === null ||\n typeof value === `string` ||\n typeof value === `number` ||\n typeof value === `boolean`\n ) {\n return value\n }\n\n if (value instanceof Date) {\n return { __type: `date`, value: value.toJSON() }\n }\n\n if (Array.isArray(value)) {\n return value.map((item) => serializeValue(item))\n }\n\n if (typeof value === `object`) {\n return Object.fromEntries(\n Object.entries(value as Record<string, unknown>).map(([key, val]) => [\n key,\n serializeValue(val),\n ])\n )\n }\n\n return value\n}\n"],"names":[],"mappings":";;AAMO,SAAS,2BACd,SACoB;AACpB,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,QAAM,SAAkC,CAAA;AAExC,MAAI,QAAQ,OAAO;AACjB,WAAO,QAAQ,oBAAoB,QAAQ,KAAK;AAAA,EAClD;AAEA,MAAI,QAAQ,SAAS,QAAQ;AAC3B,WAAO,UAAU,QAAQ,QAAQ,IAAI,CAAC,WAAW;AAC/C,YAAM,cAAc;AAAA,QAClB,YAAY,oBAAoB,OAAO,UAAU;AAAA,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OAAO,mBAAmB;AACtC,aAAO,EAAE,QAAQ,YAAY,MAAM,GAAA;AAAA,IACrC;AAAA,EACF;AAEA,MACE,UAAU,QACV,OAAO,UAAU,YACjB,OAAO,UAAU,YACjB,OAAO,UAAU,WACjB;AACA,WAAO;AAAA,EACT;AAEA,MAAI,iBAAiB,MAAM;AACzB,WAAO,EAAE,QAAQ,QAAQ,OAAO,MAAM,SAAO;AAAA,EAC/C;AAEA,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,WAAO,MAAM,IAAI,CAAC,SAAS,eAAe,IAAI,CAAC;AAAA,EACjD;AAEA,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO,OAAO;AAAA,MACZ,OAAO,QAAQ,KAAgC,EAAE,IAAI,CAAC,CAAC,KAAK,GAAG,MAAM;AAAA,QACnE;AAAA,QACA,eAAe,GAAG;AAAA,MAAA,CACnB;AAAA,IAAA;AAAA,EAEL;AAEA,SAAO;AACT;;"}
|
package/dist/esm/query.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { hashKey, QueryObserver } from "@tanstack/query-core";
|
|
2
|
-
import { DeduplicatedLoadSubset } from "@tanstack/db";
|
|
3
2
|
import { QueryKeyRequiredError, QueryFnRequiredError, QueryClientRequiredError, GetKeyRequiredError } from "./errors.js";
|
|
4
3
|
import { createWriteUtils } from "./manual-sync.js";
|
|
4
|
+
import { serializeLoadSubsetOptions } from "./serialization.js";
|
|
5
5
|
class QueryCollectionUtilsImpl {
|
|
6
6
|
constructor(state, refetch, writeUtils) {
|
|
7
7
|
this.state = state;
|
|
@@ -121,7 +121,15 @@ function queryCollectionOptions(config) {
|
|
|
121
121
|
const { begin, write, commit, markReady, collection } = params;
|
|
122
122
|
let syncStarted = false;
|
|
123
123
|
const createQueryFromOpts = (opts = {}, queryFunction = queryFn) => {
|
|
124
|
-
|
|
124
|
+
let key;
|
|
125
|
+
if (typeof queryKey === `function`) {
|
|
126
|
+
key = queryKey(opts);
|
|
127
|
+
} else if (syncMode === `on-demand`) {
|
|
128
|
+
const serialized = serializeLoadSubsetOptions(opts);
|
|
129
|
+
key = serialized !== void 0 ? [...queryKey, serialized] : queryKey;
|
|
130
|
+
} else {
|
|
131
|
+
key = queryKey;
|
|
132
|
+
}
|
|
125
133
|
const hashedQueryKey = hashKey(key);
|
|
126
134
|
const extendedMeta = { ...meta, loadSubsetOptions: opts };
|
|
127
135
|
if (state.observers.has(hashedQueryKey)) {
|
|
@@ -249,16 +257,6 @@ function queryCollectionOptions(config) {
|
|
|
249
257
|
};
|
|
250
258
|
return handleQueryResult;
|
|
251
259
|
};
|
|
252
|
-
const createLocalQuery = (opts) => {
|
|
253
|
-
const queryFn2 = ({ meta: meta2 }) => {
|
|
254
|
-
const inserts = collection.currentStateAsChanges(
|
|
255
|
-
meta2.loadSubsetOptions
|
|
256
|
-
);
|
|
257
|
-
const data = inserts.map(({ value }) => value);
|
|
258
|
-
return Promise.resolve(data);
|
|
259
|
-
};
|
|
260
|
-
createQueryFromOpts(opts, queryFn2);
|
|
261
|
-
};
|
|
262
260
|
const isSubscribed = (hashedQueryKey) => {
|
|
263
261
|
return unsubscribes.has(hashedQueryKey);
|
|
264
262
|
};
|
|
@@ -349,12 +347,9 @@ function queryCollectionOptions(config) {
|
|
|
349
347
|
})
|
|
350
348
|
);
|
|
351
349
|
};
|
|
352
|
-
const loadSubsetDedupe = syncMode === `eager` ? void 0 :
|
|
353
|
-
loadSubset: createQueryFromOpts,
|
|
354
|
-
onDeduplicate: createLocalQuery
|
|
355
|
-
});
|
|
350
|
+
const loadSubsetDedupe = syncMode === `eager` ? void 0 : createQueryFromOpts;
|
|
356
351
|
return {
|
|
357
|
-
loadSubset: loadSubsetDedupe
|
|
352
|
+
loadSubset: loadSubsetDedupe,
|
|
358
353
|
cleanup
|
|
359
354
|
};
|
|
360
355
|
};
|
package/dist/esm/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sources":["../../src/query.ts"],"sourcesContent":["import { QueryObserver, hashKey } from \"@tanstack/query-core\"\nimport { DeduplicatedLoadSubset } from \"@tanstack/db\"\nimport {\n GetKeyRequiredError,\n QueryClientRequiredError,\n QueryFnRequiredError,\n QueryKeyRequiredError,\n} from \"./errors\"\nimport { createWriteUtils } from \"./manual-sync\"\nimport type {\n BaseCollectionConfig,\n ChangeMessage,\n CollectionConfig,\n DeleteMutationFnParams,\n InsertMutationFnParams,\n LoadSubsetOptions,\n SyncConfig,\n UpdateMutationFnParams,\n UtilsRecord,\n} from \"@tanstack/db\"\nimport type {\n FetchStatus,\n QueryClient,\n QueryFunctionContext,\n QueryKey,\n QueryObserverOptions,\n QueryObserverResult,\n} from \"@tanstack/query-core\"\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\"\n\n// Re-export for external use\nexport type { SyncOperation } from \"./manual-sync\"\n\n// Schema output type inference helper (matches electric.ts pattern)\ntype InferSchemaOutput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferOutput<T> extends object\n ? StandardSchemaV1.InferOutput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\n// Schema input type inference helper (matches electric.ts pattern)\ntype InferSchemaInput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferInput<T> extends object\n ? StandardSchemaV1.InferInput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\ntype TQueryKeyBuilder<TQueryKey> = (opts: LoadSubsetOptions) => TQueryKey\n\n/**\n * Configuration options for creating a Query Collection\n * @template T - The explicit type of items stored in the collection\n * @template TQueryFn - The queryFn type\n * @template TError - The type of errors that can occur during queries\n * @template TQueryKey - The type of the query key\n * @template TKey - The type of the item keys\n * @template TSchema - The schema type for validation\n */\nexport interface QueryCollectionConfig<\n T extends object = object,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any> = (\n context: QueryFunctionContext<any>\n ) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TSchema extends StandardSchemaV1 = never,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n> extends BaseCollectionConfig<T, TKey, TSchema> {\n /** The query key used by TanStack Query to identify this query */\n queryKey: TQueryKey | TQueryKeyBuilder<TQueryKey>\n /** Function that fetches data from the server. Must return the complete collection state */\n queryFn: TQueryFn extends (\n context: QueryFunctionContext<TQueryKey>\n ) => Promise<Array<any>>\n ? (context: QueryFunctionContext<TQueryKey>) => Promise<Array<T>>\n : TQueryFn\n /* Function that extracts array items from wrapped API responses (e.g metadata, pagination) */\n select?: (data: TQueryData) => Array<T>\n /** The TanStack Query client instance */\n queryClient: QueryClient\n\n // Query-specific options\n /** Whether the query should automatically run (default: true) */\n enabled?: boolean\n refetchInterval?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`refetchInterval`]\n retry?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`retry`]\n retryDelay?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`retryDelay`]\n staleTime?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`staleTime`]\n\n /**\n * Metadata to pass to the query.\n * Available in queryFn via context.meta\n *\n * @example\n * // Using meta for error context\n * queryFn: async (context) => {\n * try {\n * return await api.getTodos(userId)\n * } catch (error) {\n * // Use meta for better error messages\n * throw new Error(\n * context.meta?.errorMessage || 'Failed to load todos'\n * )\n * }\n * },\n * meta: {\n * errorMessage: `Failed to load todos for user ${userId}`\n * }\n */\n meta?: Record<string, unknown>\n}\n\n/**\n * Type for the refetch utility function\n * Returns the QueryObserverResult from TanStack Query\n */\nexport type RefetchFn = (opts?: {\n throwOnError?: boolean\n}) => Promise<QueryObserverResult<any, any> | void>\n\n/**\n * Utility methods available on Query Collections for direct writes and manual operations.\n * Direct writes bypass the normal query/mutation flow and write directly to the synced data store.\n * @template TItem - The type of items stored in the collection\n * @template TKey - The type of the item keys\n * @template TInsertInput - The type accepted for insert operations\n * @template TError - The type of errors that can occur during queries\n */\nexport interface QueryCollectionUtils<\n TItem extends object = Record<string, unknown>,\n TKey extends string | number = string | number,\n TInsertInput extends object = TItem,\n TError = unknown,\n> extends UtilsRecord {\n /** Manually trigger a refetch of the query */\n refetch: RefetchFn\n /** Insert one or more items directly into the synced data store without triggering a query refetch or optimistic update */\n writeInsert: (data: TInsertInput | Array<TInsertInput>) => void\n /** Update one or more items directly in the synced data store without triggering a query refetch or optimistic update */\n writeUpdate: (updates: Partial<TItem> | Array<Partial<TItem>>) => void\n /** Delete one or more items directly from the synced data store without triggering a query refetch or optimistic update */\n writeDelete: (keys: TKey | Array<TKey>) => void\n /** Insert or update one or more items directly in the synced data store without triggering a query refetch or optimistic update */\n writeUpsert: (data: Partial<TItem> | Array<Partial<TItem>>) => void\n /** Execute multiple write operations as a single atomic batch to the synced data store */\n writeBatch: (callback: () => void) => void\n\n // Query Observer State (getters)\n /** Get the last error encountered by the query (if any); reset on success */\n lastError: TError | undefined\n /** Check if the collection is in an error state */\n isError: boolean\n /**\n * Get the number of consecutive sync failures.\n * Incremented only when query fails completely (not per retry attempt); reset on success.\n */\n errorCount: number\n /** Check if query is currently fetching (initial or background) */\n isFetching: boolean\n /** Check if query is refetching in background (not initial fetch) */\n isRefetching: boolean\n /** Check if query is loading for the first time (no data yet) */\n isLoading: boolean\n /** Get timestamp of last successful data update (in milliseconds) */\n dataUpdatedAt: number\n /** Get current fetch status */\n fetchStatus: `fetching` | `paused` | `idle`\n\n /**\n * Clear the error state and trigger a refetch of the query\n * @returns Promise that resolves when the refetch completes successfully\n * @throws Error if the refetch fails\n */\n clearError: () => Promise<void>\n}\n\n/**\n * Internal state object for tracking query observer and errors\n */\ninterface QueryCollectionState {\n lastError: any\n errorCount: number\n lastErrorUpdatedAt: number\n observers: Map<\n string,\n QueryObserver<Array<any>, any, Array<any>, Array<any>, any>\n >\n}\n\n/**\n * Implementation class for QueryCollectionUtils with explicit dependency injection\n * for better testability and architectural clarity\n */\nclass QueryCollectionUtilsImpl {\n private state: QueryCollectionState\n private refetchFn: RefetchFn\n\n // Write methods\n public refetch: RefetchFn\n public writeInsert: any\n public writeUpdate: any\n public writeDelete: any\n public writeUpsert: any\n public writeBatch: any\n\n constructor(\n state: QueryCollectionState,\n refetch: RefetchFn,\n writeUtils: ReturnType<typeof createWriteUtils>\n ) {\n this.state = state\n this.refetchFn = refetch\n\n // Initialize methods to use passed dependencies\n this.refetch = refetch\n this.writeInsert = writeUtils.writeInsert\n this.writeUpdate = writeUtils.writeUpdate\n this.writeDelete = writeUtils.writeDelete\n this.writeUpsert = writeUtils.writeUpsert\n this.writeBatch = writeUtils.writeBatch\n }\n\n public async clearError() {\n this.state.lastError = undefined\n this.state.errorCount = 0\n this.state.lastErrorUpdatedAt = 0\n await this.refetchFn({ throwOnError: true })\n }\n\n // Getters for error state\n public get lastError() {\n return this.state.lastError\n }\n\n public get isError() {\n return !!this.state.lastError\n }\n\n public get errorCount() {\n return this.state.errorCount\n }\n\n // Getters for QueryObserver state\n public get isFetching() {\n // check if any observer is fetching\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isFetching\n )\n }\n\n public get isRefetching() {\n // check if any observer is refetching\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isRefetching\n )\n }\n\n public get isLoading() {\n // check if any observer is loading\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isLoading\n )\n }\n\n public get dataUpdatedAt() {\n // compute the max dataUpdatedAt of all observers\n return Math.max(\n 0,\n ...Array.from(this.state.observers.values()).map(\n (observer) => observer.getCurrentResult().dataUpdatedAt\n )\n )\n }\n\n public get fetchStatus(): Array<FetchStatus> {\n return Array.from(this.state.observers.values()).map(\n (observer) => observer.getCurrentResult().fetchStatus\n )\n }\n}\n\n/**\n * Creates query collection options for use with a standard Collection.\n * This integrates TanStack Query with TanStack DB for automatic synchronization.\n *\n * Supports automatic type inference following the priority order:\n * 1. Schema inference (highest priority)\n * 2. QueryFn return type inference (second priority)\n *\n * @template T - Type of the schema if a schema is provided otherwise it is the type of the values returned by the queryFn\n * @template TError - The type of errors that can occur during queries\n * @template TQueryKey - The type of the query key\n * @template TKey - The type of the item keys\n * @param config - Configuration options for the Query collection\n * @returns Collection options with utilities for direct writes and manual operations\n *\n * @example\n * // Type inferred from queryFn return type (NEW!)\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => {\n * const response = await fetch('/api/todos')\n * return response.json() as Todo[] // Type automatically inferred!\n * },\n * queryClient,\n * getKey: (item) => item.id, // item is typed as Todo\n * })\n * )\n *\n * @example\n * // Explicit type\n * const todosCollection = createCollection<Todo>(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * queryClient,\n * getKey: (item) => item.id,\n * })\n * )\n *\n * @example\n * // Schema inference\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * queryClient,\n * schema: todoSchema, // Type inferred from schema\n * getKey: (item) => item.id,\n * })\n * )\n *\n * @example\n * // With persistence handlers\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: fetchTodos,\n * queryClient,\n * getKey: (item) => item.id,\n * onInsert: async ({ transaction }) => {\n * await api.createTodos(transaction.mutations.map(m => m.modified))\n * },\n * onUpdate: async ({ transaction }) => {\n * await api.updateTodos(transaction.mutations)\n * },\n * onDelete: async ({ transaction }) => {\n * await api.deleteTodos(transaction.mutations.map(m => m.key))\n * }\n * })\n * )\n *\n * @example\n * // The select option extracts the items array from a response with metadata\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * select: (data) => data.items, // Extract the array of items\n * queryClient,\n * schema: todoSchema,\n * getKey: (item) => item.id,\n * })\n * )\n */\n// Overload for when schema is provided and select present\nexport function queryCollectionOptions<\n T extends StandardSchemaV1,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n>(\n config: QueryCollectionConfig<\n InferSchemaOutput<T>,\n TQueryFn,\n TError,\n TQueryKey,\n TKey,\n T\n > & {\n schema: T\n select: (data: TQueryData) => Array<InferSchemaInput<T>>\n }\n): CollectionConfig<\n InferSchemaOutput<T>,\n TKey,\n T,\n QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>\n> & {\n schema: T\n utils: QueryCollectionUtils<\n InferSchemaOutput<T>,\n TKey,\n InferSchemaInput<T>,\n TError\n >\n}\n\n// Overload for when no schema is provided and select present\nexport function queryCollectionOptions<\n T extends object,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any> = (\n context: QueryFunctionContext<any>\n ) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n>(\n config: QueryCollectionConfig<\n T,\n TQueryFn,\n TError,\n TQueryKey,\n TKey,\n never,\n TQueryData\n > & {\n schema?: never // prohibit schema\n select: (data: TQueryData) => Array<T>\n }\n): CollectionConfig<\n T,\n TKey,\n never,\n QueryCollectionUtils<T, TKey, T, TError>\n> & {\n schema?: never // no schema in the result\n utils: QueryCollectionUtils<T, TKey, T, TError>\n}\n\n// Overload for when schema is provided\nexport function queryCollectionOptions<\n T extends StandardSchemaV1,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n>(\n config: QueryCollectionConfig<\n InferSchemaOutput<T>,\n (\n context: QueryFunctionContext<any>\n ) => Promise<Array<InferSchemaOutput<T>>>,\n TError,\n TQueryKey,\n TKey,\n T\n > & {\n schema: T\n }\n): CollectionConfig<\n InferSchemaOutput<T>,\n TKey,\n T,\n QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>\n> & {\n schema: T\n utils: QueryCollectionUtils<\n InferSchemaOutput<T>,\n TKey,\n InferSchemaInput<T>,\n TError\n >\n}\n\n// Overload for when no schema is provided\nexport function queryCollectionOptions<\n T extends object,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n>(\n config: QueryCollectionConfig<\n T,\n (context: QueryFunctionContext<any>) => Promise<Array<T>>,\n TError,\n TQueryKey,\n TKey\n > & {\n schema?: never // prohibit schema\n }\n): CollectionConfig<\n T,\n TKey,\n never,\n QueryCollectionUtils<T, TKey, T, TError>\n> & {\n schema?: never // no schema in the result\n utils: QueryCollectionUtils<T, TKey, T, TError>\n}\n\nexport function queryCollectionOptions(\n config: QueryCollectionConfig<Record<string, unknown>>\n): CollectionConfig<\n Record<string, unknown>,\n string | number,\n never,\n QueryCollectionUtils\n> & {\n utils: QueryCollectionUtils\n} {\n const {\n queryKey,\n queryFn,\n select,\n queryClient,\n enabled,\n refetchInterval,\n retry,\n retryDelay,\n staleTime,\n getKey,\n onInsert,\n onUpdate,\n onDelete,\n meta,\n ...baseCollectionConfig\n } = config\n\n // Default to eager sync mode if not provided\n const syncMode = baseCollectionConfig.syncMode ?? `eager`\n\n // Validate required parameters\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryKey) {\n throw new QueryKeyRequiredError()\n }\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryFn) {\n throw new QueryFnRequiredError()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryClient) {\n throw new QueryClientRequiredError()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!getKey) {\n throw new GetKeyRequiredError()\n }\n\n /** State object to hold error tracking and observer reference */\n const state: QueryCollectionState = {\n lastError: undefined as any,\n errorCount: 0,\n lastErrorUpdatedAt: 0,\n observers: new Map<\n string,\n QueryObserver<Array<any>, any, Array<any>, Array<any>, any>\n >(),\n }\n\n // hashedQueryKey → queryKey\n const hashToQueryKey = new Map<string, QueryKey>()\n\n // queryKey → Set<RowKey>\n const queryToRows = new Map<string, Set<string | number>>()\n\n // RowKey → Set<queryKey>\n const rowToQueries = new Map<string | number, Set<string>>()\n\n // queryKey → QueryObserver's unsubscribe function\n const unsubscribes = new Map<string, () => void>()\n\n // Helper function to add a row to the internal state\n const addRow = (rowKey: string | number, hashedQueryKey: string) => {\n const rowToQueriesSet = rowToQueries.get(rowKey) || new Set()\n rowToQueriesSet.add(hashedQueryKey)\n rowToQueries.set(rowKey, rowToQueriesSet)\n\n const queryToRowsSet = queryToRows.get(hashedQueryKey) || new Set()\n queryToRowsSet.add(rowKey)\n queryToRows.set(hashedQueryKey, queryToRowsSet)\n }\n\n // Helper function to remove a row from the internal state\n const removeRow = (rowKey: string | number, hashedQuerKey: string) => {\n const rowToQueriesSet = rowToQueries.get(rowKey) || new Set()\n rowToQueriesSet.delete(hashedQuerKey)\n rowToQueries.set(rowKey, rowToQueriesSet)\n\n const queryToRowsSet = queryToRows.get(hashedQuerKey) || new Set()\n queryToRowsSet.delete(rowKey)\n queryToRows.set(hashedQuerKey, queryToRowsSet)\n\n return rowToQueriesSet.size === 0\n }\n\n const internalSync: SyncConfig<any>[`sync`] = (params) => {\n const { begin, write, commit, markReady, collection } = params\n\n // Track whether sync has been started\n let syncStarted = false\n\n const createQueryFromOpts = (\n opts: LoadSubsetOptions = {},\n queryFunction: typeof queryFn = queryFn\n ): true | Promise<void> => {\n // Push the predicates down to the queryKey and queryFn\n const key = typeof queryKey === `function` ? queryKey(opts) : queryKey\n const hashedQueryKey = hashKey(key)\n const extendedMeta = { ...meta, loadSubsetOptions: opts }\n\n if (state.observers.has(hashedQueryKey)) {\n // We already have a query for this queryKey\n // Get the current result and return based on its state\n const observer = state.observers.get(hashedQueryKey)!\n const currentResult = observer.getCurrentResult()\n\n if (currentResult.isSuccess) {\n // Data is already available, return true synchronously\n return true\n } else if (currentResult.isError) {\n // Error already occurred, reject immediately\n return Promise.reject(currentResult.error)\n } else {\n // Query is still loading, wait for the first result\n return new Promise<void>((resolve, reject) => {\n const unsubscribe = observer.subscribe((result) => {\n if (result.isSuccess) {\n unsubscribe()\n resolve()\n } else if (result.isError) {\n unsubscribe()\n reject(result.error)\n }\n })\n })\n }\n }\n\n const observerOptions: QueryObserverOptions<\n Array<any>,\n any,\n Array<any>,\n Array<any>,\n any\n > = {\n queryKey: key,\n queryFn: queryFunction,\n meta: extendedMeta,\n structuralSharing: true,\n notifyOnChangeProps: `all`,\n\n // Only include options that are explicitly defined to allow QueryClient defaultOptions to be used\n ...(enabled !== undefined && { enabled }),\n ...(refetchInterval !== undefined && { refetchInterval }),\n ...(retry !== undefined && { retry }),\n ...(retryDelay !== undefined && { retryDelay }),\n ...(staleTime !== undefined && { staleTime }),\n }\n\n const localObserver = new QueryObserver<\n Array<any>,\n any,\n Array<any>,\n Array<any>,\n any\n >(queryClient, observerOptions)\n\n hashToQueryKey.set(hashedQueryKey, key)\n state.observers.set(hashedQueryKey, localObserver)\n\n // Create a promise that resolves when the query result is first available\n const readyPromise = new Promise<void>((resolve, reject) => {\n const unsubscribe = localObserver.subscribe((result) => {\n if (result.isSuccess) {\n unsubscribe()\n resolve()\n } else if (result.isError) {\n unsubscribe()\n reject(result.error)\n }\n })\n })\n\n // If sync has started or there are subscribers to the collection, subscribe to the query straight away\n // This creates the main subscription that handles data updates\n if (syncStarted || collection.subscriberCount > 0) {\n subscribeToQuery(localObserver, hashedQueryKey)\n }\n\n // Tell tanstack query to GC the query when the subscription is unsubscribed\n // The subscription is unsubscribed when the live query is GCed.\n const subscription = opts.subscription\n subscription?.once(`unsubscribed`, () => {\n queryClient.removeQueries({ queryKey: key, exact: true })\n })\n\n return readyPromise\n }\n\n type UpdateHandler = Parameters<QueryObserver[`subscribe`]>[0]\n\n const makeQueryResultHandler = (queryKey: QueryKey) => {\n const hashedQueryKey = hashKey(queryKey)\n const handleQueryResult: UpdateHandler = (result) => {\n if (result.isSuccess) {\n // Clear error state\n state.lastError = undefined\n state.errorCount = 0\n\n const rawData = result.data\n const newItemsArray = select ? select(rawData) : rawData\n\n if (\n !Array.isArray(newItemsArray) ||\n newItemsArray.some((item) => typeof item !== `object`)\n ) {\n const errorMessage = select\n ? `@tanstack/query-db-collection: select() must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`\n : `@tanstack/query-db-collection: queryFn must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`\n\n console.error(errorMessage)\n return\n }\n\n const currentSyncedItems: Map<string | number, any> = new Map(\n collection._state.syncedData.entries()\n )\n const newItemsMap = new Map<string | number, any>()\n newItemsArray.forEach((item) => {\n const key = getKey(item)\n newItemsMap.set(key, item)\n })\n\n begin()\n\n // Helper function for shallow equality check of objects\n const shallowEqual = (\n obj1: Record<string, any>,\n obj2: Record<string, any>\n ): boolean => {\n // Get all keys from both objects\n const keys1 = Object.keys(obj1)\n const keys2 = Object.keys(obj2)\n\n // If number of keys is different, objects are not equal\n if (keys1.length !== keys2.length) return false\n\n // Check if all keys in obj1 have the same values in obj2\n return keys1.every((key) => {\n // Skip comparing functions and complex objects deeply\n if (typeof obj1[key] === `function`) return true\n return obj1[key] === obj2[key]\n })\n }\n\n currentSyncedItems.forEach((oldItem, key) => {\n const newItem = newItemsMap.get(key)\n if (!newItem) {\n const needToRemove = removeRow(key, hashedQueryKey) // returns true if the row is no longer referenced by any queries\n if (needToRemove) {\n write({ type: `delete`, value: oldItem })\n }\n } else if (\n !shallowEqual(\n oldItem as Record<string, any>,\n newItem as Record<string, any>\n )\n ) {\n // Only update if there are actual differences in the properties\n write({ type: `update`, value: newItem })\n }\n })\n\n newItemsMap.forEach((newItem, key) => {\n addRow(key, hashedQueryKey)\n if (!currentSyncedItems.has(key)) {\n write({ type: `insert`, value: newItem })\n }\n })\n\n commit()\n\n // Mark collection as ready after first successful query result\n markReady()\n } else if (result.isError) {\n if (result.errorUpdatedAt !== state.lastErrorUpdatedAt) {\n state.lastError = result.error\n state.errorCount++\n state.lastErrorUpdatedAt = result.errorUpdatedAt\n }\n\n console.error(\n `[QueryCollection] Error observing query ${String(queryKey)}:`,\n result.error\n )\n\n // Mark collection as ready even on error to avoid blocking apps\n markReady()\n }\n }\n return handleQueryResult\n }\n\n // This function is called when a loadSubset call is deduplicated\n // meaning that we have all the data locally available to answer the query\n // so we execute the query locally\n const createLocalQuery = (opts: LoadSubsetOptions) => {\n const queryFn = ({ meta }: QueryFunctionContext<any>) => {\n const inserts = collection.currentStateAsChanges(\n meta!.loadSubsetOptions as LoadSubsetOptions\n )!\n const data = inserts.map(({ value }) => value)\n return Promise.resolve(data)\n }\n\n createQueryFromOpts(opts, queryFn)\n }\n\n const isSubscribed = (hashedQueryKey: string) => {\n return unsubscribes.has(hashedQueryKey)\n }\n\n const subscribeToQuery = (\n observer: QueryObserver<Array<any>, any, Array<any>, Array<any>, any>,\n hashedQueryKey: string\n ) => {\n if (!isSubscribed(hashedQueryKey)) {\n const queryKey = hashToQueryKey.get(hashedQueryKey)!\n const handleQueryResult = makeQueryResultHandler(queryKey)\n const unsubscribeFn = observer.subscribe(handleQueryResult)\n unsubscribes.set(hashedQueryKey, unsubscribeFn)\n }\n }\n\n const subscribeToQueries = () => {\n state.observers.forEach(subscribeToQuery)\n }\n\n const unsubscribeFromQueries = () => {\n unsubscribes.forEach((unsubscribeFn) => {\n unsubscribeFn()\n })\n unsubscribes.clear()\n }\n\n // Mark that sync has started\n syncStarted = true\n\n // Set up event listener for subscriber changes\n const unsubscribeFromCollectionEvents = collection.on(\n `subscribers:change`,\n ({ subscriberCount }) => {\n if (subscriberCount > 0) {\n subscribeToQueries()\n } else if (subscriberCount === 0) {\n unsubscribeFromQueries()\n }\n }\n )\n\n // If syncMode is eager, create the initial query without any predicates\n if (syncMode === `eager`) {\n // Catch any errors to prevent unhandled rejections\n const initialResult = createQueryFromOpts({})\n if (initialResult instanceof Promise) {\n initialResult.catch(() => {\n // Errors are already handled by the query result handler\n })\n }\n } else {\n // In on-demand mode, mark ready immediately since there's no initial query\n markReady()\n }\n\n // Always subscribe when sync starts (this could be from preload(), startSync config, or first subscriber)\n // We'll dynamically unsubscribe/resubscribe based on subscriber count to maintain staleTime behavior\n subscribeToQueries()\n\n // Ensure we process any existing query data (QueryObserver doesn't invoke its callback automatically with initial state)\n state.observers.forEach((observer, hashedQueryKey) => {\n const queryKey = hashToQueryKey.get(hashedQueryKey)!\n const handleQueryResult = makeQueryResultHandler(queryKey)\n handleQueryResult(observer.getCurrentResult())\n })\n\n // Subscribe to the query client's cache to handle queries that are GCed by tanstack query\n const unsubscribeQueryCache = queryClient\n .getQueryCache()\n .subscribe((event) => {\n const hashedKey = event.query.queryHash\n if (event.type === `removed`) {\n cleanupQuery(hashedKey)\n }\n })\n\n function cleanupQuery(hashedQueryKey: string) {\n // Unsubscribe from the query's observer\n unsubscribes.get(hashedQueryKey)?.()\n\n // Get all the rows that are in the result of this query\n const rowKeys = queryToRows.get(hashedQueryKey) ?? new Set()\n\n // Remove the query from these rows\n rowKeys.forEach((rowKey) => {\n const queries = rowToQueries.get(rowKey) // set of queries that reference this row\n if (queries && queries.size > 0) {\n queries.delete(hashedQueryKey)\n if (queries.size === 0) {\n // Reference count dropped to 0, we can GC the row\n rowToQueries.delete(rowKey)\n\n if (collection.has(rowKey)) {\n begin()\n write({ type: `delete`, value: collection.get(rowKey) })\n commit()\n }\n }\n }\n })\n\n // Remove the query from the internal state\n unsubscribes.delete(hashedQueryKey)\n state.observers.delete(hashedQueryKey)\n queryToRows.delete(hashedQueryKey)\n hashToQueryKey.delete(hashedQueryKey)\n }\n\n const cleanup = async () => {\n unsubscribeFromCollectionEvents()\n unsubscribeFromQueries()\n\n const queryKeys = [...hashToQueryKey.values()]\n\n hashToQueryKey.clear()\n queryToRows.clear()\n rowToQueries.clear()\n state.observers.clear()\n unsubscribeQueryCache()\n\n await Promise.all(\n queryKeys.map(async (queryKey) => {\n await queryClient.cancelQueries({ queryKey })\n queryClient.removeQueries({ queryKey })\n })\n )\n }\n\n // Create deduplicated loadSubset wrapper for non-eager modes\n // This prevents redundant snapshot requests when multiple concurrent\n // live queries request overlapping or subset predicates\n const loadSubsetDedupe =\n syncMode === `eager`\n ? undefined\n : new DeduplicatedLoadSubset({\n loadSubset: createQueryFromOpts,\n onDeduplicate: createLocalQuery,\n })\n\n return {\n loadSubset: loadSubsetDedupe?.loadSubset,\n cleanup,\n }\n }\n\n /**\n * Refetch the query data\n *\n * Uses queryObserver.refetch() because:\n * - Bypasses `enabled: false` to support manual/imperative refetch patterns (e.g., button-triggered fetch)\n * - Ensures clearError() works even when enabled: false\n * - Always refetches THIS specific collection (exact targeting via observer)\n * - Respects retry, retryDelay, and other observer options\n *\n * This matches TanStack Query's hook behavior where refetch() bypasses enabled: false.\n * See: https://tanstack.com/query/latest/docs/framework/react/guides/disabling-queries\n *\n * Used by both:\n * - utils.refetch() - for explicit user-triggered refetches\n * - Internal handlers (onInsert/onUpdate/onDelete) - after mutations to get fresh data\n *\n * @returns Promise that resolves when the refetch is complete, with QueryObserverResult\n */\n const refetch: RefetchFn = async (opts) => {\n const queryKeys = [...hashToQueryKey.values()]\n const refetchPromises = queryKeys.map((queryKey) => {\n const queryObserver = state.observers.get(hashKey(queryKey))!\n return queryObserver.refetch({\n throwOnError: opts?.throwOnError,\n })\n })\n\n await Promise.all(refetchPromises)\n }\n\n // Create write context for manual write operations\n let writeContext: {\n collection: any\n queryClient: QueryClient\n queryKey: Array<unknown>\n getKey: (item: any) => string | number\n begin: () => void\n write: (message: Omit<ChangeMessage<any>, `key`>) => void\n commit: () => void\n } | null = null\n\n // Enhanced internalSync that captures write functions for manual use\n const enhancedInternalSync: SyncConfig<any>[`sync`] = (params) => {\n const { begin, write, commit, collection } = params\n\n // Store references for manual write operations\n writeContext = {\n collection,\n queryClient,\n queryKey: queryKey as unknown as Array<unknown>,\n getKey: getKey as (item: any) => string | number,\n begin,\n write,\n commit,\n }\n\n // Call the original internalSync logic\n return internalSync(params)\n }\n\n // Create write utils using the manual-sync module\n const writeUtils = createWriteUtils<any, string | number, any>(\n () => writeContext\n )\n\n // Create wrapper handlers for direct persistence operations that handle refetching\n const wrappedOnInsert = onInsert\n ? async (params: InsertMutationFnParams<any>) => {\n const handlerResult = (await onInsert(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnUpdate = onUpdate\n ? async (params: UpdateMutationFnParams<any>) => {\n const handlerResult = (await onUpdate(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnDelete = onDelete\n ? async (params: DeleteMutationFnParams<any>) => {\n const handlerResult = (await onDelete(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n // Create utils instance with state and dependencies passed explicitly\n const utils: any = new QueryCollectionUtilsImpl(state, refetch, writeUtils)\n\n return {\n ...baseCollectionConfig,\n getKey,\n syncMode,\n sync: { sync: enhancedInternalSync },\n onInsert: wrappedOnInsert,\n onUpdate: wrappedOnUpdate,\n onDelete: wrappedOnDelete,\n utils,\n }\n}\n"],"names":["queryKey","queryFn","meta"],"mappings":";;;;AA0NA,MAAM,yBAAyB;AAAA,EAY7B,YACE,OACA,SACA,YACA;AACA,SAAK,QAAQ;AACb,SAAK,YAAY;AAGjB,SAAK,UAAU;AACf,SAAK,cAAc,WAAW;AAC9B,SAAK,cAAc,WAAW;AAC9B,SAAK,cAAc,WAAW;AAC9B,SAAK,cAAc,WAAW;AAC9B,SAAK,aAAa,WAAW;AAAA,EA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,QAAM,kBAAkB,WACpB,OAAO,WAAwC;AAC7C,UAAM,gBAAiB,MAAM,SAAS,MAAM,KAAM,CAAA;AAClD,UAAM,gBACH,cAAwC,YAAY;AAEvD,QAAI,eAAe;AACjB,YAAM,QAAA;AAAA,IACR;AAEA,WAAO;AAAA,EACT,IACA;AAGJ,QAAM,QAAa,IAAI,yBAAyB,OAAO,SAAS,UAAU;AAE1E,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA,MAAM,EAAE,MAAM,qBAAA;AAAA,IACd,UAAU;AAAA,IACV,UAAU;AAAA,IACV,UAAU;AAAA,IACV;AAAA,EAAA;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"query.js","sources":["../../src/query.ts"],"sourcesContent":["import { QueryObserver, hashKey } from \"@tanstack/query-core\"\nimport {\n GetKeyRequiredError,\n QueryClientRequiredError,\n QueryFnRequiredError,\n QueryKeyRequiredError,\n} from \"./errors\"\nimport { createWriteUtils } from \"./manual-sync\"\nimport { serializeLoadSubsetOptions } from \"./serialization\"\nimport type {\n BaseCollectionConfig,\n ChangeMessage,\n CollectionConfig,\n DeleteMutationFnParams,\n InsertMutationFnParams,\n LoadSubsetOptions,\n SyncConfig,\n UpdateMutationFnParams,\n UtilsRecord,\n} from \"@tanstack/db\"\nimport type {\n FetchStatus,\n QueryClient,\n QueryFunctionContext,\n QueryKey,\n QueryObserverOptions,\n QueryObserverResult,\n} from \"@tanstack/query-core\"\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\"\n\n// Re-export for external use\nexport type { SyncOperation } from \"./manual-sync\"\n\n// Schema output type inference helper (matches electric.ts pattern)\ntype InferSchemaOutput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferOutput<T> extends object\n ? StandardSchemaV1.InferOutput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\n// Schema input type inference helper (matches electric.ts pattern)\ntype InferSchemaInput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferInput<T> extends object\n ? StandardSchemaV1.InferInput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\ntype TQueryKeyBuilder<TQueryKey> = (opts: LoadSubsetOptions) => TQueryKey\n\n/**\n * Configuration options for creating a Query Collection\n * @template T - The explicit type of items stored in the collection\n * @template TQueryFn - The queryFn type\n * @template TError - The type of errors that can occur during queries\n * @template TQueryKey - The type of the query key\n * @template TKey - The type of the item keys\n * @template TSchema - The schema type for validation\n */\nexport interface QueryCollectionConfig<\n T extends object = object,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any> = (\n context: QueryFunctionContext<any>\n ) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TSchema extends StandardSchemaV1 = never,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n> extends BaseCollectionConfig<T, TKey, TSchema> {\n /** The query key used by TanStack Query to identify this query */\n queryKey: TQueryKey | TQueryKeyBuilder<TQueryKey>\n /** Function that fetches data from the server. Must return the complete collection state */\n queryFn: TQueryFn extends (\n context: QueryFunctionContext<TQueryKey>\n ) => Promise<Array<any>>\n ? (context: QueryFunctionContext<TQueryKey>) => Promise<Array<T>>\n : TQueryFn\n /* Function that extracts array items from wrapped API responses (e.g metadata, pagination) */\n select?: (data: TQueryData) => Array<T>\n /** The TanStack Query client instance */\n queryClient: QueryClient\n\n // Query-specific options\n /** Whether the query should automatically run (default: true) */\n enabled?: boolean\n refetchInterval?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`refetchInterval`]\n retry?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`retry`]\n retryDelay?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`retryDelay`]\n staleTime?: QueryObserverOptions<\n Array<T>,\n TError,\n Array<T>,\n Array<T>,\n TQueryKey\n >[`staleTime`]\n\n /**\n * Metadata to pass to the query.\n * Available in queryFn via context.meta\n *\n * @example\n * // Using meta for error context\n * queryFn: async (context) => {\n * try {\n * return await api.getTodos(userId)\n * } catch (error) {\n * // Use meta for better error messages\n * throw new Error(\n * context.meta?.errorMessage || 'Failed to load todos'\n * )\n * }\n * },\n * meta: {\n * errorMessage: `Failed to load todos for user ${userId}`\n * }\n */\n meta?: Record<string, unknown>\n}\n\n/**\n * Type for the refetch utility function\n * Returns the QueryObserverResult from TanStack Query\n */\nexport type RefetchFn = (opts?: {\n throwOnError?: boolean\n}) => Promise<QueryObserverResult<any, any> | void>\n\n/**\n * Utility methods available on Query Collections for direct writes and manual operations.\n * Direct writes bypass the normal query/mutation flow and write directly to the synced data store.\n * @template TItem - The type of items stored in the collection\n * @template TKey - The type of the item keys\n * @template TInsertInput - The type accepted for insert operations\n * @template TError - The type of errors that can occur during queries\n */\nexport interface QueryCollectionUtils<\n TItem extends object = Record<string, unknown>,\n TKey extends string | number = string | number,\n TInsertInput extends object = TItem,\n TError = unknown,\n> extends UtilsRecord {\n /** Manually trigger a refetch of the query */\n refetch: RefetchFn\n /** Insert one or more items directly into the synced data store without triggering a query refetch or optimistic update */\n writeInsert: (data: TInsertInput | Array<TInsertInput>) => void\n /** Update one or more items directly in the synced data store without triggering a query refetch or optimistic update */\n writeUpdate: (updates: Partial<TItem> | Array<Partial<TItem>>) => void\n /** Delete one or more items directly from the synced data store without triggering a query refetch or optimistic update */\n writeDelete: (keys: TKey | Array<TKey>) => void\n /** Insert or update one or more items directly in the synced data store without triggering a query refetch or optimistic update */\n writeUpsert: (data: Partial<TItem> | Array<Partial<TItem>>) => void\n /** Execute multiple write operations as a single atomic batch to the synced data store */\n writeBatch: (callback: () => void) => void\n\n // Query Observer State (getters)\n /** Get the last error encountered by the query (if any); reset on success */\n lastError: TError | undefined\n /** Check if the collection is in an error state */\n isError: boolean\n /**\n * Get the number of consecutive sync failures.\n * Incremented only when query fails completely (not per retry attempt); reset on success.\n */\n errorCount: number\n /** Check if query is currently fetching (initial or background) */\n isFetching: boolean\n /** Check if query is refetching in background (not initial fetch) */\n isRefetching: boolean\n /** Check if query is loading for the first time (no data yet) */\n isLoading: boolean\n /** Get timestamp of last successful data update (in milliseconds) */\n dataUpdatedAt: number\n /** Get current fetch status */\n fetchStatus: `fetching` | `paused` | `idle`\n\n /**\n * Clear the error state and trigger a refetch of the query\n * @returns Promise that resolves when the refetch completes successfully\n * @throws Error if the refetch fails\n */\n clearError: () => Promise<void>\n}\n\n/**\n * Internal state object for tracking query observer and errors\n */\ninterface QueryCollectionState {\n lastError: any\n errorCount: number\n lastErrorUpdatedAt: number\n observers: Map<\n string,\n QueryObserver<Array<any>, any, Array<any>, Array<any>, any>\n >\n}\n\n/**\n * Implementation class for QueryCollectionUtils with explicit dependency injection\n * for better testability and architectural clarity\n */\nclass QueryCollectionUtilsImpl {\n private state: QueryCollectionState\n private refetchFn: RefetchFn\n\n // Write methods\n public refetch: RefetchFn\n public writeInsert: any\n public writeUpdate: any\n public writeDelete: any\n public writeUpsert: any\n public writeBatch: any\n\n constructor(\n state: QueryCollectionState,\n refetch: RefetchFn,\n writeUtils: ReturnType<typeof createWriteUtils>\n ) {\n this.state = state\n this.refetchFn = refetch\n\n // Initialize methods to use passed dependencies\n this.refetch = refetch\n this.writeInsert = writeUtils.writeInsert\n this.writeUpdate = writeUtils.writeUpdate\n this.writeDelete = writeUtils.writeDelete\n this.writeUpsert = writeUtils.writeUpsert\n this.writeBatch = writeUtils.writeBatch\n }\n\n public async clearError() {\n this.state.lastError = undefined\n this.state.errorCount = 0\n this.state.lastErrorUpdatedAt = 0\n await this.refetchFn({ throwOnError: true })\n }\n\n // Getters for error state\n public get lastError() {\n return this.state.lastError\n }\n\n public get isError() {\n return !!this.state.lastError\n }\n\n public get errorCount() {\n return this.state.errorCount\n }\n\n // Getters for QueryObserver state\n public get isFetching() {\n // check if any observer is fetching\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isFetching\n )\n }\n\n public get isRefetching() {\n // check if any observer is refetching\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isRefetching\n )\n }\n\n public get isLoading() {\n // check if any observer is loading\n return Array.from(this.state.observers.values()).some(\n (observer) => observer.getCurrentResult().isLoading\n )\n }\n\n public get dataUpdatedAt() {\n // compute the max dataUpdatedAt of all observers\n return Math.max(\n 0,\n ...Array.from(this.state.observers.values()).map(\n (observer) => observer.getCurrentResult().dataUpdatedAt\n )\n )\n }\n\n public get fetchStatus(): Array<FetchStatus> {\n return Array.from(this.state.observers.values()).map(\n (observer) => observer.getCurrentResult().fetchStatus\n )\n }\n}\n\n/**\n * Creates query collection options for use with a standard Collection.\n * This integrates TanStack Query with TanStack DB for automatic synchronization.\n *\n * Supports automatic type inference following the priority order:\n * 1. Schema inference (highest priority)\n * 2. QueryFn return type inference (second priority)\n *\n * @template T - Type of the schema if a schema is provided otherwise it is the type of the values returned by the queryFn\n * @template TError - The type of errors that can occur during queries\n * @template TQueryKey - The type of the query key\n * @template TKey - The type of the item keys\n * @param config - Configuration options for the Query collection\n * @returns Collection options with utilities for direct writes and manual operations\n *\n * @example\n * // Type inferred from queryFn return type (NEW!)\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => {\n * const response = await fetch('/api/todos')\n * return response.json() as Todo[] // Type automatically inferred!\n * },\n * queryClient,\n * getKey: (item) => item.id, // item is typed as Todo\n * })\n * )\n *\n * @example\n * // Explicit type\n * const todosCollection = createCollection<Todo>(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * queryClient,\n * getKey: (item) => item.id,\n * })\n * )\n *\n * @example\n * // Schema inference\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * queryClient,\n * schema: todoSchema, // Type inferred from schema\n * getKey: (item) => item.id,\n * })\n * )\n *\n * @example\n * // With persistence handlers\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: fetchTodos,\n * queryClient,\n * getKey: (item) => item.id,\n * onInsert: async ({ transaction }) => {\n * await api.createTodos(transaction.mutations.map(m => m.modified))\n * },\n * onUpdate: async ({ transaction }) => {\n * await api.updateTodos(transaction.mutations)\n * },\n * onDelete: async ({ transaction }) => {\n * await api.deleteTodos(transaction.mutations.map(m => m.key))\n * }\n * })\n * )\n *\n * @example\n * // The select option extracts the items array from a response with metadata\n * const todosCollection = createCollection(\n * queryCollectionOptions({\n * queryKey: ['todos'],\n * queryFn: async () => fetch('/api/todos').then(r => r.json()),\n * select: (data) => data.items, // Extract the array of items\n * queryClient,\n * schema: todoSchema,\n * getKey: (item) => item.id,\n * })\n * )\n */\n// Overload for when schema is provided and select present\nexport function queryCollectionOptions<\n T extends StandardSchemaV1,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n>(\n config: QueryCollectionConfig<\n InferSchemaOutput<T>,\n TQueryFn,\n TError,\n TQueryKey,\n TKey,\n T\n > & {\n schema: T\n select: (data: TQueryData) => Array<InferSchemaInput<T>>\n }\n): CollectionConfig<\n InferSchemaOutput<T>,\n TKey,\n T,\n QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>\n> & {\n schema: T\n utils: QueryCollectionUtils<\n InferSchemaOutput<T>,\n TKey,\n InferSchemaInput<T>,\n TError\n >\n}\n\n// Overload for when no schema is provided and select present\nexport function queryCollectionOptions<\n T extends object,\n TQueryFn extends (context: QueryFunctionContext<any>) => Promise<any> = (\n context: QueryFunctionContext<any>\n ) => Promise<any>,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n TQueryData = Awaited<ReturnType<TQueryFn>>,\n>(\n config: QueryCollectionConfig<\n T,\n TQueryFn,\n TError,\n TQueryKey,\n TKey,\n never,\n TQueryData\n > & {\n schema?: never // prohibit schema\n select: (data: TQueryData) => Array<T>\n }\n): CollectionConfig<\n T,\n TKey,\n never,\n QueryCollectionUtils<T, TKey, T, TError>\n> & {\n schema?: never // no schema in the result\n utils: QueryCollectionUtils<T, TKey, T, TError>\n}\n\n// Overload for when schema is provided\nexport function queryCollectionOptions<\n T extends StandardSchemaV1,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n>(\n config: QueryCollectionConfig<\n InferSchemaOutput<T>,\n (\n context: QueryFunctionContext<any>\n ) => Promise<Array<InferSchemaOutput<T>>>,\n TError,\n TQueryKey,\n TKey,\n T\n > & {\n schema: T\n }\n): CollectionConfig<\n InferSchemaOutput<T>,\n TKey,\n T,\n QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>\n> & {\n schema: T\n utils: QueryCollectionUtils<\n InferSchemaOutput<T>,\n TKey,\n InferSchemaInput<T>,\n TError\n >\n}\n\n// Overload for when no schema is provided\nexport function queryCollectionOptions<\n T extends object,\n TError = unknown,\n TQueryKey extends QueryKey = QueryKey,\n TKey extends string | number = string | number,\n>(\n config: QueryCollectionConfig<\n T,\n (context: QueryFunctionContext<any>) => Promise<Array<T>>,\n TError,\n TQueryKey,\n TKey\n > & {\n schema?: never // prohibit schema\n }\n): CollectionConfig<\n T,\n TKey,\n never,\n QueryCollectionUtils<T, TKey, T, TError>\n> & {\n schema?: never // no schema in the result\n utils: QueryCollectionUtils<T, TKey, T, TError>\n}\n\nexport function queryCollectionOptions(\n config: QueryCollectionConfig<Record<string, unknown>>\n): CollectionConfig<\n Record<string, unknown>,\n string | number,\n never,\n QueryCollectionUtils\n> & {\n utils: QueryCollectionUtils\n} {\n const {\n queryKey,\n queryFn,\n select,\n queryClient,\n enabled,\n refetchInterval,\n retry,\n retryDelay,\n staleTime,\n getKey,\n onInsert,\n onUpdate,\n onDelete,\n meta,\n ...baseCollectionConfig\n } = config\n\n // Default to eager sync mode if not provided\n const syncMode = baseCollectionConfig.syncMode ?? `eager`\n\n // Validate required parameters\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryKey) {\n throw new QueryKeyRequiredError()\n }\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryFn) {\n throw new QueryFnRequiredError()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!queryClient) {\n throw new QueryClientRequiredError()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!getKey) {\n throw new GetKeyRequiredError()\n }\n\n /** State object to hold error tracking and observer reference */\n const state: QueryCollectionState = {\n lastError: undefined as any,\n errorCount: 0,\n lastErrorUpdatedAt: 0,\n observers: new Map<\n string,\n QueryObserver<Array<any>, any, Array<any>, Array<any>, any>\n >(),\n }\n\n // hashedQueryKey → queryKey\n const hashToQueryKey = new Map<string, QueryKey>()\n\n // queryKey → Set<RowKey>\n const queryToRows = new Map<string, Set<string | number>>()\n\n // RowKey → Set<queryKey>\n const rowToQueries = new Map<string | number, Set<string>>()\n\n // queryKey → QueryObserver's unsubscribe function\n const unsubscribes = new Map<string, () => void>()\n\n // Helper function to add a row to the internal state\n const addRow = (rowKey: string | number, hashedQueryKey: string) => {\n const rowToQueriesSet = rowToQueries.get(rowKey) || new Set()\n rowToQueriesSet.add(hashedQueryKey)\n rowToQueries.set(rowKey, rowToQueriesSet)\n\n const queryToRowsSet = queryToRows.get(hashedQueryKey) || new Set()\n queryToRowsSet.add(rowKey)\n queryToRows.set(hashedQueryKey, queryToRowsSet)\n }\n\n // Helper function to remove a row from the internal state\n const removeRow = (rowKey: string | number, hashedQuerKey: string) => {\n const rowToQueriesSet = rowToQueries.get(rowKey) || new Set()\n rowToQueriesSet.delete(hashedQuerKey)\n rowToQueries.set(rowKey, rowToQueriesSet)\n\n const queryToRowsSet = queryToRows.get(hashedQuerKey) || new Set()\n queryToRowsSet.delete(rowKey)\n queryToRows.set(hashedQuerKey, queryToRowsSet)\n\n return rowToQueriesSet.size === 0\n }\n\n const internalSync: SyncConfig<any>[`sync`] = (params) => {\n const { begin, write, commit, markReady, collection } = params\n\n // Track whether sync has been started\n let syncStarted = false\n\n const createQueryFromOpts = (\n opts: LoadSubsetOptions = {},\n queryFunction: typeof queryFn = queryFn\n ): true | Promise<void> => {\n // Push the predicates down to the queryKey and queryFn\n let key: QueryKey\n if (typeof queryKey === `function`) {\n // Function-based queryKey: use it to build the key from opts\n key = queryKey(opts)\n } else if (syncMode === `on-demand`) {\n // Static queryKey in on-demand mode: automatically append serialized predicates\n // to create separate cache entries for different predicate combinations\n const serialized = serializeLoadSubsetOptions(opts)\n key = serialized !== undefined ? [...queryKey, serialized] : queryKey\n } else {\n // Static queryKey in eager mode: use as-is\n key = queryKey\n }\n const hashedQueryKey = hashKey(key)\n const extendedMeta = { ...meta, loadSubsetOptions: opts }\n\n if (state.observers.has(hashedQueryKey)) {\n // We already have a query for this queryKey\n // Get the current result and return based on its state\n const observer = state.observers.get(hashedQueryKey)!\n const currentResult = observer.getCurrentResult()\n\n if (currentResult.isSuccess) {\n // Data is already available, return true synchronously\n return true\n } else if (currentResult.isError) {\n // Error already occurred, reject immediately\n return Promise.reject(currentResult.error)\n } else {\n // Query is still loading, wait for the first result\n return new Promise<void>((resolve, reject) => {\n const unsubscribe = observer.subscribe((result) => {\n if (result.isSuccess) {\n unsubscribe()\n resolve()\n } else if (result.isError) {\n unsubscribe()\n reject(result.error)\n }\n })\n })\n }\n }\n\n const observerOptions: QueryObserverOptions<\n Array<any>,\n any,\n Array<any>,\n Array<any>,\n any\n > = {\n queryKey: key,\n queryFn: queryFunction,\n meta: extendedMeta,\n structuralSharing: true,\n notifyOnChangeProps: `all`,\n\n // Only include options that are explicitly defined to allow QueryClient defaultOptions to be used\n ...(enabled !== undefined && { enabled }),\n ...(refetchInterval !== undefined && { refetchInterval }),\n ...(retry !== undefined && { retry }),\n ...(retryDelay !== undefined && { retryDelay }),\n ...(staleTime !== undefined && { staleTime }),\n }\n\n const localObserver = new QueryObserver<\n Array<any>,\n any,\n Array<any>,\n Array<any>,\n any\n >(queryClient, observerOptions)\n\n hashToQueryKey.set(hashedQueryKey, key)\n state.observers.set(hashedQueryKey, localObserver)\n\n // Create a promise that resolves when the query result is first available\n const readyPromise = new Promise<void>((resolve, reject) => {\n const unsubscribe = localObserver.subscribe((result) => {\n if (result.isSuccess) {\n unsubscribe()\n resolve()\n } else if (result.isError) {\n unsubscribe()\n reject(result.error)\n }\n })\n })\n\n // If sync has started or there are subscribers to the collection, subscribe to the query straight away\n // This creates the main subscription that handles data updates\n if (syncStarted || collection.subscriberCount > 0) {\n subscribeToQuery(localObserver, hashedQueryKey)\n }\n\n // Tell tanstack query to GC the query when the subscription is unsubscribed\n // The subscription is unsubscribed when the live query is GCed.\n const subscription = opts.subscription\n subscription?.once(`unsubscribed`, () => {\n queryClient.removeQueries({ queryKey: key, exact: true })\n })\n\n return readyPromise\n }\n\n type UpdateHandler = Parameters<QueryObserver[`subscribe`]>[0]\n\n const makeQueryResultHandler = (queryKey: QueryKey) => {\n const hashedQueryKey = hashKey(queryKey)\n const handleQueryResult: UpdateHandler = (result) => {\n if (result.isSuccess) {\n // Clear error state\n state.lastError = undefined\n state.errorCount = 0\n\n const rawData = result.data\n const newItemsArray = select ? select(rawData) : rawData\n\n if (\n !Array.isArray(newItemsArray) ||\n newItemsArray.some((item) => typeof item !== `object`)\n ) {\n const errorMessage = select\n ? `@tanstack/query-db-collection: select() must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`\n : `@tanstack/query-db-collection: queryFn must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`\n\n console.error(errorMessage)\n return\n }\n\n const currentSyncedItems: Map<string | number, any> = new Map(\n collection._state.syncedData.entries()\n )\n const newItemsMap = new Map<string | number, any>()\n newItemsArray.forEach((item) => {\n const key = getKey(item)\n newItemsMap.set(key, item)\n })\n\n begin()\n\n // Helper function for shallow equality check of objects\n const shallowEqual = (\n obj1: Record<string, any>,\n obj2: Record<string, any>\n ): boolean => {\n // Get all keys from both objects\n const keys1 = Object.keys(obj1)\n const keys2 = Object.keys(obj2)\n\n // If number of keys is different, objects are not equal\n if (keys1.length !== keys2.length) return false\n\n // Check if all keys in obj1 have the same values in obj2\n return keys1.every((key) => {\n // Skip comparing functions and complex objects deeply\n if (typeof obj1[key] === `function`) return true\n return obj1[key] === obj2[key]\n })\n }\n\n currentSyncedItems.forEach((oldItem, key) => {\n const newItem = newItemsMap.get(key)\n if (!newItem) {\n const needToRemove = removeRow(key, hashedQueryKey) // returns true if the row is no longer referenced by any queries\n if (needToRemove) {\n write({ type: `delete`, value: oldItem })\n }\n } else if (\n !shallowEqual(\n oldItem as Record<string, any>,\n newItem as Record<string, any>\n )\n ) {\n // Only update if there are actual differences in the properties\n write({ type: `update`, value: newItem })\n }\n })\n\n newItemsMap.forEach((newItem, key) => {\n addRow(key, hashedQueryKey)\n if (!currentSyncedItems.has(key)) {\n write({ type: `insert`, value: newItem })\n }\n })\n\n commit()\n\n // Mark collection as ready after first successful query result\n markReady()\n } else if (result.isError) {\n if (result.errorUpdatedAt !== state.lastErrorUpdatedAt) {\n state.lastError = result.error\n state.errorCount++\n state.lastErrorUpdatedAt = result.errorUpdatedAt\n }\n\n console.error(\n `[QueryCollection] Error observing query ${String(queryKey)}:`,\n result.error\n )\n\n // Mark collection as ready even on error to avoid blocking apps\n markReady()\n }\n }\n return handleQueryResult\n }\n\n const isSubscribed = (hashedQueryKey: string) => {\n return unsubscribes.has(hashedQueryKey)\n }\n\n const subscribeToQuery = (\n observer: QueryObserver<Array<any>, any, Array<any>, Array<any>, any>,\n hashedQueryKey: string\n ) => {\n if (!isSubscribed(hashedQueryKey)) {\n const queryKey = hashToQueryKey.get(hashedQueryKey)!\n const handleQueryResult = makeQueryResultHandler(queryKey)\n const unsubscribeFn = observer.subscribe(handleQueryResult)\n unsubscribes.set(hashedQueryKey, unsubscribeFn)\n }\n }\n\n const subscribeToQueries = () => {\n state.observers.forEach(subscribeToQuery)\n }\n\n const unsubscribeFromQueries = () => {\n unsubscribes.forEach((unsubscribeFn) => {\n unsubscribeFn()\n })\n unsubscribes.clear()\n }\n\n // Mark that sync has started\n syncStarted = true\n\n // Set up event listener for subscriber changes\n const unsubscribeFromCollectionEvents = collection.on(\n `subscribers:change`,\n ({ subscriberCount }) => {\n if (subscriberCount > 0) {\n subscribeToQueries()\n } else if (subscriberCount === 0) {\n unsubscribeFromQueries()\n }\n }\n )\n\n // If syncMode is eager, create the initial query without any predicates\n if (syncMode === `eager`) {\n // Catch any errors to prevent unhandled rejections\n const initialResult = createQueryFromOpts({})\n if (initialResult instanceof Promise) {\n initialResult.catch(() => {\n // Errors are already handled by the query result handler\n })\n }\n } else {\n // In on-demand mode, mark ready immediately since there's no initial query\n markReady()\n }\n\n // Always subscribe when sync starts (this could be from preload(), startSync config, or first subscriber)\n // We'll dynamically unsubscribe/resubscribe based on subscriber count to maintain staleTime behavior\n subscribeToQueries()\n\n // Ensure we process any existing query data (QueryObserver doesn't invoke its callback automatically with initial state)\n state.observers.forEach((observer, hashedQueryKey) => {\n const queryKey = hashToQueryKey.get(hashedQueryKey)!\n const handleQueryResult = makeQueryResultHandler(queryKey)\n handleQueryResult(observer.getCurrentResult())\n })\n\n // Subscribe to the query client's cache to handle queries that are GCed by tanstack query\n const unsubscribeQueryCache = queryClient\n .getQueryCache()\n .subscribe((event) => {\n const hashedKey = event.query.queryHash\n if (event.type === `removed`) {\n cleanupQuery(hashedKey)\n }\n })\n\n function cleanupQuery(hashedQueryKey: string) {\n // Unsubscribe from the query's observer\n unsubscribes.get(hashedQueryKey)?.()\n\n // Get all the rows that are in the result of this query\n const rowKeys = queryToRows.get(hashedQueryKey) ?? new Set()\n\n // Remove the query from these rows\n rowKeys.forEach((rowKey) => {\n const queries = rowToQueries.get(rowKey) // set of queries that reference this row\n if (queries && queries.size > 0) {\n queries.delete(hashedQueryKey)\n if (queries.size === 0) {\n // Reference count dropped to 0, we can GC the row\n rowToQueries.delete(rowKey)\n\n if (collection.has(rowKey)) {\n begin()\n write({ type: `delete`, value: collection.get(rowKey) })\n commit()\n }\n }\n }\n })\n\n // Remove the query from the internal state\n unsubscribes.delete(hashedQueryKey)\n state.observers.delete(hashedQueryKey)\n queryToRows.delete(hashedQueryKey)\n hashToQueryKey.delete(hashedQueryKey)\n }\n\n const cleanup = async () => {\n unsubscribeFromCollectionEvents()\n unsubscribeFromQueries()\n\n const queryKeys = [...hashToQueryKey.values()]\n\n hashToQueryKey.clear()\n queryToRows.clear()\n rowToQueries.clear()\n state.observers.clear()\n unsubscribeQueryCache()\n\n await Promise.all(\n queryKeys.map(async (queryKey) => {\n await queryClient.cancelQueries({ queryKey })\n queryClient.removeQueries({ queryKey })\n })\n )\n }\n\n // Create deduplicated loadSubset wrapper for non-eager modes\n // This prevents redundant snapshot requests when multiple concurrent\n // live queries request overlapping or subset predicates\n const loadSubsetDedupe =\n syncMode === `eager` ? undefined : createQueryFromOpts\n\n return {\n loadSubset: loadSubsetDedupe,\n cleanup,\n }\n }\n\n /**\n * Refetch the query data\n *\n * Uses queryObserver.refetch() because:\n * - Bypasses `enabled: false` to support manual/imperative refetch patterns (e.g., button-triggered fetch)\n * - Ensures clearError() works even when enabled: false\n * - Always refetches THIS specific collection (exact targeting via observer)\n * - Respects retry, retryDelay, and other observer options\n *\n * This matches TanStack Query's hook behavior where refetch() bypasses enabled: false.\n * See: https://tanstack.com/query/latest/docs/framework/react/guides/disabling-queries\n *\n * Used by both:\n * - utils.refetch() - for explicit user-triggered refetches\n * - Internal handlers (onInsert/onUpdate/onDelete) - after mutations to get fresh data\n *\n * @returns Promise that resolves when the refetch is complete, with QueryObserverResult\n */\n const refetch: RefetchFn = async (opts) => {\n const queryKeys = [...hashToQueryKey.values()]\n const refetchPromises = queryKeys.map((queryKey) => {\n const queryObserver = state.observers.get(hashKey(queryKey))!\n return queryObserver.refetch({\n throwOnError: opts?.throwOnError,\n })\n })\n\n await Promise.all(refetchPromises)\n }\n\n // Create write context for manual write operations\n let writeContext: {\n collection: any\n queryClient: QueryClient\n queryKey: Array<unknown>\n getKey: (item: any) => string | number\n begin: () => void\n write: (message: Omit<ChangeMessage<any>, `key`>) => void\n commit: () => void\n } | null = null\n\n // Enhanced internalSync that captures write functions for manual use\n const enhancedInternalSync: SyncConfig<any>[`sync`] = (params) => {\n const { begin, write, commit, collection } = params\n\n // Store references for manual write operations\n writeContext = {\n collection,\n queryClient,\n queryKey: queryKey as unknown as Array<unknown>,\n getKey: getKey as (item: any) => string | number,\n begin,\n write,\n commit,\n }\n\n // Call the original internalSync logic\n return internalSync(params)\n }\n\n // Create write utils using the manual-sync module\n const writeUtils = createWriteUtils<any, string | number, any>(\n () => writeContext\n )\n\n // Create wrapper handlers for direct persistence operations that handle refetching\n const wrappedOnInsert = onInsert\n ? async (params: InsertMutationFnParams<any>) => {\n const handlerResult = (await onInsert(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnUpdate = onUpdate\n ? async (params: UpdateMutationFnParams<any>) => {\n const handlerResult = (await onUpdate(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnDelete = onDelete\n ? async (params: DeleteMutationFnParams<any>) => {\n const handlerResult = (await onDelete(params)) ?? {}\n const shouldRefetch =\n (handlerResult as { refetch?: boolean }).refetch !== false\n\n if (shouldRefetch) {\n await refetch()\n }\n\n return handlerResult\n }\n : undefined\n\n // Create utils instance with state and dependencies passed explicitly\n const utils: any = new QueryCollectionUtilsImpl(state, refetch, writeUtils)\n\n return {\n ...baseCollectionConfig,\n getKey,\n syncMode,\n sync: { sync: enhancedInternalSync },\n onInsert: wr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iB,MAAM,SAAS,MAAM,KAAM,CAAA;AAClD,UAAM,gBACH,cAAwC,YAAY;AAEvD,QAAI,eAAe;AACjB,YAAM,QAAA;AAAA,IACR;AAEA,WAAO;AAAA,EACT,IACA;AAEJ,QAAM,kBAAkB,WACpB,OAAO,WAAwC;AAC7C,UAAM,gBAAiB,MAAM,SAAS,MAAM,KAAM,CAAA;AAClD,UAAM,gBACH,cAAwC,YAAY;AAEvD,QAAI,eAAe;AACjB,YAAM,QAAA;AAAA,IACR;AAEA,WAAO;AAAA,EACT,IACA;AAGJ,QAAM,QAAa,IAAI,yBAAyB,OAAO,SAAS,UAAU;AAE1E,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA,MAAM,EAAE,MAAM,qBAAA;AAAA,IACd,UAAU;AAAA,IACV,UAAU;AAAA,IACV,UAAU;AAAA,IACV;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
function serializeLoadSubsetOptions(options) {
|
|
2
|
+
if (!options) {
|
|
3
|
+
return void 0;
|
|
4
|
+
}
|
|
5
|
+
const result = {};
|
|
6
|
+
if (options.where) {
|
|
7
|
+
result.where = serializeExpression(options.where);
|
|
8
|
+
}
|
|
9
|
+
if (options.orderBy?.length) {
|
|
10
|
+
result.orderBy = options.orderBy.map((clause) => {
|
|
11
|
+
const baseOrderBy = {
|
|
12
|
+
expression: serializeExpression(clause.expression),
|
|
13
|
+
direction: clause.compareOptions.direction,
|
|
14
|
+
nulls: clause.compareOptions.nulls,
|
|
15
|
+
stringSort: clause.compareOptions.stringSort
|
|
16
|
+
};
|
|
17
|
+
if (clause.compareOptions.stringSort === `locale`) {
|
|
18
|
+
return {
|
|
19
|
+
...baseOrderBy,
|
|
20
|
+
locale: clause.compareOptions.locale,
|
|
21
|
+
localeOptions: clause.compareOptions.localeOptions
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return baseOrderBy;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (options.limit !== void 0) {
|
|
28
|
+
result.limit = options.limit;
|
|
29
|
+
}
|
|
30
|
+
return Object.keys(result).length === 0 ? void 0 : JSON.stringify(result);
|
|
31
|
+
}
|
|
32
|
+
function serializeExpression(expr) {
|
|
33
|
+
if (!expr) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
switch (expr.type) {
|
|
37
|
+
case `val`:
|
|
38
|
+
return {
|
|
39
|
+
type: `val`,
|
|
40
|
+
value: serializeValue(expr.value)
|
|
41
|
+
};
|
|
42
|
+
case `ref`:
|
|
43
|
+
return {
|
|
44
|
+
type: `ref`,
|
|
45
|
+
path: [...expr.path]
|
|
46
|
+
};
|
|
47
|
+
case `func`:
|
|
48
|
+
return {
|
|
49
|
+
type: `func`,
|
|
50
|
+
name: expr.name,
|
|
51
|
+
args: expr.args.map((arg) => serializeExpression(arg))
|
|
52
|
+
};
|
|
53
|
+
default:
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function serializeValue(value) {
|
|
58
|
+
if (value === void 0) {
|
|
59
|
+
return { __type: `undefined` };
|
|
60
|
+
}
|
|
61
|
+
if (typeof value === `number`) {
|
|
62
|
+
if (Number.isNaN(value)) {
|
|
63
|
+
return { __type: `nan` };
|
|
64
|
+
}
|
|
65
|
+
if (value === Number.POSITIVE_INFINITY) {
|
|
66
|
+
return { __type: `infinity`, sign: 1 };
|
|
67
|
+
}
|
|
68
|
+
if (value === Number.NEGATIVE_INFINITY) {
|
|
69
|
+
return { __type: `infinity`, sign: -1 };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (value === null || typeof value === `string` || typeof value === `number` || typeof value === `boolean`) {
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
if (value instanceof Date) {
|
|
76
|
+
return { __type: `date`, value: value.toJSON() };
|
|
77
|
+
}
|
|
78
|
+
if (Array.isArray(value)) {
|
|
79
|
+
return value.map((item) => serializeValue(item));
|
|
80
|
+
}
|
|
81
|
+
if (typeof value === `object`) {
|
|
82
|
+
return Object.fromEntries(
|
|
83
|
+
Object.entries(value).map(([key, val]) => [
|
|
84
|
+
key,
|
|
85
|
+
serializeValue(val)
|
|
86
|
+
])
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
serializeLoadSubsetOptions
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=serialization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialization.js","sources":["../../src/serialization.ts"],"sourcesContent":["import type { IR, LoadSubsetOptions } from \"@tanstack/db\"\n\n/**\n * Serializes LoadSubsetOptions into a stable, hashable format for query keys\n * @internal\n */\nexport function serializeLoadSubsetOptions(\n options: LoadSubsetOptions | undefined\n): string | undefined {\n if (!options) {\n return undefined\n }\n\n const result: Record<string, unknown> = {}\n\n if (options.where) {\n result.where = serializeExpression(options.where)\n }\n\n if (options.orderBy?.length) {\n result.orderBy = options.orderBy.map((clause) => {\n const baseOrderBy = {\n expression: serializeExpression(clause.expression),\n direction: clause.compareOptions.direction,\n nulls: clause.compareOptions.nulls,\n stringSort: clause.compareOptions.stringSort,\n }\n\n // Handle locale-specific options when stringSort is 'locale'\n if (clause.compareOptions.stringSort === `locale`) {\n return {\n ...baseOrderBy,\n locale: clause.compareOptions.locale,\n localeOptions: clause.compareOptions.localeOptions,\n }\n }\n\n return baseOrderBy\n })\n }\n\n if (options.limit !== undefined) {\n result.limit = options.limit\n }\n\n return Object.keys(result).length === 0 ? undefined : JSON.stringify(result)\n}\n\n/**\n * Recursively serializes an IR expression for stable hashing\n * @internal\n */\nfunction serializeExpression(expr: IR.BasicExpression | undefined): unknown {\n if (!expr) {\n return null\n }\n\n switch (expr.type) {\n case `val`:\n return {\n type: `val`,\n value: serializeValue(expr.value),\n }\n case `ref`:\n return {\n type: `ref`,\n path: [...expr.path],\n }\n case `func`:\n return {\n type: `func`,\n name: expr.name,\n args: expr.args.map((arg) => serializeExpression(arg)),\n }\n default:\n return null\n }\n}\n\n/**\n * Serializes special JavaScript values (undefined, NaN, Infinity, Date)\n * @internal\n */\nfunction serializeValue(value: unknown): unknown {\n if (value === undefined) {\n return { __type: `undefined` }\n }\n\n if (typeof value === `number`) {\n if (Number.isNaN(value)) {\n return { __type: `nan` }\n }\n if (value === Number.POSITIVE_INFINITY) {\n return { __type: `infinity`, sign: 1 }\n }\n if (value === Number.NEGATIVE_INFINITY) {\n return { __type: `infinity`, sign: -1 }\n }\n }\n\n if (\n value === null ||\n typeof value === `string` ||\n typeof value === `number` ||\n typeof value === `boolean`\n ) {\n return value\n }\n\n if (value instanceof Date) {\n return { __type: `date`, value: value.toJSON() }\n }\n\n if (Array.isArray(value)) {\n return value.map((item) => serializeValue(item))\n }\n\n if (typeof value === `object`) {\n return Object.fromEntries(\n Object.entries(value as Record<string, unknown>).map(([key, val]) => [\n key,\n serializeValue(val),\n ])\n )\n }\n\n return value\n}\n"],"names":[],"mappings":"AAMO,SAAS,2BACd,SACoB;AACpB,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,QAAM,SAAkC,CAAA;AAExC,MAAI,QAAQ,OAAO;AACjB,WAAO,QAAQ,oBAAoB,QAAQ,KAAK;AAAA,EAClD;AAEA,MAAI,QAAQ,SAAS,QAAQ;AAC3B,WAAO,UAAU,QAAQ,QAAQ,IAAI,CAAC,WAAW;AAC/C,YAAM,cAAc;AAAA,QAClB,YAAY,oBAAoB,OAAO,UAAU;AAAA,QACjD,WAAW,OAAO,eAAe;AAAA,QACjC,OAAO,OAAO,eAAe;AAAA,QAC7B,YAAY,OAAO,eAAe;AAAA,MAAA;AAIpC,UAAI,OAAO,eAAe,eAAe,UAAU;AACjD,eAAO;AAAA,UACL,GAAG;AAAA,UACH,QAAQ,OAAO,eAAe;AAAA,UAC9B,eAAe,OAAO,eAAe;AAAA,QAAA;AAAA,MAEzC;AAEA,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AAA,EAC/C;AAEA,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,WAAO,MAAM,IAAI,CAAC,SAAS,eAAe,IAAI,CAAC;AAAA,EACjD;AAEA,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO,OAAO;AAAA,MACZ,OAAO,QAAQ,KAAgC,EAAE,IAAI,CAAC,CAAC,KAAK,GAAG,MAAM;AAAA,QACnE;AAAA,QACA,eAAe,GAAG;AAAA,MAAA,CACnB;AAAA,IAAA;AAAA,EAEL;AAEA,SAAO;AACT;"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-db-collection",
|
|
3
3
|
"description": "TanStack Query collection for TanStack DB",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@standard-schema/spec": "^1.0.0"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"@tanstack/query-core": "^5.90.
|
|
9
|
+
"@tanstack/query-core": "^5.90.10",
|
|
10
10
|
"@vitest/coverage-istanbul": "^3.2.4",
|
|
11
|
-
"@tanstack/db": "0.5.
|
|
11
|
+
"@tanstack/db": "0.5.3"
|
|
12
12
|
},
|
|
13
13
|
"exports": {
|
|
14
14
|
".": {
|
package/src/query.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { QueryObserver, hashKey } from "@tanstack/query-core"
|
|
2
|
-
import { DeduplicatedLoadSubset } from "@tanstack/db"
|
|
3
2
|
import {
|
|
4
3
|
GetKeyRequiredError,
|
|
5
4
|
QueryClientRequiredError,
|
|
@@ -7,6 +6,7 @@ import {
|
|
|
7
6
|
QueryKeyRequiredError,
|
|
8
7
|
} from "./errors"
|
|
9
8
|
import { createWriteUtils } from "./manual-sync"
|
|
9
|
+
import { serializeLoadSubsetOptions } from "./serialization"
|
|
10
10
|
import type {
|
|
11
11
|
BaseCollectionConfig,
|
|
12
12
|
ChangeMessage,
|
|
@@ -627,7 +627,19 @@ export function queryCollectionOptions(
|
|
|
627
627
|
queryFunction: typeof queryFn = queryFn
|
|
628
628
|
): true | Promise<void> => {
|
|
629
629
|
// Push the predicates down to the queryKey and queryFn
|
|
630
|
-
|
|
630
|
+
let key: QueryKey
|
|
631
|
+
if (typeof queryKey === `function`) {
|
|
632
|
+
// Function-based queryKey: use it to build the key from opts
|
|
633
|
+
key = queryKey(opts)
|
|
634
|
+
} else if (syncMode === `on-demand`) {
|
|
635
|
+
// Static queryKey in on-demand mode: automatically append serialized predicates
|
|
636
|
+
// to create separate cache entries for different predicate combinations
|
|
637
|
+
const serialized = serializeLoadSubsetOptions(opts)
|
|
638
|
+
key = serialized !== undefined ? [...queryKey, serialized] : queryKey
|
|
639
|
+
} else {
|
|
640
|
+
// Static queryKey in eager mode: use as-is
|
|
641
|
+
key = queryKey
|
|
642
|
+
}
|
|
631
643
|
const hashedQueryKey = hashKey(key)
|
|
632
644
|
const extendedMeta = { ...meta, loadSubsetOptions: opts }
|
|
633
645
|
|
|
@@ -824,21 +836,6 @@ export function queryCollectionOptions(
|
|
|
824
836
|
return handleQueryResult
|
|
825
837
|
}
|
|
826
838
|
|
|
827
|
-
// This function is called when a loadSubset call is deduplicated
|
|
828
|
-
// meaning that we have all the data locally available to answer the query
|
|
829
|
-
// so we execute the query locally
|
|
830
|
-
const createLocalQuery = (opts: LoadSubsetOptions) => {
|
|
831
|
-
const queryFn = ({ meta }: QueryFunctionContext<any>) => {
|
|
832
|
-
const inserts = collection.currentStateAsChanges(
|
|
833
|
-
meta!.loadSubsetOptions as LoadSubsetOptions
|
|
834
|
-
)!
|
|
835
|
-
const data = inserts.map(({ value }) => value)
|
|
836
|
-
return Promise.resolve(data)
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
createQueryFromOpts(opts, queryFn)
|
|
840
|
-
}
|
|
841
|
-
|
|
842
839
|
const isSubscribed = (hashedQueryKey: string) => {
|
|
843
840
|
return unsubscribes.has(hashedQueryKey)
|
|
844
841
|
}
|
|
@@ -972,15 +969,10 @@ export function queryCollectionOptions(
|
|
|
972
969
|
// This prevents redundant snapshot requests when multiple concurrent
|
|
973
970
|
// live queries request overlapping or subset predicates
|
|
974
971
|
const loadSubsetDedupe =
|
|
975
|
-
syncMode === `eager`
|
|
976
|
-
? undefined
|
|
977
|
-
: new DeduplicatedLoadSubset({
|
|
978
|
-
loadSubset: createQueryFromOpts,
|
|
979
|
-
onDeduplicate: createLocalQuery,
|
|
980
|
-
})
|
|
972
|
+
syncMode === `eager` ? undefined : createQueryFromOpts
|
|
981
973
|
|
|
982
974
|
return {
|
|
983
|
-
loadSubset: loadSubsetDedupe
|
|
975
|
+
loadSubset: loadSubsetDedupe,
|
|
984
976
|
cleanup,
|
|
985
977
|
}
|
|
986
978
|
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { IR, LoadSubsetOptions } from "@tanstack/db"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Serializes LoadSubsetOptions into a stable, hashable format for query keys
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export function serializeLoadSubsetOptions(
|
|
8
|
+
options: LoadSubsetOptions | undefined
|
|
9
|
+
): string | undefined {
|
|
10
|
+
if (!options) {
|
|
11
|
+
return undefined
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const result: Record<string, unknown> = {}
|
|
15
|
+
|
|
16
|
+
if (options.where) {
|
|
17
|
+
result.where = serializeExpression(options.where)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (options.orderBy?.length) {
|
|
21
|
+
result.orderBy = options.orderBy.map((clause) => {
|
|
22
|
+
const baseOrderBy = {
|
|
23
|
+
expression: serializeExpression(clause.expression),
|
|
24
|
+
direction: clause.compareOptions.direction,
|
|
25
|
+
nulls: clause.compareOptions.nulls,
|
|
26
|
+
stringSort: clause.compareOptions.stringSort,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Handle locale-specific options when stringSort is 'locale'
|
|
30
|
+
if (clause.compareOptions.stringSort === `locale`) {
|
|
31
|
+
return {
|
|
32
|
+
...baseOrderBy,
|
|
33
|
+
locale: clause.compareOptions.locale,
|
|
34
|
+
localeOptions: clause.compareOptions.localeOptions,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return baseOrderBy
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (options.limit !== undefined) {
|
|
43
|
+
result.limit = options.limit
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return Object.keys(result).length === 0 ? undefined : JSON.stringify(result)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Recursively serializes an IR expression for stable hashing
|
|
51
|
+
* @internal
|
|
52
|
+
*/
|
|
53
|
+
function serializeExpression(expr: IR.BasicExpression | undefined): unknown {
|
|
54
|
+
if (!expr) {
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
switch (expr.type) {
|
|
59
|
+
case `val`:
|
|
60
|
+
return {
|
|
61
|
+
type: `val`,
|
|
62
|
+
value: serializeValue(expr.value),
|
|
63
|
+
}
|
|
64
|
+
case `ref`:
|
|
65
|
+
return {
|
|
66
|
+
type: `ref`,
|
|
67
|
+
path: [...expr.path],
|
|
68
|
+
}
|
|
69
|
+
case `func`:
|
|
70
|
+
return {
|
|
71
|
+
type: `func`,
|
|
72
|
+
name: expr.name,
|
|
73
|
+
args: expr.args.map((arg) => serializeExpression(arg)),
|
|
74
|
+
}
|
|
75
|
+
default:
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Serializes special JavaScript values (undefined, NaN, Infinity, Date)
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
function serializeValue(value: unknown): unknown {
|
|
85
|
+
if (value === undefined) {
|
|
86
|
+
return { __type: `undefined` }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (typeof value === `number`) {
|
|
90
|
+
if (Number.isNaN(value)) {
|
|
91
|
+
return { __type: `nan` }
|
|
92
|
+
}
|
|
93
|
+
if (value === Number.POSITIVE_INFINITY) {
|
|
94
|
+
return { __type: `infinity`, sign: 1 }
|
|
95
|
+
}
|
|
96
|
+
if (value === Number.NEGATIVE_INFINITY) {
|
|
97
|
+
return { __type: `infinity`, sign: -1 }
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (
|
|
102
|
+
value === null ||
|
|
103
|
+
typeof value === `string` ||
|
|
104
|
+
typeof value === `number` ||
|
|
105
|
+
typeof value === `boolean`
|
|
106
|
+
) {
|
|
107
|
+
return value
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (value instanceof Date) {
|
|
111
|
+
return { __type: `date`, value: value.toJSON() }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (Array.isArray(value)) {
|
|
115
|
+
return value.map((item) => serializeValue(item))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (typeof value === `object`) {
|
|
119
|
+
return Object.fromEntries(
|
|
120
|
+
Object.entries(value as Record<string, unknown>).map(([key, val]) => [
|
|
121
|
+
key,
|
|
122
|
+
serializeValue(val),
|
|
123
|
+
])
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return value
|
|
128
|
+
}
|