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

collections.ts

99 lines3.6 KB

1import { createCollection } from "@tanstack/react-db";
2import type { Transaction } from "@tanstack/react-db";
3import { queryCollectionOptions } from "@tanstack/query-db-collection";
4​
5import { api } from "@/api/client";
6​
7import { queryClient } from "./query-client";
8import { inOrder, saveEach } from "./write-queue";
9import { accountSchema, folderSchema, noteSchema } from "./schema";
10import type { Account, Folder, Note } from "./schema";
11​
12/**
13 * The app's data: three query collections over the REST API. Each one loads
14 * its endpoint with `queryFn`, and its handlers send optimistic changes to the
15 * server, one record at a time:
16 *
17 * - The server's response is written into the collection as confirmed data,
18 * so the collection holds exactly what the server saved.
19 * - A rejected request (4xx or 5xx) makes the handler throw, which rolls the
20 * transaction's optimistic changes back.
21 * - `inOrder` keeps each record's request and write-back in order, so an
22 * older save can never land after a newer one.
23 */
24​
25export const foldersCollection = createCollection(
26 queryCollectionOptions({
27 id: "folders",
28 queryKey: ["folders"],
29 queryFn: ({ signal }) => api.get<Folder[]>("/api/folders", { signal }),
30 queryClient,
31 schema: folderSchema,
32 getKey: (folder) => folder.id,
33 onUpdate: ({ transaction, collection }) =>
34 saveEach(transaction.mutations, ({ key, changes }) =>
35 inOrder(`/api/folders/${key}`, async () => {
36 const folder = await api.patch<Folder>(`/api/folders/${key}`, changes);
37 await collection.utils.writeUpdate(folder);
38 }),
39 ),
40 }),
41);
42​
43export const notesCollection = createCollection(
44 queryCollectionOptions({
45 id: "notes",
46 queryKey: ["notes"],
47 queryFn: ({ signal }) => api.get<Note[]>("/api/notes", { signal }),
48 queryClient,
49 schema: noteSchema,
50 getKey: (note) => note.id,
51 onInsert: ({ transaction, collection }) =>
52 saveEach(transaction.mutations, ({ key, modified }) =>
53 inOrder(`/api/notes/${key}`, async () => {
54 const note = await api.post<Note>("/api/notes", modified);
55 await collection.utils.writeUpsert(note);
56 }),
57 ),
58 onUpdate: ({ transaction }) => saveNoteUpdates(transaction),
59 onDelete: ({ transaction, collection }) =>
60 saveEach(transaction.mutations, ({ key }) =>
61 inOrder(`/api/notes/${key}`, async () => {
62 await api.delete(`/api/notes/${key}`);
63 await collection.utils.writeDelete(key);
64 }),
65 ),
66 }),
67);
68​
69/**
70 * Sends a transaction's note updates and writes back the saved notes. Also
71 * the save step of the editor's paced mutations (`editNote`).
72 */
73export function saveNoteUpdates(transaction: Transaction<Note>) {
74 return saveEach(transaction.mutations, ({ key, changes }) =>
75 inOrder(`/api/notes/${key}`, async () => {
76 const note = await api.patch<Note>(`/api/notes/${key}`, changes);
77 await notesCollection.utils.writeUpdate(note);
78 }),
79 );
81​
82export const accountCollection = createCollection(
83 queryCollectionOptions({
84 id: "account",
85 queryKey: ["account"],
86 // One signed-in account: the collection holds a single row.
87 queryFn: async ({ signal }) => [await api.get<Account>("/api/account", { signal })],
88 queryClient,
89 schema: accountSchema,
90 getKey: (account) => account.id,
91 onUpdate: ({ transaction, collection }) =>
92 saveEach(transaction.mutations, ({ changes }) =>
93 inOrder("/api/account", async () => {
94 const account = await api.patch<Account>("/api/account", changes);
95 await collection.utils.writeUpdate(account);
96 }),
97 ),
98 }),
99);