| 563 | } |
| 564 | |
| 565 | export function queryCollectionOptions( |
| 566 | config: QueryCollectionConfig< |
| 567 | Record<string, unknown>, |
| 568 | (context: QueryFunctionContext<any>) => any |
| 569 | >, |
| 570 | ): CollectionConfig< |
| 571 | Record<string, unknown>, |
| 572 | string | number, |
| 573 | never, |
| 574 | QueryCollectionUtils |
| 575 | > & { |
| 576 | utils: QueryCollectionUtils |
| 577 | } { |
| 578 | const { |
| 579 | queryKey, |
| 580 | queryFn, |
| 581 | select, |
| 582 | queryClient, |
| 583 | enabled, |
| 584 | refetchInterval, |
| 585 | retry, |
| 586 | retryDelay, |
| 587 | staleTime, |
| 588 | gcTime, |
| 589 | persistedGcTime, |
| 590 | getKey, |
| 591 | onInsert, |
| 592 | onUpdate, |
| 593 | onDelete, |
| 594 | meta, |
| 595 | ...baseCollectionConfig |
| 596 | } = config |
| 597 | |
| 598 | // Default to eager sync mode if not provided |
| 599 | const syncMode = baseCollectionConfig.syncMode ?? `eager` |
| 600 | |
| 601 | // Compute the base query key once for cache lookups. |
| 602 | // All derived keys (from on-demand predicates or function-based queryKey) must |
| 603 | // share this prefix so that queryCache.findAll({ queryKey: baseKey }) can find them. |
| 604 | const baseKey: QueryKey = |
| 605 | typeof queryKey === `function` |
| 606 | ? (queryKey({}) as unknown as QueryKey) |
| 607 | : (queryKey as unknown as QueryKey) |
| 608 | |
| 609 | /** |
| 610 | * Validates that a derived query key extends the base key prefix. |
| 611 | * TanStack Query uses prefix matching in findAll(), so all keys for this collection |
| 612 | * must start with baseKey for stale cache updates to work correctly. |
| 613 | */ |
| 614 | const validateQueryKeyPrefix = (key: QueryKey): void => { |
| 615 | if (typeof queryKey !== `function`) return |
| 616 | const isValidPrefix = |
| 617 | key.length >= baseKey.length && |
| 618 | baseKey.every((segment, i) => deepEquals(segment, key[i])) |
| 619 | if (!isValidPrefix) { |
| 620 | console.warn( |
| 621 | `[QueryCollection] queryKey function must return keys that extend the base key prefix. ` + |
| 622 | `Base: ${JSON.stringify(baseKey)}, Got: ${JSON.stringify(key)}. ` + |