@terreno/mcp 0.31.0 → 56.0.0-beta.2

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 (116) hide show
  1. package/dist/bootstrap.d.ts.map +1 -1
  2. package/dist/bootstrap.js +8 -9
  3. package/dist/bootstrap.js.map +1 -1
  4. package/dist/docs/templates/bootstrap/frontend/app/_layout.template.tsx +1 -1
  5. package/dist/docs/upgrades/56.0.0-beta.2.md +59 -0
  6. package/dist/docs/versioned/next/README.md +2 -0
  7. package/dist/docs/versioned/next/explanation/roadmap-seed-issues.md +21 -0
  8. package/dist/docs/versioned/next/how-to/README.md +2 -0
  9. package/dist/docs/versioned/next/how-to/migrate-rtk-to-syncdb.md +445 -0
  10. package/dist/docs/versioned/next/implementationPlans/PLAN_INDEX.md +28 -50
  11. package/dist/docs/versioned/next/implementationPlans/b2b-platform-program.md +3 -2
  12. package/dist/docs/versioned/next/implementationPlans/comms-abstraction.md +98 -15
  13. package/dist/docs/versioned/next/implementationPlans/comms-adapter-sendgrid.md +63 -13
  14. package/dist/docs/versioned/next/implementationPlans/comms-adapter-twilio-sms.md +65 -12
  15. package/dist/docs/versioned/next/implementationPlans/comms-adapter-twilio-verify.md +9 -3
  16. package/dist/docs/versioned/next/implementationPlans/comms-admin-dashboard.md +180 -0
  17. package/dist/docs/versioned/next/implementationPlans/migrate-cicd-to-circleci-research.md +126 -0
  18. package/dist/docs/versioned/next/implementationPlans/migrate-cicd-to-circleci.md +269 -0
  19. package/dist/docs/versioned/next/implementationPlans/rtk-to-syncdb-migration-docs.md +52 -47
  20. package/dist/docs/versioned/next/implementationPlans/syncdb-api-inventory.md +303 -0
  21. package/dist/docs/versioned/next/implementationPlans/syncdb-codegen.md +252 -0
  22. package/dist/docs/versioned/next/implementationPlans/syncdb-local-first.md +468 -420
  23. package/dist/docs/versioned/next/implementationPlans/syncdb-phase-c-design.md +425 -0
  24. package/dist/docs/versioned/next/implementationPlans/terreno-syncdb-2.md +800 -0
  25. package/dist/docs/versioned/next/reference/README.md +2 -0
  26. package/dist/docs/versioned/next/reference/comms.md +132 -0
  27. package/dist/docs/versioned/next/reference/environment-variables.md +9 -0
  28. package/dist/docs/versioned/next/reference/syncdb.md +456 -0
  29. package/dist/docs/versioned/next/superpowers/plans/2026-07-28-admin-mobile-navigation.md +97 -0
  30. package/dist/docs/versioned/next/superpowers/specs/2026-07-28-admin-mobile-navigation-design.md +36 -0
  31. package/dist/docs/versioned/next/tasks/comms-abstraction.md +29 -16
  32. package/dist/docs/versioned/next/tasks/comms-adapter-sendgrid.md +17 -7
  33. package/dist/docs/versioned/next/tasks/comms-adapter-twilio-sms.md +17 -7
  34. package/dist/docs/versioned/next/tasks/comms-adapter-twilio-verify.md +3 -3
  35. package/dist/docs/versioned/next/tasks/comms-admin-dashboard.md +52 -0
  36. package/dist/docs/versioned/next/tasks/migrate-cicd-to-circleci.md +215 -0
  37. package/dist/docs/versioned/next/tasks/rtk-to-syncdb-migration-docs.md +10 -10
  38. package/dist/docs/versioned/next/tasks/syncdb-codegen.md +65 -0
  39. package/dist/docs/versioned/next/tasks/syncdb-local-first.md +434 -117
  40. package/package.json +3 -3
  41. package/src/docs/templates/bootstrap/frontend/app/_layout.template.tsx +1 -1
  42. package/src/docs/upgrades/56.0.0-beta.2.md +59 -0
  43. package/src/docs/versioned/next/README.md +2 -0
  44. package/src/docs/versioned/next/explanation/roadmap-seed-issues.md +21 -0
  45. package/src/docs/versioned/next/how-to/README.md +2 -0
  46. package/src/docs/versioned/next/how-to/migrate-rtk-to-syncdb.md +445 -0
  47. package/src/docs/versioned/next/implementationPlans/PLAN_INDEX.md +28 -50
  48. package/src/docs/versioned/next/implementationPlans/b2b-platform-program.md +3 -2
  49. package/src/docs/versioned/next/implementationPlans/comms-abstraction.md +98 -15
  50. package/src/docs/versioned/next/implementationPlans/comms-adapter-sendgrid.md +63 -13
  51. package/src/docs/versioned/next/implementationPlans/comms-adapter-twilio-sms.md +65 -12
  52. package/src/docs/versioned/next/implementationPlans/comms-adapter-twilio-verify.md +9 -3
  53. package/src/docs/versioned/next/implementationPlans/comms-admin-dashboard.md +180 -0
  54. package/src/docs/versioned/next/implementationPlans/migrate-cicd-to-circleci-research.md +126 -0
  55. package/src/docs/versioned/next/implementationPlans/migrate-cicd-to-circleci.md +269 -0
  56. package/src/docs/versioned/next/implementationPlans/rtk-to-syncdb-migration-docs.md +52 -47
  57. package/src/docs/versioned/next/implementationPlans/syncdb-api-inventory.md +303 -0
  58. package/src/docs/versioned/next/implementationPlans/syncdb-codegen.md +252 -0
  59. package/src/docs/versioned/next/implementationPlans/syncdb-local-first.md +468 -420
  60. package/src/docs/versioned/next/implementationPlans/syncdb-phase-c-design.md +425 -0
  61. package/src/docs/versioned/next/implementationPlans/terreno-syncdb-2.md +800 -0
  62. package/src/docs/versioned/next/reference/README.md +2 -0
  63. package/src/docs/versioned/next/reference/comms.md +132 -0
  64. package/src/docs/versioned/next/reference/environment-variables.md +9 -0
  65. package/src/docs/versioned/next/reference/syncdb.md +456 -0
  66. package/src/docs/versioned/next/superpowers/plans/2026-07-28-admin-mobile-navigation.md +97 -0
  67. package/src/docs/versioned/next/superpowers/specs/2026-07-28-admin-mobile-navigation-design.md +36 -0
  68. package/src/docs/versioned/next/tasks/comms-abstraction.md +29 -16
  69. package/src/docs/versioned/next/tasks/comms-adapter-sendgrid.md +17 -7
  70. package/src/docs/versioned/next/tasks/comms-adapter-twilio-sms.md +17 -7
  71. package/src/docs/versioned/next/tasks/comms-adapter-twilio-verify.md +3 -3
  72. package/src/docs/versioned/next/tasks/comms-admin-dashboard.md +52 -0
  73. package/src/docs/versioned/next/tasks/migrate-cicd-to-circleci.md +215 -0
  74. package/src/docs/versioned/next/tasks/rtk-to-syncdb-migration-docs.md +10 -10
  75. package/src/docs/versioned/next/tasks/syncdb-codegen.md +65 -0
  76. package/src/docs/versioned/next/tasks/syncdb-local-first.md +434 -117
  77. package/dist/__tests__/bootstrap.test.d.ts +0 -2
  78. package/dist/__tests__/bootstrap.test.d.ts.map +0 -1
  79. package/dist/__tests__/bootstrap.test.js +0 -299
  80. package/dist/__tests__/bootstrap.test.js.map +0 -1
  81. package/dist/__tests__/databaseQuery.test.d.ts +0 -2
  82. package/dist/__tests__/databaseQuery.test.d.ts.map +0 -1
  83. package/dist/__tests__/databaseQuery.test.js +0 -83
  84. package/dist/__tests__/databaseQuery.test.js.map +0 -1
  85. package/dist/__tests__/docsRoot.test.d.ts +0 -2
  86. package/dist/__tests__/docsRoot.test.d.ts.map +0 -1
  87. package/dist/__tests__/docsRoot.test.js +0 -59
  88. package/dist/__tests__/docsRoot.test.js.map +0 -1
  89. package/dist/__tests__/inferPackages.test.d.ts +0 -2
  90. package/dist/__tests__/inferPackages.test.d.ts.map +0 -1
  91. package/dist/__tests__/inferPackages.test.js +0 -35
  92. package/dist/__tests__/inferPackages.test.js.map +0 -1
  93. package/dist/__tests__/preload.d.ts +0 -2
  94. package/dist/__tests__/preload.d.ts.map +0 -1
  95. package/dist/__tests__/preload.js +0 -12
  96. package/dist/__tests__/preload.js.map +0 -1
  97. package/dist/__tests__/prompts.test.d.ts +0 -2
  98. package/dist/__tests__/prompts.test.d.ts.map +0 -1
  99. package/dist/__tests__/prompts.test.js +0 -295
  100. package/dist/__tests__/prompts.test.js.map +0 -1
  101. package/dist/__tests__/resources.test.d.ts +0 -2
  102. package/dist/__tests__/resources.test.d.ts.map +0 -1
  103. package/dist/__tests__/resources.test.js +0 -352
  104. package/dist/__tests__/resources.test.js.map +0 -1
  105. package/dist/__tests__/search.test.d.ts +0 -2
  106. package/dist/__tests__/search.test.d.ts.map +0 -1
  107. package/dist/__tests__/search.test.js +0 -163
  108. package/dist/__tests__/search.test.js.map +0 -1
  109. package/dist/__tests__/tools.test.d.ts +0 -2
  110. package/dist/__tests__/tools.test.d.ts.map +0 -1
  111. package/dist/__tests__/tools.test.js +0 -540
  112. package/dist/__tests__/tools.test.js.map +0 -1
  113. package/dist/__tests__/upgradeGuide.test.d.ts +0 -2
  114. package/dist/__tests__/upgradeGuide.test.d.ts.map +0 -1
  115. package/dist/__tests__/upgradeGuide.test.js +0 -22
  116. package/dist/__tests__/upgradeGuide.test.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terreno/mcp",
3
- "version": "0.31.0",
3
+ "version": "56.0.0-beta.2",
4
4
  "description": "MCP server for Terreno - provides documentation, tools, and prompts for building full-stack apps",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -27,7 +27,7 @@
27
27
  "dependencies": {
28
28
  "@modelcontextprotocol/sdk": "^1.27.0",
29
29
  "@sentry/bun": "^10.39.0",
30
- "@terreno/api": "0.31.0",
30
+ "@terreno/api": "56.0.0-beta.2",
31
31
  "minisearch": "^7.2.0",
32
32
  "mongoose": "9.7.4"
33
33
  },
@@ -35,7 +35,7 @@
35
35
  "@biomejs/biome": "^2.3.6",
36
36
  "@types/bun": "^1.3.8",
37
37
  "@types/node": "^25.3.0",
38
- "typescript": "~5.9.2"
38
+ "typescript": "~6.0.3"
39
39
  },
40
40
  "files": [
41
41
  "LICENSE",
@@ -1,5 +1,5 @@
1
1
  import FontAwesome from "@expo/vector-icons/FontAwesome";
2
- import {DarkTheme, DefaultTheme, ThemeProvider} from "@react-navigation/native";
2
+ import {DarkTheme, DefaultTheme, ThemeProvider} from "expo-router/react-navigation";
3
3
  import {useFonts} from "expo-font";
4
4
  import {Stack} from "expo-router";
5
5
  import * as SplashScreen from "expo-splash-screen";
@@ -0,0 +1,59 @@
1
+ # 56.0.0-beta.2
2
+
3
+ Second beta of the Expo SDK 56 line. Packages publish under the npm `beta` dist-tag;
4
+ `npm install @terreno/ui` (no tag) still resolves to the stable `0.x` release.
5
+
6
+ ```bash
7
+ bun add @terreno/ui@beta @terreno/api@beta @terreno/rtk@beta
8
+ # optional local-first data layer
9
+ bun add @terreno/syncdb@beta
10
+ ```
11
+
12
+ ## Upgrade Expo before Terreno
13
+
14
+ This line targets Expo SDK 56. Upgrade the app first, then install Terreno betas:
15
+
16
+ | Package | Version |
17
+ |---------|---------|
18
+ | `expo` | `~56.0.12` |
19
+ | `react-native` | `0.85.3` |
20
+ | `react` | `19.2.3` |
21
+ | `typescript` | `~6.0.3` |
22
+
23
+ Follow Expo's SDK 56 upgrade guide, then bump `@terreno/*` to `56.0.0-beta.2` (or `@beta`).
24
+ Run `bun run compile` / `bun run lint` at the app root and regenerate the OpenAPI SDK after
25
+ any `@terreno/api` surface change (`bun run sdk` in the frontend).
26
+
27
+ Terreno's version major now tracks the Expo SDK major. Stay on the latest `0.x` packages
28
+ until you are ready to move the app to Expo 56.
29
+
30
+ ## New package: `@terreno/syncdb`
31
+
32
+ Optional local-first layer (TinyBase MergeableStore, durable outbox, websocket delta sync,
33
+ encrypted web persistence). Install as `@terreno/syncdb@beta` (or the exact version). See:
34
+
35
+ - [`docs/how-to/migrate-rtk-to-syncdb.md`](https://github.com/FlourishHealth/terreno/blob/master/docs/how-to/migrate-rtk-to-syncdb.md)
36
+ - [`docs/reference/syncdb.md`](https://github.com/FlourishHealth/terreno/blob/master/docs/reference/syncdb.md)
37
+
38
+ `@terreno/syncdb` does not replace the generated OpenAPI SDK, Better Auth wiring, or
39
+ feature flags — only collection sync/CRUD offline paths.
40
+
41
+ ## `excludeArchivedPlugin` now filters `findOne`
42
+
43
+ `excludeArchivedPlugin` applies the same archived filter to `findOne` that it already
44
+ applied to `find`, matching `isDeletedPlugin`. Code that relied on `findOne` returning
45
+ archived documents must query with an explicit archived override or a different plugin
46
+ configuration.
47
+
48
+ ## Sync / REST error codes
49
+
50
+ Sync and REST CRUD executors preserve kebab-case error `code` values
51
+ (`create-not-allowed`, `update-not-allowed`, `delete-not-allowed`, `invalid-request-body`)
52
+ and status-specific error names on hook-rejection paths. Clients that matched on older
53
+ code strings should update.
54
+
55
+ ## From `56.0.0-beta.0`
56
+
57
+ `beta.0` was cut from `release-56.0.0`. This beta is cut from `master` after that branch
58
+ merged (`#976`), and also includes `@terreno/comms` plus the sync/Mongoose 9 fixes listed
59
+ in `CHANGELOG.md` for this version.
@@ -17,9 +17,11 @@ Documentation for the Terreno monorepo: shared packages for full-stack applicati
17
17
  | [@terreno/api](reference/api.md) | REST API framework (Express/Mongoose) |
18
18
  | [@terreno/ui](reference/ui.md) | React Native UI component library |
19
19
  | [@terreno/rtk](reference/rtk.md) | RTK Query utilities for API backends |
20
+ | [@terreno/syncdb](reference/syncdb.md) | Local-first data layer (offline sync, conflicts) |
20
21
  | [@terreno/admin-backend](reference/admin-backend.md) | Admin panel backend plugin |
21
22
  | [@terreno/admin-frontend](reference/admin-frontend.md) | Admin panel frontend screens |
22
23
  | [@terreno/api-health](reference/api-health.md) | Health check endpoint plugin |
24
+ | [@terreno/comms](reference/comms.md) | Pluggable communications providers and delivery logging |
23
25
  | [@terreno/mcp](reference/mcp-server.md) | AI coding assistant integration (MCP) |
24
26
 
25
27
  ## Quick links
@@ -417,6 +417,27 @@ SES, SMTP) get their own items when demand appears.
417
417
 
418
418
  ---
419
419
 
420
+ ## comms-admin-dashboard
421
+
422
+ **Title:** `[Roadmap] Comms admin dashboard (errors, retries, log digging)`
423
+
424
+ **Labels:** `area:admin`, `type:feature`
425
+ **Project fields:** Area=`admin`, Target=`Next`, Impact=`Feature`, IP=`comms-admin-dashboard`, Status=`Planned`
426
+
427
+ Makes the admin panel the operations surface for the `@terreno/comms` layer: filterable
428
+ delivery logs (channel, provider, status, error code/class, recipient, date range, free
429
+ text), a message detail view with per-attempt history and raw provider metadata for log
430
+ digging, one-click and bulk retry of failed sends, and a stats endpoint for failure-rate
431
+ cards. Builds on the error taxonomy, lifecycle hooks, and payload retention added to the
432
+ comms abstraction.
433
+
434
+ - **Implementation plan:** [comms-admin-dashboard.md](https://github.com/FlourishHealth/terreno/blob/master/docs/implementationPlans/comms-admin-dashboard.md)
435
+ - **Tasks:** [comms-admin-dashboard.md](https://github.com/FlourishHealth/terreno/blob/master/docs/tasks/comms-admin-dashboard.md)
436
+ - **RTK flag:** Partial — screens use the generated SDK; migrate with syncdb like other admin screens
437
+ - **Depends on:** comms-abstraction
438
+
439
+ ---
440
+
420
441
  ## password-reset-and-email-verification
421
442
 
422
443
  **Title:** `[Roadmap] Password reset and email verification`
@@ -17,6 +17,8 @@ Problem-oriented, practical steps. Use these when you know what you want to do.
17
17
  - [Configure Better Auth](configure-better-auth.md) — Set up Better Auth with social OAuth (Google, GitHub, Apple)
18
18
  - [Add WebSocket integration](websocket-integration.md) — Set up real-time Socket.io connections
19
19
  - [Call external APIs](call-external-apis.md) — Authenticated HTTP client, retries, and error normalization for third-party integrations
20
+ - [Deploy to Google Cloud Platform](deploy-to-gcp.md) — Deploy demo and example apps to GCS with CDN
21
+ - [Migrate from @terreno/rtk to @terreno/syncdb](migrate-rtk-to-syncdb.md) — Move data sync to the local-first layer
20
22
 
21
23
  ## Coming Soon
22
24
 
@@ -0,0 +1,445 @@
1
+ # How to Migrate from @terreno/rtk to @terreno/syncdb
2
+
3
+ This guide is **AI-context-first**: it is written for an agent (or human) performing a screen-by-screen migration, not as a codemod. Work through one concern at a time — reads, then writes, then conflicts, then sync-status UX — and delete RTK-era code as you go.
4
+
5
+ `@terreno/syncdb` supersedes `@terreno/rtk` for **data synchronization** (CRUD reads/writes, offline queue, realtime convergence). It does not replace the generated OpenAPI SDK, Better Auth Redux wiring, or feature-flag infrastructure.
6
+
7
+ ## 1. Before you start
8
+
9
+ | Decision | Requirement | Why |
10
+ |----------|-------------|-----|
11
+ | **Better Auth first?** | Recommended before or in parallel with data migration | `betterAuthAdapter` is the shipped `AuthProvider`; socket sessions work cleanly with `RealtimeApp({betterAuth})`. Legacy JWT sockets still work during transition. |
12
+ | **Backend version** | `@terreno/api` with `SyncApp`, `RealtimeApp`, `syncPlugin`, `isDeletedPlugin` | Without backend sync registration, the client has nothing to talk to. |
13
+ | **MongoDB replica set** | Required for `RealtimeApp` change streams | Single-node replset is fine for dev. |
14
+ | **Per-collection rollout** | One synced collection at a time | REST and sync share the same write path; migrate screen-by-screen. |
15
+ | **Redux** | Keep Redux for auth, feature flags, app state; drop RTK Query for migrated collections | See §2. |
16
+
17
+ **Auth can migrate before data (M3).** A typical two-step order:
18
+
19
+ 1. **Step A — Auth:** Move login/session to Better Auth (`generateBetterAuthSlice`, `betterAuthAdapter`, `syncDb.start()` on login). Screens can still read/write via RTK Query during this step.
20
+ 2. **Step B — Data:** For each collection, replace RTK Query hooks with syncdb hooks and delete the RTK path for that screen.
21
+
22
+ The example app completed both steps; todos are syncdb-only while profile/admin still use generated RTK hooks.
23
+
24
+ Reference: [`docs/reference/syncdb.md`](../reference/syncdb.md), and the package [`syncdb/README.md`](https://github.com/flourishhealth/terreno/blob/release-56.0.0/syncdb/README.md).
25
+
26
+ ## 2. Install and configure
27
+
28
+ ### Install packages
29
+
30
+ ```bash
31
+ bun install @terreno/syncdb
32
+ bunx expo install expo-sqlite # native persistence — install in the app, rebuild native
33
+ ```
34
+
35
+ ### Create the sync client
36
+
37
+ **After** (`example-frontend/store/syncdb.ts`):
38
+
39
+ ```typescript
40
+ import {baseUrl} from "@terreno/rtk";
41
+ import {betterAuthAdapter, createSyncDb} from "@terreno/syncdb";
42
+ import {betterAuthClient} from "@/lib/betterAuth";
43
+
44
+ const authProvider = betterAuthAdapter(betterAuthClient, {pollIntervalMs: 60_000});
45
+
46
+ export const syncDb = createSyncDb({
47
+ authProvider,
48
+ baseUrl,
49
+ collections: ["todos"],
50
+ name: "terreno-example",
51
+ debug: __DEV__ ? {capacity: 1000} : false,
52
+ });
53
+ ```
54
+
55
+ ### Start/stop on auth
56
+
57
+ **After** (`example-frontend/app/_layout.tsx`):
58
+
59
+ ```typescript
60
+ useEffect(() => {
61
+ if (!userId) {
62
+ return;
63
+ }
64
+ let stopped = false;
65
+ syncDb
66
+ .start()
67
+ .then(() => {
68
+ if (!stopped) {
69
+ setSyncDbReady(true);
70
+ }
71
+ })
72
+ .catch((error) => {
73
+ console.error("[syncdb] Failed to start client", error);
74
+ });
75
+ return () => {
76
+ stopped = true;
77
+ setSyncDbReady(false);
78
+ void syncDb.stop();
79
+ };
80
+ }, [userId, syncDbStartAttempt]);
81
+ ```
82
+
83
+ Gate writes with a ready flag (`useSyncDbReady`) so `mutate()` is not called before `start()` resolves a user.
84
+
85
+ ### Wrap screens with `SyncDbProvider`
86
+
87
+ **After** (`example-frontend/app/(tabs)/index.tsx`):
88
+
89
+ ```tsx
90
+ import {SyncDbProvider} from "@terreno/syncdb/react";
91
+ import {syncDb} from "@/store/syncdb";
92
+
93
+ const TodosScreen = () => (
94
+ <SyncDbProvider client={syncDb}>
95
+ <SyncTodosScreen />
96
+ </SyncDbProvider>
97
+ );
98
+ ```
99
+
100
+ The example also mounts `SyncDbProvider` at the root (`_layout.tsx`) for `SyncHealthToast`.
101
+
102
+ ### Redux store: keep vs remove
103
+
104
+ | Keep on Redux | Remove for migrated collections |
105
+ |---------------|--------------------------------|
106
+ | `generateBetterAuthSlice` / session | `generateAuthSlice` (JWT) once Better Auth ships |
107
+ | `terrenoApi` reducer for **non-synced** routes | `useGetTodosQuery` / todo mutations |
108
+ | `createOfflineMiddleware` + offline slice | `realtimeList` / `realtimeDocument` for that collection |
109
+ | Feature-flag / app-state slices | Per-screen `isLoading` spinners tied to RTK mutations |
110
+
111
+ **Before** (`origin/master:example-frontend/store/index.ts`): `generateAuthSlice`, `createOfflineMiddleware`, offline payload sessionStorage.
112
+
113
+ **After** (`example-frontend/store/index.ts`): `generateBetterAuthSlice`, `terrenoApi` only — no offline middleware.
114
+
115
+ ## 3. Reads
116
+
117
+ There is no `isLoading` for reads after bootstrap — data is already in the local store.
118
+
119
+ ### List reads
120
+
121
+ **Before** (`origin/master:example-frontend/app/(tabs)/index.tsx`):
122
+
123
+ ```typescript
124
+ const {data: todosData, isLoading, refetch, isFetching} = useGetTodosQuery({}, {skip: !userId});
125
+ const todos = todosData?.data ?? [];
126
+ ```
127
+
128
+ **After** (`example-frontend/components/SyncTodosScreen.tsx`):
129
+
130
+ ```typescript
131
+ import {useEntity, useEntityIds} from "@terreno/syncdb/react";
132
+
133
+ const incompleteIds = useEntityIds<SyncTodo>("todos", {
134
+ filter: (t) => !t.completed,
135
+ sort: sortByCreatedDesc,
136
+ });
137
+
138
+ // Per row:
139
+ const {data} = useEntity<SyncTodo>("todos", id);
140
+ ```
141
+
142
+ For small lists, `useQuery` is fine:
143
+
144
+ ```typescript
145
+ import {useQuery} from "@terreno/syncdb/react";
146
+ const todos = useQuery<Todo>("todos", {filter: (t) => !t.completed});
147
+ ```
148
+
149
+ `filter` / `sort` run in JS — there are no server query params. Tombstones are excluded unless `{includeDeleted: true}`.
150
+
151
+ ### Delete this RTK read code
152
+
153
+ - `useGetTodosQuery` / `useGet*Query` for the migrated collection
154
+ - `isLoading`, `isFetching`, `refetch`, `RefreshControl` wired to list fetch
155
+ - `skip: !userId` guards on list queries (replace with `useSyncDbReady` on writes; reads are safe once bootstrapped)
156
+ - `realtimeList("todos")` / `realtimeDocument("todos")` in `store/sdk.ts` for that collection
157
+
158
+ ## 4. Writes
159
+
160
+ Writes are synchronous from the UI's perspective: local apply + outbox enqueue + background replay.
161
+
162
+ ### Create / update / delete
163
+
164
+ **Before** (`origin/master:example-frontend/app/(tabs)/index.tsx`):
165
+
166
+ ```typescript
167
+ const [createTodo, {isLoading: isCreating}] = usePostTodosMutation();
168
+ const [updateTodo] = usePatchTodosByIdMutation();
169
+ const [deleteTodo] = useDeleteTodosByIdMutation();
170
+
171
+ await createTodo({title, completed: false}).unwrap();
172
+ await updateTodo({id, completed: true}).unwrap();
173
+ await deleteTodo(id).unwrap();
174
+ ```
175
+
176
+ **After** (`example-frontend/components/SyncTodosScreen.tsx`):
177
+
178
+ ```typescript
179
+ import {generateMutationId} from "@terreno/syncdb";
180
+ import {useMutate} from "@terreno/syncdb/react";
181
+
182
+ const {create, update, remove} = useMutate("todos");
183
+
184
+ const id = generateMutationId();
185
+ create({data: {_id: id, title, completed: false, created: DateTime.now().toISO()}});
186
+ update({id, data: {completed: true}});
187
+ remove({id});
188
+ ```
189
+
190
+ Client-minted `_id` on creates lets the optimistic row render before the server acks.
191
+
192
+ ### Delete this RTK write code
193
+
194
+ - `usePost*Mutation`, `usePatch*Mutation`, `useDelete*Mutation` for the migrated collection
195
+ - `.unwrap()` / try-catch around mutation promises for happy-path UX
196
+ - **`isLoading` / `isCreating` / `isUpdating` spinners on write buttons** — syncdb writes do not await the server
197
+ - Manual optimistic cache updates (`updateQueryData`, `upsertQueryData`, temporary local rows in component state)
198
+ - `refetch()` or tag invalidation after mutate (`invalidatesTags` for that collection)
199
+ - `createOfflineMiddleware` endpoint list entries for that collection (`configureOfflineMutationEndpoints`)
200
+ - `If-Unmodified-Since` / 409 handling in custom mutation code (conflicts move to `useConflicts()`)
201
+
202
+ Server rejection surfaces later as outbox nacks — conflicts in `useConflicts()`, terminal failures in `getSyncStatus().failedCount`.
203
+
204
+ ## 5. Conflicts
205
+
206
+ A **conflict** means the client's `baseVersion` (last seen `_syncSeq`) disagrees with the server. The server returns a `conflict` nack:
207
+
208
+ - HTTP: `POST /sync/mutate` → **409** with `{nack: {code: "conflict", serverDoc, serverSeq, ...}}`
209
+ - Socket: `sync:nack` with `code: "conflict"`
210
+
211
+ The client records the conflict locally; the UI must let the user pick a side.
212
+
213
+ ### Strategies
214
+
215
+ | Strategy | When to use |
216
+ |----------|-------------|
217
+ | `useServer` | Accept canonical server data (or tombstone if server deleted the doc) |
218
+ | `keepMine` | Re-enqueue local changes with a fresh `baseVersion` |
219
+
220
+ ```typescript
221
+ import {useConflicts} from "@terreno/syncdb/react";
222
+
223
+ const {conflicts, resolve} = useConflicts();
224
+ resolve({mutationId, strategy: "useServer"});
225
+ resolve({mutationId, strategy: "keepMine"});
226
+ ```
227
+
228
+ ### Minimum UI
229
+
230
+ The example app uses:
231
+
232
+ - `SyncConflictsController` — single app-wide sheet state (`example-frontend/components/SyncConflictsController.tsx`)
233
+ - `ConflictSheet` rendered from `SyncHealthToast` (`example-frontend/app/_layout.tsx`)
234
+ - `SyncStatusBanner` conflict badge → `openConflicts("todos")` (`example-frontend/components/SyncTodosScreen.tsx`)
235
+
236
+ Copy that pattern or build equivalent: show local vs server fields, two resolution actions, block further edits on conflicted entities until resolved.
237
+
238
+ ## 6. Sync status
239
+
240
+ Replace **request-loading UX** with **sync-state UX**.
241
+
242
+ | RTK era | Syncdb era |
243
+ |---------|------------|
244
+ | `isLoading` on queries | No list loading spinner (data is local) |
245
+ | `useOfflineStatus` / `useServerStatus` | `useSyncStatus()` |
246
+ | `OfflineBanner` on fetch failure | `SyncStatusBanner` + `SyncHealthToast` |
247
+ | Pull-to-refresh `refetch()` | Optional `client.forceResync()` for support tooling |
248
+
249
+ **After** (`example-frontend/components/SyncTodosScreen.tsx`):
250
+
251
+ ```typescript
252
+ import {useSyncStatus} from "@terreno/syncdb/react";
253
+ import {SyncStatusBanner} from "@terreno/ui";
254
+
255
+ const syncStatus = useSyncStatus();
256
+
257
+ <SyncStatusBanner
258
+ isOnline={syncStatus.isOnline}
259
+ queuedCount={syncStatus.queuedCount}
260
+ conflictCount={syncStatus.conflictCount}
261
+ failedCount={syncStatus.failedCount}
262
+ paused={syncStatus.paused}
263
+ draining={syncStatus.draining}
264
+ sentThisDrain={syncStatus.sentThisDrain}
265
+ totalThisDrain={syncStatus.totalThisDrain}
266
+ onOpenConflicts={openConflictSheet}
267
+ onAuthRequired={handleAuthRequired}
268
+ />
269
+ ```
270
+
271
+ `SyncHealthToast` (`example-frontend/components/SyncHealthToast.tsx`) adds persistent toasts for per-collection conflicts/failures and an initial "Syncing…" toast with optional force reload.
272
+
273
+ `paused: "auth"` means replay is halted until the same user re-authenticates — wire `onAuthRequired` to your login flow.
274
+
275
+ ## 7. Auth
276
+
277
+ **Auth can ship before data migration.** Complete Better Auth + `betterAuthAdapter` first; keep RTK Query for reads/writes until each screen migrates.
278
+
279
+ ### Better Auth adapter
280
+
281
+ **After** (`example-frontend/store/syncdb.ts`):
282
+
283
+ ```typescript
284
+ import {betterAuthAdapter, createSyncDb} from "@terreno/syncdb";
285
+ import {betterAuthClient} from "@/lib/betterAuth";
286
+
287
+ const authProvider = betterAuthAdapter(syncAuthClient, {pollIntervalMs: 60_000});
288
+
289
+ export const syncDb = createSyncDb({authProvider, baseUrl, collections: ["todos"], name: "terreno-example"});
290
+ ```
291
+
292
+ `betterAuthAdapter` implements `AuthProvider`:
293
+
294
+ - `getToken()` / `getUserId()` — read session per request (not cached)
295
+ - `onAuthChange` — session atom subscription (example bridges `$store.atoms.session`)
296
+ - `refresh()` — one silent refresh per auth-pause episode
297
+
298
+ ### Token storage
299
+
300
+ Better Auth session tokens live in the Better Auth client (`@terreno/rtk` `createBetterAuthClient`):
301
+
302
+ - **Native:** `expo-secure-store`
303
+ - **Web:** `@react-native-async-storage/async-storage`
304
+
305
+ Syncdb does not store tokens itself — it calls `authProvider` on every transport/HTTP request.
306
+
307
+ ### Socket auth and `sync:auth-expired`
308
+
309
+ On connect, the socket transport sends the bearer token from `getToken()`. When the server's session re-validation sweep finds an expired/disabled session, it emits `sync:auth-expired` then disconnects (`api/src/realtime/sessionRevalidation.ts`). The client maps this to `paused: "auth"` — **no local wipe**, outbox preserved (INV-2).
310
+
311
+ ### Backend
312
+
313
+ ```typescript
314
+ new RealtimeApp({betterAuth: {auth, userModel: User}})
315
+ ```
316
+
317
+ Required for Better Auth socket sessions. Legacy JWT sockets continue to work without it.
318
+
319
+ ## 8. Codegen
320
+
321
+ ### Does `bun run sdk` still work?
322
+
323
+ **Yes.** `example-frontend/package.json` still defines:
324
+
325
+ ```json
326
+ "sdk": "bun scripts/generate-sdk.ts && bun biome check --write scripts/generate-sdk.ts"
327
+ ```
328
+
329
+ It runs `@rtk-query/codegen-openapi` against `openapi-config.ts` and writes `store/openApiSdk.ts`.
330
+
331
+ ### What changes post-migration
332
+
333
+ | Still generated | Stop using for migrated collections |
334
+ |-----------------|-------------------------------------|
335
+ | Auth routes, `GET /auth/me`, admin, AI explorer, version config, feature flags | `useGetTodosQuery`, `usePostTodosMutation`, etc. |
336
+ | Custom REST / RPC endpoints | Todo `realtimeList` / `realtimeDocument` wiring in `store/sdk.ts` |
337
+
338
+ Keep running `bun run sdk` after backend route changes — non-synced screens still import from `store/sdk.ts`.
339
+
340
+ Workflow:
341
+
342
+ ```bash
343
+ # Backend running on port 4000
344
+ cd example-frontend && bun run sdk
345
+ ```
346
+
347
+ ## 9. Feature flags
348
+
349
+ Post-migration, feature flags stay on `@terreno/rtk` + OpenFeature — **not** on syncdb.
350
+
351
+ **After** (`example-frontend/app/_layout.tsx`):
352
+
353
+ ```typescript
354
+ import {OpenFeatureProvider} from "@openfeature/react-sdk";
355
+ import {useTerrenoFeatureFlags} from "@terreno/rtk";
356
+
357
+ const OpenFeatureBridge = ({children, socket}) => {
358
+ const bridgeUserId = useSelector(selectBetterAuthUserId) ?? undefined;
359
+ useTerrenoFeatureFlags(terrenoApi, {skip: !bridgeUserId, socket, userId: bridgeUserId});
360
+ return <OpenFeatureProvider domain="feature-flags">{children}</OpenFeatureProvider>;
361
+ };
362
+ ```
363
+
364
+ See [`docs/reference/feature-flags.md`](../reference/feature-flags.md) and [`docs/how-to/add-feature-flags.md`](add-feature-flags.md).
365
+
366
+ **Example app gap:** todos do **not** gate on a feature flag — `SyncTodosScreen` is syncdb-only. For a gradual rollout, add a boolean flag and render the RTK screen when off, syncdb screen when on (see §10). The reference migration intentionally skipped flag wiring to keep one data path for e2e tests.
367
+
368
+ ## 10. Rollback
369
+
370
+ ### Per-screen feature flag
371
+
372
+ Ship both implementations behind a flag:
373
+
374
+ - Flag **off** → legacy RTK Query screen
375
+ - Flag **on** → syncdb screen
376
+
377
+ Both bundles ship until you flip the default and delete the RTK path.
378
+
379
+ The example app does **not** use this pattern for todos.
380
+
381
+ ### What local data survives
382
+
383
+ | Event | Local IndexedDB / SQLite | Outbox |
384
+ |-------|------------------------|--------|
385
+ | Logout / 401 | **Kept** (INV-2) | **Kept** — same user can sign back in and replay |
386
+ | Different-user login | **Wiped** | **Wiped** |
387
+ | `signOut()` default | **Kept** | **Kept** |
388
+ | `signOut()` with `wipeOnSignOut: true` | **Wiped** | **Wiped** |
389
+ | Schema version mismatch | **Wiped** + re-bootstrap | Re-built |
390
+ | Decrypt failure (web, default) | **Wiped** + re-bootstrap | Re-built |
391
+
392
+ `wipeLocalData` clears store tables, persisted databases, and cached encryption keys (`syncdb/src/storage/wipe.ts`).
393
+
394
+ Rolling back a **screen** to RTK does not automatically clear syncdb data — stale local todos may still exist until user switch or explicit wipe. Plan for that if you flip the flag off in production.
395
+
396
+ ## 11. Checklist
397
+
398
+ Copy and track per collection:
399
+
400
+ ```
401
+ [ ] 1. Backend: isDeletedPlugin + syncPlugin on schema
402
+ [ ] 2. Backend: modelRouter sync scope configured
403
+ [ ] 3. Backend: SyncApp + RealtimeApp registered; Mongo replica set running
404
+ [ ] 4. Backend: ensureSyncIndexes succeeds at startup
405
+ [ ] 5. Frontend: @terreno/syncdb installed; expo-sqlite in app (native)
406
+ [ ] 6. Frontend: createSyncDb client + betterAuthAdapter
407
+ [ ] 7. Frontend: syncDb.start()/stop() wired to auth in root layout
408
+ [ ] 8. Frontend: SyncDbProvider wraps migrated screens
409
+ [ ] 9. Reads: useGet*Query → useQuery / useEntityIds + useEntity
410
+ [ ] 10. Writes: usePost/Patch/Delete*Mutation → useMutate().create/update/remove
411
+ [ ] 11. Deleted RTK: mutation isLoading spinners, refetch-after-mutate, offline middleware entries, realtime cache patches
412
+ [ ] 12. Conflicts: useConflicts + resolution UI (ConflictSheet or equivalent)
413
+ [ ] 13. Sync status: useSyncStatus + SyncStatusBanner / health toasts (not query isLoading)
414
+ [ ] 14. Auth: Better Auth session + onAuthRequired → re-login when paused
415
+ [ ] 15. Codegen: bun run sdk still run for non-synced routes; remove todo hooks from screens
416
+ [ ] 16. Feature flags: OpenFeature bridge still fed via useTerrenoFeatureFlags (if used)
417
+ [ ] 17. Rollback plan: feature flag or accept stale local data on revert
418
+ [ ] 18. Verify: CRUD, offline create/edit/delete, reconnect, conflict both strategies, user switch
419
+ ```
420
+
421
+ ## Backend prerequisites (detail)
422
+
423
+ Before a collection can sync:
424
+
425
+ - [ ] `isDeletedPlugin` on schema (soft delete required)
426
+ - [ ] `syncPlugin` on schema (`_syncSeq` stamping)
427
+ - [ ] `modelRouter("/path", Model, {sync: {scope: ...}})` three-argument form
428
+ - [ ] Custom scopes include `snapshotFilter`
429
+ - [ ] `new SyncApp({getUserScopes?})` for tenant/custom scopes
430
+ - [ ] `new RealtimeApp({betterAuth?})` + MongoDB replica set
431
+ - [ ] No `updateMany`, `deleteMany`, `deleteOne`, `findOneAndDelete`, or `bulkWrite` on synced models
432
+
433
+ ## What stays on @terreno/rtk
434
+
435
+ - Generated OpenAPI SDK for non-synced routes
436
+ - Better Auth Redux slice (`generateBetterAuthSlice`)
437
+ - Feature flags (`useTerrenoFeatureFlags` / `useFeatureFlags`)
438
+ - Socket connection for flags and legacy realtime (until removed)
439
+ - `@terreno/rtk` is not being removed
440
+
441
+ ## Further reading
442
+
443
+ - [`docs/reference/syncdb.md`](../reference/syncdb.md) — full API reference
444
+ - [`syncdb/README.md`](https://github.com/flourishhealth/terreno/blob/release-56.0.0/syncdb/README.md) — architecture and protocol detail
445
+ - [`docs/how-to/configure-better-auth.md`](configure-better-auth.md) — Better Auth setup