wcz-layout 10.1.7 → 10.1.8
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.
- package/dist/{AggregatedCell-CqGptV6i.mjs → AggregatedCell-oGcwbetF.mjs} +3 -3
- package/dist/{AggregatedCell-CqGptV6i.mjs.map → AggregatedCell-oGcwbetF.mjs.map} +1 -1
- package/dist/Approval-DoD-GRRH.mjs.map +1 -1
- package/dist/DialogsContext-DLqA8RJ_.mjs.map +1 -1
- package/dist/Email-BzQQkbe9.mjs.map +1 -1
- package/dist/FileMeta-DDqUju1Y.mjs.map +1 -1
- package/dist/{RouterListItemButton-ke6iPKkL.mjs → RouterListItemButton-ULMlaQh6.mjs} +9 -9
- package/dist/RouterListItemButton-ULMlaQh6.mjs.map +1 -0
- package/dist/approval-L09uPzTU.mjs.map +1 -1
- package/dist/auth.mjs.map +1 -1
- package/dist/components.mjs +31 -28
- package/dist/components.mjs.map +1 -1
- package/dist/createApi-BhNkG4jz.mjs.map +1 -1
- package/dist/data/client.mjs.map +1 -1
- package/dist/data/server.mjs.map +1 -1
- package/dist/file-Chlljz3o.mjs.map +1 -1
- package/dist/file-Dl8JCcqy.mjs.map +1 -1
- package/dist/hooks.mjs +27 -27
- package/dist/hooks.mjs.map +1 -1
- package/dist/index.mjs +9 -9
- package/dist/index.mjs.map +1 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/models.mjs.map +1 -1
- package/dist/peoplesoft-BhintHQz.mjs.map +1 -1
- package/dist/providers-BvuZWkpD.mjs.map +1 -1
- package/dist/tokens-XuFhVbh5.mjs.map +1 -1
- package/dist/utils-CaeVFWF-.mjs.map +1 -1
- package/dist/vite.mjs.map +1 -1
- package/package.json +131 -131
- package/skills/client-db/SKILL.md +136 -136
- package/skills/db-schema/SKILL.md +98 -98
- package/skills/dialogs/SKILL.md +74 -74
- package/skills/forms/SKILL.md +122 -122
- package/skills/general/SKILL.md +127 -127
- package/skills/migrations/SKILL.md +57 -57
- package/skills/migrations/versions/migrate-v10-to-v11.md +4 -2
- package/skills/migrations/versions/migrate-v9-to-v10.md +582 -582
- package/skills/routing/SKILL.md +87 -87
- package/skills/server-functions/SKILL.md +95 -95
- package/skills/services/SKILL.md +34 -34
- package/skills/services/docs/approval.md +258 -258
- package/skills/services/docs/email.md +32 -32
- package/skills/services/docs/file.md +108 -108
- package/skills/services/docs/peoplesoft.md +114 -114
- package/skills/start/SKILL.md +52 -52
- package/skills/start/steps/01-git-setup.md +10 -10
- package/skills/start/steps/02-project-name-setup.md +19 -19
- package/skills/start/steps/04-wicop-setup.md +105 -105
- package/skills/start/steps/05-database-setup.md +52 -52
- package/skills/start/steps/06-entra-setup.md +96 -96
- package/skills/start/steps/07-vault-setup.md +115 -115
- package/skills/start/steps/08-generate-favicon.md +8 -8
- package/skills/start/steps/09-gitlab-ci.md +25 -25
- package/skills/start/steps/10-commit.md +15 -15
- package/skills/table/SKILL.md +267 -266
- package/dist/RouterListItemButton-ke6iPKkL.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "wcz-layout",
|
|
3
|
-
"version": "10.1.
|
|
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.
|
|
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.
|
|
87
|
-
"@tanstack/ai-client": "^0.
|
|
88
|
-
"@tanstack/ai-openai": "^0.30.
|
|
89
|
-
"@tanstack/highlight": "^1.1.0",
|
|
90
|
-
"@tanstack/intent": "^0.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
|
+
{
|
|
2
|
+
"name": "wcz-layout",
|
|
3
|
+
"version": "10.1.8",
|
|
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.3",
|
|
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.70.0",
|
|
87
|
+
"@tanstack/ai-client": "^0.40.0",
|
|
88
|
+
"@tanstack/ai-openai": "^0.30.1",
|
|
89
|
+
"@tanstack/highlight": "^1.1.0",
|
|
90
|
+
"@tanstack/intent": "^0.5.6",
|
|
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
|
+
```
|