Open History to see the last commit
1# React Action-Enforcement Example
2
3This example uses ESLint to keep Collection mutations in action modules while
4allowing feature code to read Collections with `useLiveQuery`.
5
6## Run
7
8From the repository root:
9
10```bash
11pnpm install
12pnpm --filter @tanstack/db-example-react-action-enforcement... build
13pnpm --filter @tanstack/db-example-react-action-enforcement dev
14```
15
16From this example directory, run `pnpm test`, `pnpm lint`, or `pnpm build`.
17The example shares the repository lockfile and uses the workspace DB packages.
18Tests cover the lint boundary and the todo form. The form preserves newer drafts
19while an add settles and disables each row’s toggle while it has pending writes.
20Add stays disabled until its current action settles. The input remains editable,
21and submissions work again after success or failure. A failed toggle rolls back,
22shows an error, and permits another attempt.
23
24## What is enforced
25
26In `src/features/**`, the custom rule rejects direct calls to `insert`, `update`,
27`delete`, and `upsert` on ES imports from `@/db/collections`, its subpaths, and
28relative paths such as `../../db/collections/todoCollection`. The default
29patterns match import text: `@/db/collections` or one or more `./` or `../`
30segments followed by `db/collections`, with an optional subpath. Aliases retain
31this classification. Reads are allowed. Writes go through `src/db/actions/*`.
32
33- `src/db/collections/todoCollection.ts` configures the Collection.
34- `src/db/actions/todoActions.ts` defines optimistic actions and waits for server
35 writes to sync back before each action completes.
36- `src/features/todos/TodoApp.tsx` reads directly with `useLiveQuery` and calls actions.
37- `eslint-rules/no-direct-collection-mutations.js` implements the rule.
38
39The rule resolves lexical bindings, including aliases created by declarations,
40assignments, and namespace destructuring. A callback can appear before its alias
41is initialized. Dot access, quoted strings, and template literals without
42interpolation identify static method names. Unrelated local variables with the
43same name remain independent.
44A variable assigned a Collection remains classified as a Collection throughout
45its scope, even if another assignment gives it a different value.
46
47This is a static, file-local example. It assumes exports from matching paths are
48Collections or namespaces of Collections. It does not perform type checking or
49resolve paths on disk, track CommonJS/import-equals bindings, or follow
50re-exports, function arguments, factory returns, aliases stored in new
51objects/arrays, extracted mutation functions, or dynamic method names. Configure
52`collectionImportPatterns` (regular expressions) for your application's import
53paths and `mutationMethods` for its write API.
54
55For a stricter boundary, `eslint.config.mjs` includes a commented
56`no-restricted-imports` alternative. That option also prohibits direct reads;
57use the provided `src/db/queries/useTodos.ts` hook in feature code instead.
58
59## Rejected code
60
61This call in a feature module produces a lint error:
62
63```ts
64import { todoCollection } from '@/db/collections/todoCollection'
65
66todoCollection.insert({
67 id: crypto.randomUUID(),
68 text: 'bad write',
69 completed: false,
70 createdAt: new Date(),
71})
72```
73
74The rejected forms live in the test suite, so the runnable app passes lint.
75The bounded syntax oracle checks reads and writes across alias forms, callback
76orders, TypeScript wrappers, and lexical scopes using ESLint's public diagnostics.