Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

query-driven-sync.tsx

96 lines4.4 KB

1import { ClientOnly, createFileRoute } from "@tanstack/react-router";
2​
3import { Callout } from "@/components/guide/callout";
4import { DeepDivePage } from "@/components/guide/deep-dive-page";
5import { OnDemandDemo } from "@/components/guide/on-demand-demo";
6import { SourceSnippet } from "@/components/guide/source-snippet";
7import { SNIPPETS } from "@/guide/snippets";
8​
9export const Route = createFileRoute("/deep-dives/query-driven-sync")({
10 head: () => ({ meta: [{ title: "Query-driven sync — Try TanStack DB" }] }),
11 component: QueryDrivenSyncPage,
12});
13​
14function QueryDrivenSyncPage() {
15 return (
16 <DeepDivePage
17 title="Query-driven sync"
18 intro={
19 <p>Load only the rows your live queries ask for, instead of the whole table up front.</p>
20 }
21 >
22 <p>
23 The Notes app loads every note when it starts. That's the default,{" "}
24 <code>syncMode: "eager"</code>, and it's the right choice for small tables, up to about ten
25 thousand rows. For bigger ones, <code>syncMode: "on-demand"</code> loads nothing up front.
26 Each live query tells the collection which rows it needs, and the collection loads just
27 those.
28 </p>
29​
30 <h3>The collection</h3>
31 <p>
32 Here's a second notes collection in on-demand mode. When a live query needs rows, its{" "}
33 <code>where</code>, <code>orderBy</code> and <code>limit</code> reach <code>queryFn</code>{" "}
34 as <code>meta.loadSubsetOptions</code>. <code>parseLoadSubsetOptions</code> turns the{" "}
35 <code>where</code> into a flat list, like{" "}
36 <code>{'[{ field: ["folderId"], operator: "eq", value: "work" }]'}</code>, and{" "}
37 <code>queryFn</code> turns the parts the API understands into a URL.
38 </p>
39 <SourceSnippet snippet={SNIPPETS.onDemandCollection} />
40 <p>
41 You don't need a query key per folder: in on-demand mode, the collection adds each subset to{" "}
42 <code>queryKey</code> itself, so every folder gets its own entry in the query cache.
43 </p>
44​
45 <h3>The live query</h3>
46 <p>A live query over that collection decides what gets loaded:</p>
47 <SourceSnippet snippet={SNIPPETS.folderNotesQuery} />
48 <p>
49 The API can only filter by folder, so <code>isNull(note.deletedAt)</code> never reaches the
50 server. That's fine: the live query still applies all of its conditions to the rows it gets.
51 The server may send more rows than the query needs, never fewer.
52 </p>
53​
54 <h3>Try it</h3>
55 <p>
56 Pick a folder. The live query asks for its notes, and the collection sends a request for
57 just that folder. Going back to a folder sends its request again.
58 </p>
59 <ClientOnly fallback={<div className="my-5 h-48 rounded-xl border bg-muted/40" />}>
60 <OnDemandDemo />
61 </ClientOnly>
62​
63 <h3>Pitfalls</h3>
64 <ul>
65 <li>
66 <strong>Return every matching row.</strong> What <code>queryFn</code> returns is the
67 complete result for that subset. A row it leaves out counts as gone for that query.
68 </li>
69 <li>
70 <strong>Know what the parser handles.</strong> <code>parseLoadSubsetOptions</code>{" "}
71 understands comparisons joined with <code>and</code> (<code>eq</code>, <code>gt</code>,{" "}
72 <code>gte</code>, <code>lt</code>, <code>lte</code>, <code>in</code>), null checks, and{" "}
73 <code>not</code> around those. It throws for <code>or</code>, <code>like</code> and other
74 functions. For those, use <code>parseWhereExpression</code> with your own handlers.
75 </li>
76 <li>
77 <strong>Preload the query, not the collection.</strong> <code>collection.preload()</code>{" "}
78 does nothing in on-demand mode. Preload the live query that needs the data instead.
79 </li>
80 <li>
81 <strong>No initial data.</strong> <code>initialData</code> isn't supported in on-demand
82 mode and throws.
83 </li>
84 <li>
85 <strong>Custom query keys need the base prefix.</strong> If <code>queryKey</code> is a
86 function, every key it returns must start with the base key, <code>queryKey({"{}"})</code>
87 , or cache updates miss them.
88 </li>
89 </ul>
90 <Callout variant="note">
91 This collection is only used on this page. Its notes come from the same fake API as the
92 Notes app, so edits you made there show up here.
93 </Callout>
94 </DeepDivePage>
95 );