You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto append to static queryKey when in on-demand mode for Query Collection (#800)
* feat: automatically append predicates to static queryKey in on-demand mode
When using a static queryKey with syncMode: 'on-demand', the system now
automatically appends serialized LoadSubsetOptions to create unique cache
keys for different predicate combinations.
This fixes an issue where static queryKeys in on-demand mode would cause
all live queries with different predicates to share the same cache entry,
defeating the purpose of predicate push-down.
Changes:
- Added serialization utilities for LoadSubsetOptions (serializeLoadSubsetOptions,
serializeExpression, serializeValue)
- Modified createQueryFromOpts to automatically append serialized predicates
when queryKey is static and syncMode is 'on-demand'
- Function-based queryKeys continue to work as before
- Eager mode with static queryKeys unchanged (no automatic serialization)
Tests:
- Added comprehensive test suite for static queryKey with on-demand mode
- Tests verify different predicates create separate cache entries
- Tests verify identical predicates reuse the same cache entry
- Tests verify eager mode behavior unchanged
- All existing tests pass
* chore: add changeset for static queryKey on-demand mode fix
* chore: update changeset to patch instead of minor
* chore: format changeset with prettier
* refactor: address PR review feedback
Addresses Kevin's review comments:
1. Move serialization functions to dedicated utility file
- Created src/serialization.ts with serializeLoadSubsetOptions,
serializeExpression, and serializeValue functions
- Keeps query.ts focused on query logic
2. Fix return type and use undefined instead of null
- Changed serializeLoadSubsetOptions return type from `unknown` to
`string | undefined`
- Returns undefined instead of null for empty options
- Updated usage to conditionally append only when serialized result
is not undefined
3. Add missing CompareOptions properties to orderBy serialization
- Now includes stringSort, locale, and localeOptions properties
- Properly handles the StringCollationConfig union type with
conditional serialization for locale-specific options
All runtime tests pass (65/65 in query.test.ts).
---------
Co-authored-by: Claude <noreply@anthropic.com>
Automatically append predicates to static queryKey in on-demand mode.
6
+
7
+
When using a static `queryKey` with `syncMode: 'on-demand'`, the system now automatically appends serialized LoadSubsetOptions to create unique cache keys for different predicate combinations. This fixes an issue where all live queries with different predicates would share the same TanStack Query cache entry, causing data to be overwritten.
8
+
9
+
**Before:**
10
+
11
+
```typescript
12
+
// This would cause conflicts between different queries
With different live queries filtering by `category='A'` and `category='B'`, both would share the same cache key `['products']`, causing the last query to overwrite the first.
24
+
25
+
**After:**
26
+
Static queryKeys now work correctly in on-demand mode! The system automatically creates unique cache keys:
27
+
28
+
- Query with `category='A'` → `['products', '{"where":{...A...}}']`
29
+
- Query with `category='B'` → `['products', '{"where":{...B...}}']`
30
+
31
+
**Key behaviors:**
32
+
33
+
- ✅ Static queryKeys now work correctly with on-demand mode (automatic serialization)
34
+
- ✅ Function-based queryKeys continue to work as before (no change)
35
+
- ✅ Eager mode with static queryKeys unchanged (no automatic serialization)
36
+
- ✅ Identical predicates correctly reuse the same cache entry
37
+
38
+
This makes the documentation example work correctly without requiring users to manually implement function-based queryKeys for predicate push-down.
0 commit comments