Open History to see the last commit
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
43// #region notes-collection
44export const notesCollection = createCollection(
45 queryCollectionOptions({
46 id: "notes",
47 queryKey: ["notes"],
48 queryFn: ({ signal }) => api.get<Note[]>("/api/notes", { signal }),
49 queryClient,
50 schema: noteSchema,
51 getKey: (note) => note.id,
52 onInsert: ({ transaction }) => saveNewNotes(transaction),
53 onUpdate: ({ transaction }) => saveNoteUpdates(transaction),
54 onDelete: ({ transaction }) => deleteNotes(transaction),
55 }),
56);
57// #endregion notes-collection
58
59function saveNewNotes(transaction: Transaction<Note>) {
60 return saveEach(transaction.mutations, ({ key, modified }) =>
61 inOrder(`/api/notes/${key}`, async () => {
62 const note = await api.post<Note>("/api/notes", modified);
63 await notesCollection.utils.writeUpsert(note);
64 }),
65 );
66}
67
68// #region save-note-updates
69/**
70 * Sends a transaction's note updates and writes back the saved notes. Also
71 * the save step of the paced mutation every note change goes through
72 * (`changeNote` in `mutations.ts`).
73 */
74export function saveNoteUpdates(transaction: Transaction<Note>) {
75 return saveEach(transaction.mutations, ({ key, changes }) =>
76 inOrder(`/api/notes/${key}`, async () => {
77 const note = await api.patch<Note>(`/api/notes/${key}`, changes);
78 await notesCollection.utils.writeUpdate(note);
79 }),
80 );
81}
82// #endregion save-note-updates
83
84function deleteNotes(transaction: Transaction<Note>) {
85 return saveEach(transaction.mutations, ({ key }) =>
86 inOrder(`/api/notes/${key}`, async () => {
87 await api.delete(`/api/notes/${key}`);
88 await notesCollection.utils.writeDelete(key);
89 }),
90 );
91}
92
93export const accountCollection = createCollection(
94 queryCollectionOptions({
95 id: "account",
96 queryKey: ["account"],
97 // One signed-in account: the collection holds a single row.
98 queryFn: async ({ signal }) => [await api.get<Account>("/api/account", { signal })],
99 queryClient,
100 schema: accountSchema,
101 getKey: (account) => account.id,
102 onUpdate: ({ transaction, collection }) =>
103 saveEach(transaction.mutations, ({ changes }) =>
104 inOrder("/api/account", async () => {
105 const account = await api.patch<Account>("/api/account", changes);
106 await collection.utils.writeUpdate(account);
107 }),
108 ),
109 }),
110);