Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/open-ideas-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/query-db-collection': patch
---

Forward structuralSharing option to QueryObserver
2 changes: 1 addition & 1 deletion packages/db/src/query/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import type {
import type {
CompareOptions,
Context,
GetResult,
FunctionalHavingRow,
GetResult,
GroupByCallback,
JoinOnCallback,
MergeContextForJoinCallback,
Expand Down
10 changes: 9 additions & 1 deletion packages/query-db-collection/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export interface QueryCollectionConfig<
Array<T>,
TQueryKey
>[`staleTime`]
structuralSharing?: QueryObserverOptions<
Array<T>,
TError,
Array<T>,
Array<T>,
TQueryKey
>['structuralSharing']

/**
* Metadata to pass to the query.
Expand Down Expand Up @@ -543,6 +550,7 @@ export function queryCollectionOptions(
onUpdate,
onDelete,
meta,
structuralSharing,
...baseCollectionConfig
} = config

Expand Down Expand Up @@ -717,7 +725,6 @@ export function queryCollectionOptions(
queryKey: key,
queryFn: queryFunction,
meta: extendedMeta,
structuralSharing: true,
notifyOnChangeProps: `all`,

// Only include options that are explicitly defined to allow QueryClient defaultOptions to be used
Expand All @@ -726,6 +733,7 @@ export function queryCollectionOptions(
...(retry !== undefined && { retry }),
...(retryDelay !== undefined && { retryDelay }),
...(staleTime !== undefined && { staleTime }),
...(structuralSharing !== undefined && { structuralSharing }),
}

const localObserver = new QueryObserver<
Expand Down
26 changes: 26 additions & 0 deletions packages/query-db-collection/tests/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3500,6 +3500,32 @@ describe(`QueryCollection`, () => {
// Clean up
customQueryClient.clear()
})

it(`should forward structuralSharing option to QueryObserver`, async () => {
const queryKey = [`structuralSharingTest`]
const queryFn = vi.fn().mockResolvedValue([])

const options = queryCollectionOptions({
id: `test`,
queryClient,
queryKey,
queryFn,
getKey,
structuralSharing: false,
startSync: true,
})

createCollection(options)

await vi.waitFor(() => {
expect(queryFn).toHaveBeenCalled()
})

const query = queryClient.getQueryCache().find({ queryKey })

expect(query).toBeDefined()
expect(query!.options.structuralSharing).toBe(false)
})
})

describe(`Query Garbage Collection`, () => {
Expand Down