wcz-layout 10.1.2 → 10.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/dist/{AggregatedCell-C7nAQyMO.mjs → AggregatedCell-CqGptV6i.mjs} +199 -150
  2. package/dist/AggregatedCell-CqGptV6i.mjs.map +1 -0
  3. package/dist/Approval-DoD-GRRH.mjs.map +1 -1
  4. package/dist/DialogsContext-DLqA8RJ_.mjs.map +1 -1
  5. package/dist/Email-BzQQkbe9.mjs.map +1 -1
  6. package/dist/FileMeta-DDqUju1Y.mjs.map +1 -1
  7. package/dist/{LayoutTable-DX9IaO8V.d.mts → LayoutTable-CRo43iWr.d.mts} +7 -3
  8. package/dist/{RouterListItemButton-ULMlaQh6.mjs → RouterListItemButton-ke6iPKkL.mjs} +9 -9
  9. package/dist/RouterListItemButton-ke6iPKkL.mjs.map +1 -0
  10. package/dist/{approval-DiNOXd4B.mjs → approval-L09uPzTU.mjs} +2 -2
  11. package/dist/approval-L09uPzTU.mjs.map +1 -0
  12. package/dist/auth.d.mts +1 -0
  13. package/dist/auth.mjs +15 -12
  14. package/dist/auth.mjs.map +1 -1
  15. package/dist/components.d.mts +1 -1
  16. package/dist/components.mjs +594 -361
  17. package/dist/components.mjs.map +1 -1
  18. package/dist/{createApi-DGJJ4I_R.mjs → createApi-BhNkG4jz.mjs} +3 -3
  19. package/dist/createApi-BhNkG4jz.mjs.map +1 -0
  20. package/dist/data/client.mjs +3 -3
  21. package/dist/data/client.mjs.map +1 -1
  22. package/dist/data/server.mjs +4 -4
  23. package/dist/data/server.mjs.map +1 -1
  24. package/dist/{file-C14fhUeg.mjs → file-Chlljz3o.mjs} +4 -4
  25. package/dist/file-Chlljz3o.mjs.map +1 -0
  26. package/dist/{file-C5BOj1q8.mjs → file-Dl8JCcqy.mjs} +4 -4
  27. package/dist/file-Dl8JCcqy.mjs.map +1 -0
  28. package/dist/hooks.d.mts +2 -1
  29. package/dist/hooks.mjs +541 -591
  30. package/dist/hooks.mjs.map +1 -1
  31. package/dist/index.d.mts +9 -0
  32. package/dist/index.mjs +438 -428
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/middleware.mjs +2 -2
  35. package/dist/middleware.mjs.map +1 -1
  36. package/dist/models.mjs.map +1 -1
  37. package/dist/{peoplesoft-BkJMCcHb.mjs → peoplesoft-BhintHQz.mjs} +3 -3
  38. package/dist/peoplesoft-BhintHQz.mjs.map +1 -0
  39. package/dist/{providers-DrairEO2.mjs → providers-BvuZWkpD.mjs} +2 -2
  40. package/dist/providers-BvuZWkpD.mjs.map +1 -0
  41. package/dist/{tokens-BLRreL1j.mjs → tokens-XuFhVbh5.mjs} +9 -6
  42. package/dist/tokens-XuFhVbh5.mjs.map +1 -0
  43. package/dist/{utils-BUpDoqme.mjs → utils-CaeVFWF-.mjs} +18 -7
  44. package/dist/utils-CaeVFWF-.mjs.map +1 -0
  45. package/dist/utils.mjs +1 -1
  46. package/dist/vite.mjs +11 -0
  47. package/dist/vite.mjs.map +1 -1
  48. package/package.json +131 -130
  49. package/skills/client-db/SKILL.md +136 -136
  50. package/skills/db-schema/SKILL.md +98 -98
  51. package/skills/dialogs/SKILL.md +74 -74
  52. package/skills/forms/SKILL.md +122 -122
  53. package/skills/general/SKILL.md +127 -127
  54. package/skills/general/references/exports.md +6 -1
  55. package/skills/migrations/SKILL.md +57 -57
  56. package/skills/migrations/versions/migrate-v10-to-v11.md +2 -1
  57. package/skills/migrations/versions/migrate-v9-to-v10.md +582 -582
  58. package/skills/routing/SKILL.md +87 -87
  59. package/skills/server-functions/SKILL.md +95 -95
  60. package/skills/services/SKILL.md +34 -34
  61. package/skills/services/docs/email.md +32 -32
  62. package/skills/services/docs/file.md +108 -108
  63. package/skills/services/docs/peoplesoft.md +114 -114
  64. package/skills/start/SKILL.md +52 -52
  65. package/skills/start/steps/01-git-setup.md +10 -10
  66. package/skills/start/steps/02-project-name-setup.md +19 -19
  67. package/skills/start/steps/03-apm-setup.md +97 -208
  68. package/skills/start/steps/04-wicop-setup.md +105 -105
  69. package/skills/start/steps/05-database-setup.md +52 -52
  70. package/skills/start/steps/06-entra-setup.md +96 -96
  71. package/skills/start/steps/07-vault-setup.md +115 -115
  72. package/skills/start/steps/08-generate-favicon.md +8 -8
  73. package/skills/start/steps/09-gitlab-ci.md +25 -25
  74. package/skills/start/steps/10-commit.md +15 -15
  75. package/skills/table/SKILL.md +266 -264
  76. package/dist/AggregatedCell-C7nAQyMO.mjs.map +0 -1
  77. package/dist/RouterListItemButton-ULMlaQh6.mjs.map +0 -1
  78. package/dist/approval-DiNOXd4B.mjs.map +0 -1
  79. package/dist/createApi-DGJJ4I_R.mjs.map +0 -1
  80. package/dist/file-C14fhUeg.mjs.map +0 -1
  81. package/dist/file-C5BOj1q8.mjs.map +0 -1
  82. package/dist/peoplesoft-BkJMCcHb.mjs.map +0 -1
  83. package/dist/providers-DrairEO2.mjs.map +0 -1
  84. package/dist/tokens-BLRreL1j.mjs.map +0 -1
  85. package/dist/utils-BUpDoqme.mjs.map +0 -1
package/package.json CHANGED
@@ -1,130 +1,131 @@
1
- {
2
- "name": "wcz-layout",
3
- "version": "10.1.2",
4
- "private": false,
5
- "keywords": [
6
- "tanstack-intent"
7
- ],
8
- "repository": {
9
- "type": "git",
10
- "url": "https://gitlab.wistron.com/wcz/it-developers/layout/tanstack.git"
11
- },
12
- "files": [
13
- "dist",
14
- "skills",
15
- "!skills/_artifacts"
16
- ],
17
- "type": "module",
18
- "sideEffects": [
19
- "./dist/index.mjs"
20
- ],
21
- "exports": {
22
- "./package.json": "./package.json",
23
- ".": {
24
- "types": "./dist/index.d.mts",
25
- "import": "./dist/index.mjs"
26
- },
27
- "./components": {
28
- "types": "./dist/components.d.mts",
29
- "import": "./dist/components.mjs"
30
- },
31
- "./hooks": {
32
- "types": "./dist/hooks.d.mts",
33
- "import": "./dist/hooks.mjs"
34
- },
35
- "./middleware": {
36
- "types": "./dist/middleware.d.mts",
37
- "import": "./dist/middleware.mjs"
38
- },
39
- "./models": {
40
- "types": "./dist/models.d.mts",
41
- "import": "./dist/models.mjs"
42
- },
43
- "./data/client": {
44
- "types": "./dist/data/client.d.mts",
45
- "import": "./dist/data/client.mjs"
46
- },
47
- "./data/server": {
48
- "types": "./dist/data/server.d.mts",
49
- "import": "./dist/data/server.mjs"
50
- },
51
- "./utils": {
52
- "types": "./dist/utils.d.mts",
53
- "import": "./dist/utils.mjs"
54
- },
55
- "./auth": {
56
- "types": "./dist/auth.d.mts",
57
- "import": "./dist/auth.mjs"
58
- },
59
- "./vite": {
60
- "types": "./dist/vite.d.mts",
61
- "import": "./dist/vite.mjs"
62
- }
63
- },
64
- "scripts": {
65
- "dev": "vp dev",
66
- "typecheck": "tsc --noEmit --watch --preserveWatchOutput",
67
- "build": "vp pack",
68
- "prepublishOnly": "npm run build"
69
- },
70
- "dependencies": {
71
- "@t3-oss/env-core": "^0.13.11",
72
- "i18next": "^26.4.2",
73
- "i18next-browser-languagedetector": "^8.2.1",
74
- "jose": "^6.2.12",
75
- "react-dropzone": "^20.1.2",
76
- "react-i18next": "^17.0.16",
77
- "react-intersection-observer": "^11.0.1",
78
- "react-number-format": "^5.4.5",
79
- "tus-js-client": "^4.3.1",
80
- "uuidv7": "^1.2.1"
81
- },
82
- "devDependencies": {
83
- "@arethetypeswrong/core": "^0.18.5",
84
- "@rolldown/plugin-babel": "^0.2.4",
85
- "@tanstack/ai": "^0.65.1",
86
- "@tanstack/ai-client": "^0.38.0",
87
- "@tanstack/ai-openai": "^0.27.0",
88
- "@tanstack/highlight": "^1.0.0",
89
- "@tanstack/intent": "^0.5.5",
90
- "@tanstack/react-pacer": "^0.24.1",
91
- "@types/node": "^24.10.13",
92
- "@types/react": "^19.3.0",
93
- "@types/react-dom": "^19.3.0",
94
- "@vitejs/plugin-react": "^6.1.2",
95
- "babel-plugin-react-compiler": "^1.0.0",
96
- "nitro": "npm:nitro-nightly@latest",
97
- "publint": "^0.3.25",
98
- "typescript": "^7.0.2",
99
- "vite": "npm:@voidzero-dev/vite-plus-core@1.1.0",
100
- "vite-plus": "1.1.0"
101
- },
102
- "peerDependencies": {
103
- "@emotion/cache": "11.x",
104
- "@emotion/react": "11.x",
105
- "@emotion/styled": "11.x",
106
- "@mui/icons-material": "9.x",
107
- "@mui/material": "9.x",
108
- "@mui/x-date-pickers": "9.x",
109
- "@tanstack/query-db-collection": "1.x",
110
- "@tanstack/react-db": "0.x",
111
- "@tanstack/react-form": "1.x",
112
- "@tanstack/react-hotkeys": "0.x",
113
- "@tanstack/react-pacer": "0.x",
114
- "@tanstack/react-query": "5.x",
115
- "@tanstack/react-router": "1.x",
116
- "@tanstack/react-router-ssr-query": "1.x",
117
- "@tanstack/react-start": "1.x",
118
- "@tanstack/react-table": "9.x",
119
- "@tanstack/react-virtual": "3.x",
120
- "axios": "1.x",
121
- "dayjs": "1.x",
122
- "react": "19.x",
123
- "react-dom": "19.x",
124
- "zod": "4.x"
125
- },
126
- "overrides": {
127
- "vite": "$vite"
128
- },
129
- "packageManager": "npm@12.2.0"
130
- }
1
+ {
2
+ "name": "wcz-layout",
3
+ "version": "10.1.7",
4
+ "private": false,
5
+ "keywords": [
6
+ "tanstack-intent"
7
+ ],
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://gitlab.wistron.com/wcz/it-developers/layout/tanstack.git"
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "skills",
15
+ "!skills/_artifacts"
16
+ ],
17
+ "type": "module",
18
+ "sideEffects": [
19
+ "./dist/index.mjs"
20
+ ],
21
+ "exports": {
22
+ "./package.json": "./package.json",
23
+ ".": {
24
+ "types": "./dist/index.d.mts",
25
+ "import": "./dist/index.mjs"
26
+ },
27
+ "./components": {
28
+ "types": "./dist/components.d.mts",
29
+ "import": "./dist/components.mjs"
30
+ },
31
+ "./hooks": {
32
+ "types": "./dist/hooks.d.mts",
33
+ "import": "./dist/hooks.mjs"
34
+ },
35
+ "./middleware": {
36
+ "types": "./dist/middleware.d.mts",
37
+ "import": "./dist/middleware.mjs"
38
+ },
39
+ "./models": {
40
+ "types": "./dist/models.d.mts",
41
+ "import": "./dist/models.mjs"
42
+ },
43
+ "./data/client": {
44
+ "types": "./dist/data/client.d.mts",
45
+ "import": "./dist/data/client.mjs"
46
+ },
47
+ "./data/server": {
48
+ "types": "./dist/data/server.d.mts",
49
+ "import": "./dist/data/server.mjs"
50
+ },
51
+ "./utils": {
52
+ "types": "./dist/utils.d.mts",
53
+ "import": "./dist/utils.mjs"
54
+ },
55
+ "./auth": {
56
+ "types": "./dist/auth.d.mts",
57
+ "import": "./dist/auth.mjs"
58
+ },
59
+ "./vite": {
60
+ "types": "./dist/vite.d.mts",
61
+ "import": "./dist/vite.mjs"
62
+ }
63
+ },
64
+ "scripts": {
65
+ "dev": "vp dev",
66
+ "typecheck": "tsc --noEmit --watch --preserveWatchOutput",
67
+ "build": "vp pack",
68
+ "prepublishOnly": "npm run build"
69
+ },
70
+ "dependencies": {
71
+ "@t3-oss/env-core": "^0.13.11",
72
+ "i18next": "^26.5.2",
73
+ "i18next-browser-languagedetector": "^8.2.1",
74
+ "iron-session": "^9.0.1",
75
+ "jose": "^6.2.12",
76
+ "react-dropzone": "^20.1.4",
77
+ "react-i18next": "^17.1.0",
78
+ "react-intersection-observer": "^11.0.1",
79
+ "react-number-format": "^5.4.5",
80
+ "tus-js-client": "^4.3.1",
81
+ "uuidv7": "^1.2.1"
82
+ },
83
+ "devDependencies": {
84
+ "@arethetypeswrong/core": "^0.18.5",
85
+ "@rolldown/plugin-babel": "^0.2.4",
86
+ "@tanstack/ai": "^0.69.0",
87
+ "@tanstack/ai-client": "^0.39.3",
88
+ "@tanstack/ai-openai": "^0.30.0",
89
+ "@tanstack/highlight": "^1.1.0",
90
+ "@tanstack/intent": "^0.5.5",
91
+ "@tanstack/react-pacer": "^0.24.1",
92
+ "@types/node": "^24.10.13",
93
+ "@types/react": "^19.3.0",
94
+ "@types/react-dom": "^19.3.0",
95
+ "@vitejs/plugin-react": "^6.1.2",
96
+ "babel-plugin-react-compiler": "^1.0.0",
97
+ "nitro": "npm:nitro-nightly@latest",
98
+ "publint": "^0.3.25",
99
+ "typescript": "^7.0.2",
100
+ "vite": "npm:@voidzero-dev/vite-plus-core@1.3.0",
101
+ "vite-plus": "1.3.0"
102
+ },
103
+ "peerDependencies": {
104
+ "@emotion/cache": "11.x",
105
+ "@emotion/react": "11.x",
106
+ "@emotion/styled": "11.x",
107
+ "@mui/icons-material": "9.x",
108
+ "@mui/material": "9.x",
109
+ "@mui/x-date-pickers": "9.x",
110
+ "@tanstack/query-db-collection": "1.x",
111
+ "@tanstack/react-db": "0.x",
112
+ "@tanstack/react-form": "1.x",
113
+ "@tanstack/react-hotkeys": "0.x",
114
+ "@tanstack/react-pacer": "0.x",
115
+ "@tanstack/react-query": "5.x",
116
+ "@tanstack/react-router": "1.x",
117
+ "@tanstack/react-router-ssr-query": "1.x",
118
+ "@tanstack/react-start": "1.x",
119
+ "@tanstack/react-table": "9.x",
120
+ "@tanstack/react-virtual": "3.x",
121
+ "axios": "1.x",
122
+ "dayjs": "1.x",
123
+ "react": "19.x",
124
+ "react-dom": "19.x",
125
+ "zod": "4.x"
126
+ },
127
+ "overrides": {
128
+ "vite": "$vite"
129
+ },
130
+ "packageManager": "npm@12.2.0"
131
+ }
@@ -1,136 +1,136 @@
1
- ---
2
- name: client-db
3
- description: "Use when reading or writing client data with TanStack DB: collections, live queries, route preloading, optimistic writes, eager/on-demand sync, or browser SQLite persistence."
4
- metadata:
5
- type: convention
6
- library: wcz-layout
7
- ---
8
-
9
- > Mechanics (the live-query builder, `createOptimisticAction`, collection adapters,
10
- > browser persistence) are covered by TanStack's own skills. List the skills shipped by
11
- > `@tanstack/db` and `@tanstack/react-db` and load whichever cover the task at hand.
12
- > This skill adds the wcz-layout conventions on top, and overrides upstream where noted.
13
-
14
- ## Rules
15
-
16
- - One `DbClient` per request, built in `src/router.tsx` and put on the router context
17
- next to `queryClient`. Components reach it with `useDbClient()`, loaders with
18
- `context.dbClient`.
19
- - Declare collections as descriptors: `collectionOptions(id, (client) => ...)`. Take the
20
- query client from the factory with `client.requireDependency<QueryClient>("queryClient")`,
21
- never a module-scope one — it is built per request and a singleton leaks across SSR.
22
- - Export a `use<Table>Collection` hook next to each collection. It is the only way components
23
- get a live collection to write to.
24
- - Define every live query once in `src/db/queries/<table>.ts` as
25
- `{ query: (q: InitialQueryBuilder) => ... }`. Reuse that definition in loaders and
26
- components.
27
- - Preload in the route loader and return the promise:
28
- `loader: ({ context }) => context.dbClient.preloadLiveQuery(<x>QueryOptions)`.
29
- This transports the query result, so `useLiveQuery` has rows on the first render. Never
30
- fire it with `void`: the server then renders an empty table and the browser fetches
31
- again. Explicit collection preload transports source rows and is a no-op for on-demand
32
- collections.
33
- - A detail route awaits the preload, then throws `notFound()` when the row is missing,
34
- and shows a skeleton `pendingComponent` meanwhile (see the example).
35
- - `useLiveQuery` for list pages, `useLiveSuspenseQuery` for detail components under
36
- suspense. `.findOne()` still returns `T | undefined` after the loader's check, so keep
37
- an `if (!data) return null` guard.
38
- - Shape data in the query: filter, order, group and aggregate only the fields the
39
- consumer needs. Do not load broad results and transform them in component code.
40
- - Mutations go through `createOptimisticAction`: mirror the server write in `onMutate`,
41
- call the server function in `mutationFn`, then
42
- `await utils.refetch({ throwOnError: true })` on every collection that function writes.
43
- Await `transaction.isPersisted.promise` at the call site and surface failures with
44
- `alert()`.
45
- - `syncMode: "eager"` for top-level collections, `"on-demand"` for child data and anything expected past ~10k rows.
46
-
47
- ## Examples
48
-
49
- ```ts
50
- // src/db/collections/<table>.ts
51
- import { queryCollectionOptions } from "@tanstack/query-db-collection";
52
- import { collectionOptions, useDbClient } from "@tanstack/react-db";
53
- import type { QueryClient } from "@tanstack/react-query";
54
- import { TodoSchema } from "~/lib/schemas/todo";
55
- import { selectTodos } from "~/server/actions/todo";
56
-
57
- const TODOS_ID = "todos";
58
-
59
- export const todoCollection = collectionOptions(TODOS_ID, (client) =>
60
- queryCollectionOptions({
61
- id: TODOS_ID,
62
- queryKey: [TODOS_ID],
63
- queryFn: () => selectTodos(),
64
- getKey: ({ id }) => id,
65
- schema: TodoSchema,
66
- queryClient: client.requireDependency<QueryClient>("queryClient"),
67
- syncMode: "eager",
68
- }),
69
- );
70
-
71
- export const useTodoCollection = () => useDbClient().collection(todoCollection);
72
- ```
73
-
74
- ```ts
75
- // src/db/queries/<table>.ts
76
- import type { InitialQueryBuilder } from "@tanstack/react-db";
77
- import { eq } from "@tanstack/react-db";
78
- import { todoCollection } from "../collections/todo";
79
-
80
- export const todosQueryOptions = {
81
- query: (q: InitialQueryBuilder) =>
82
- q.from({ todo: todoCollection }).orderBy(({ todo }) => todo.id, "desc"),
83
- };
84
-
85
- export const todoByIdQueryOptions = (id: string) => ({
86
- query: (q: InitialQueryBuilder) =>
87
- q
88
- .from({ todo: todoCollection })
89
- .where(({ todo }) => eq(todo.id, id))
90
- .findOne(),
91
- });
92
- ```
93
-
94
- ```tsx
95
- // detail route: src/routes/todos/$id.tsx
96
- export const Route = createFileRoute("/todos/$id")({
97
- component: RouteComponent,
98
- pendingComponent: TodoSkeleton,
99
- beforeLoad: requireAuth("admin"),
100
- loader: async ({ context, params }) => {
101
- await context.dbClient.preloadLiveQuery(todoByIdQueryOptions(params.id));
102
- if (!context.dbClient.collection(todoCollection).has(params.id)) throw notFound();
103
- },
104
- });
105
-
106
- function RouteComponent() {
107
- const { id } = Route.useParams();
108
- const { alert } = useDialogs();
109
- const navigate = useNavigate();
110
- const todos = useTodoCollection();
111
- const { data } = useLiveSuspenseQuery(todoByIdQueryOptions(id));
112
-
113
- const handleOnDelete = createOptimisticAction<string>({
114
- onMutate: (id) => {
115
- todos.delete(id);
116
- },
117
- mutationFn: async (id) => {
118
- await deleteTodos({ data: [id] });
119
- await todos.utils.refetch({ throwOnError: true });
120
- },
121
- });
122
-
123
- const handleDelete = async () => {
124
- try {
125
- const transaction = handleOnDelete(id);
126
- await transaction.isPersisted.promise;
127
- await navigate({ to: "/todos" });
128
- } catch (error) {
129
- if (error instanceof Error) await alert(error.message);
130
- }
131
- };
132
-
133
- if (!data) return null;
134
- // render data; call handleDelete from the delete button...
135
- }
136
- ```
1
+ ---
2
+ name: client-db
3
+ description: "Use when reading or writing client data with TanStack DB: collections, live queries, route preloading, optimistic writes, eager/on-demand sync, or browser SQLite persistence."
4
+ metadata:
5
+ type: convention
6
+ library: wcz-layout
7
+ ---
8
+
9
+ > Mechanics (the live-query builder, `createOptimisticAction`, collection adapters,
10
+ > browser persistence) are covered by TanStack's own skills. List the skills shipped by
11
+ > `@tanstack/db` and `@tanstack/react-db` and load whichever cover the task at hand.
12
+ > This skill adds the wcz-layout conventions on top, and overrides upstream where noted.
13
+
14
+ ## Rules
15
+
16
+ - One `DbClient` per request, built in `src/router.tsx` and put on the router context
17
+ next to `queryClient`. Components reach it with `useDbClient()`, loaders with
18
+ `context.dbClient`.
19
+ - Declare collections as descriptors: `collectionOptions(id, (client) => ...)`. Take the
20
+ query client from the factory with `client.requireDependency<QueryClient>("queryClient")`,
21
+ never a module-scope one — it is built per request and a singleton leaks across SSR.
22
+ - Export a `use<Table>Collection` hook next to each collection. It is the only way components
23
+ get a live collection to write to.
24
+ - Define every live query once in `src/db/queries/<table>.ts` as
25
+ `{ query: (q: InitialQueryBuilder) => ... }`. Reuse that definition in loaders and
26
+ components.
27
+ - Preload in the route loader and return the promise:
28
+ `loader: ({ context }) => context.dbClient.preloadLiveQuery(<x>QueryOptions)`.
29
+ This transports the query result, so `useLiveQuery` has rows on the first render. Never
30
+ fire it with `void`: the server then renders an empty table and the browser fetches
31
+ again. Explicit collection preload transports source rows and is a no-op for on-demand
32
+ collections.
33
+ - A detail route awaits the preload, then throws `notFound()` when the row is missing,
34
+ and shows a skeleton `pendingComponent` meanwhile (see the example).
35
+ - `useLiveQuery` for list pages, `useLiveSuspenseQuery` for detail components under
36
+ suspense. `.findOne()` still returns `T | undefined` after the loader's check, so keep
37
+ an `if (!data) return null` guard.
38
+ - Shape data in the query: filter, order, group and aggregate only the fields the
39
+ consumer needs. Do not load broad results and transform them in component code.
40
+ - Mutations go through `createOptimisticAction`: mirror the server write in `onMutate`,
41
+ call the server function in `mutationFn`, then
42
+ `await utils.refetch({ throwOnError: true })` on every collection that function writes.
43
+ Await `transaction.isPersisted.promise` at the call site and surface failures with
44
+ `alert()`.
45
+ - `syncMode: "eager"` for top-level collections, `"on-demand"` for child data and anything expected past ~10k rows.
46
+
47
+ ## Examples
48
+
49
+ ```ts
50
+ // src/db/collections/<table>.ts
51
+ import { queryCollectionOptions } from "@tanstack/query-db-collection";
52
+ import { collectionOptions, useDbClient } from "@tanstack/react-db";
53
+ import type { QueryClient } from "@tanstack/react-query";
54
+ import { TodoSchema } from "~/lib/schemas/todo";
55
+ import { selectTodos } from "~/server/actions/todo";
56
+
57
+ const TODOS_ID = "todos";
58
+
59
+ export const todoCollection = collectionOptions(TODOS_ID, (client) =>
60
+ queryCollectionOptions({
61
+ id: TODOS_ID,
62
+ queryKey: [TODOS_ID],
63
+ queryFn: () => selectTodos(),
64
+ getKey: ({ id }) => id,
65
+ schema: TodoSchema,
66
+ queryClient: client.requireDependency<QueryClient>("queryClient"),
67
+ syncMode: "eager",
68
+ }),
69
+ );
70
+
71
+ export const useTodoCollection = () => useDbClient().collection(todoCollection);
72
+ ```
73
+
74
+ ```ts
75
+ // src/db/queries/<table>.ts
76
+ import type { InitialQueryBuilder } from "@tanstack/react-db";
77
+ import { eq } from "@tanstack/react-db";
78
+ import { todoCollection } from "../collections/todo";
79
+
80
+ export const todosQueryOptions = {
81
+ query: (q: InitialQueryBuilder) =>
82
+ q.from({ todo: todoCollection }).orderBy(({ todo }) => todo.id, "desc"),
83
+ };
84
+
85
+ export const todoByIdQueryOptions = (id: string) => ({
86
+ query: (q: InitialQueryBuilder) =>
87
+ q
88
+ .from({ todo: todoCollection })
89
+ .where(({ todo }) => eq(todo.id, id))
90
+ .findOne(),
91
+ });
92
+ ```
93
+
94
+ ```tsx
95
+ // detail route: src/routes/todos/$id.tsx
96
+ export const Route = createFileRoute("/todos/$id")({
97
+ component: RouteComponent,
98
+ pendingComponent: TodoSkeleton,
99
+ beforeLoad: requireAuth("admin"),
100
+ loader: async ({ context, params }) => {
101
+ await context.dbClient.preloadLiveQuery(todoByIdQueryOptions(params.id));
102
+ if (!context.dbClient.collection(todoCollection).has(params.id)) throw notFound();
103
+ },
104
+ });
105
+
106
+ function RouteComponent() {
107
+ const { id } = Route.useParams();
108
+ const { alert } = useDialogs();
109
+ const navigate = useNavigate();
110
+ const todos = useTodoCollection();
111
+ const { data } = useLiveSuspenseQuery(todoByIdQueryOptions(id));
112
+
113
+ const handleOnDelete = createOptimisticAction<string>({
114
+ onMutate: (id) => {
115
+ todos.delete(id);
116
+ },
117
+ mutationFn: async (id) => {
118
+ await deleteTodos({ data: [id] });
119
+ await todos.utils.refetch({ throwOnError: true });
120
+ },
121
+ });
122
+
123
+ const handleDelete = async () => {
124
+ try {
125
+ const transaction = handleOnDelete(id);
126
+ await transaction.isPersisted.promise;
127
+ await navigate({ to: "/todos" });
128
+ } catch (error) {
129
+ if (error instanceof Error) await alert(error.message);
130
+ }
131
+ };
132
+
133
+ if (!data) return null;
134
+ // render data; call handleDelete from the delete button...
135
+ }
136
+ ```