diff --git a/README.md b/README.md index 7ee0e205..b7ee63df 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,18 @@ TanStack Start app, and your UI reads its content through TanStack DB. `markdownStorage()` from `@tanstack/cms-core/storage` (or loaded directly via `loadMarkdownDir()` from `@tanstack/cms-api/markdown`). +```mermaid +flowchart LR + admin["Admin UI and app loaders"] -- "@tanstack/cms-api/start" --> ops + components["Components
useLiveQuery"] -- "createCmsCollectionOptions()" --> http["/api/cms/v1"] --> ops + agents["MCP, WebMCP,
content agent"] --> ops + ops["CMS operations
@tanstack/cms-api"] --> core["@tanstack/cms-core
validation, access,
revisions, review queue"] + core --> storage[("Storage and media adapters")] +``` + +See the [architecture overview](./docs/architecture/overview.md) for diagrams of +data loading, storage, mutations, and agent review. + ```ts // A CMS collection as a TanStack DB collection const products = createCollection( diff --git a/docs/architecture/data-layer.md b/docs/architecture/data-layer.md index 57440585..b995d29c 100644 --- a/docs/architecture/data-layer.md +++ b/docs/architecture/data-layer.md @@ -25,6 +25,8 @@ The CMS owns the content model, validation, revisions, and the review queue. Tan The built-in admin UI calls the server functions directly and doesn't use TanStack DB. TanStack DB is what you use in your own routes and components. +For diagrams of these paths, see [Data Loading](./overview#data-loading) and [Mutations](./overview#mutations) in the architecture overview. + ## Three Sources, One Query Model The repo has an example for each kind of source. All three read data with `useLiveQuery` from `@tanstack/react-db`; only the collection definitions differ. diff --git a/docs/architecture/overview.md b/docs/architecture/overview.md new file mode 100644 index 00000000..cd1f3e00 --- /dev/null +++ b/docs/architecture/overview.md @@ -0,0 +1,199 @@ +--- +title: Architecture Overview +id: overview +order: 0 +description: "Diagrams of how TanStack CMS fits together: the route groups and operations registry, how app code loads content through TanStack Start and TanStack DB, the storage and media adapters, the mutation path, and the agent review workflow." +keywords: + - tanstack cms + - architecture + - diagrams + - tanstack start + - tanstack db + - review queue + - storage adapters +--- + +TanStack CMS runs inside your TanStack Start app. Every surface (the admin, your own loaders and components, the HTTP API, MCP, WebMCP, and the in-app content agent) calls the same operations from `@tanstack/cms-api`, and those operations run on the engine in `@tanstack/cms-core`. The diagrams on this page show how those pieces connect. + +## Overview + +`cmsVirtualRoutes()` mounts the admin, public, and API routes from the package. Each route calls the operations registry (`ops` in `@tanstack/cms-api`), which checks access and then calls the engine. + +```mermaid +flowchart LR + subgraph start["Your TanStack Start app"] + routes["cmsVirtualRoutes()
@tanstack/cms-ui-react/virtual-routes"] + admin["Admin UI
/content, /edit/$collection/$id,
/review, /agent"] + app["App loaders and components"] + http["HTTP API
/api/cms/v1"] + mcp["MCP server
/api/mcp"] + webmcp["WebMCP RPC
/api/cms"] + end + + browserAgent["Browser agent
(WebMCP tools)"] + mcpClient["External MCP client"] + client["@tanstack/cms-client"] + + routes --> admin + routes --> http + routes --> mcp + routes --> webmcp + + ops["@tanstack/cms-api
operations registry (ops)"] + contentAgent["In-app content agent
(agentChat)"] + core["@tanstack/cms-core
validation, hooks, access,
review queue, revisions"] + storage[("StorageAdapter")] + + admin -- "server functions
@tanstack/cms-api/start" --> ops + app -- "server functions
@tanstack/cms-api/start" --> ops + client --> http --> ops + mcpClient --> mcp --> ops + browserAgent --> webmcp --> ops + admin --> contentAgent --> ops + ops --> core --> storage +``` + +## Data Loading + +The admin and your loaders call Start server functions from `@tanstack/cms-api/start` in-process, with no HTTP hop. Components use `createCmsCollectionOptions()` from `@tanstack/cms-client/db` to get a live TanStack DB collection, which reads through `listPublishedDocuments` and can be joined with collections from other services in one `useLiveQuery`. Markdown files are read-only: mount them with `markdownStorage()`, or read them into plain objects with `loadMarkdownDir()`. See [Data Layer with TanStack DB](./data-layer). + +```mermaid +flowchart TB + subgraph server["Server"] + fns["Start server functions
@tanstack/cms-api/start"] + httpApi["HTTP API
/api/cms/v1"] + ops["ops
@tanstack/cms-api"] + core["@tanstack/cms-core"] + md[(".md files with frontmatter")] + loadMd["loadMarkdownDir()
@tanstack/cms-api/markdown"] + fns --> ops + httpApi --> ops + ops --> core + core -- "markdownStorage() (read-only)" --> md + end + + loader["Admin and route loaders"] + + subgraph browser["Browser"] + cmsColl["TanStack DB collection
createCmsCollectionOptions()
@tanstack/cms-client/db"] + otherColl["TanStack DB collection
from another service"] + live["useLiveQuery
(join on product id)"] + ui["Component"] + end + + service["Other service
(prices, stock, ...)"] + + loader -- "in-process call" --> fns + cmsColl -- "listPublishedDocuments" --> httpApi + otherColl --> service + cmsColl --> live + otherColl --> live + live --> ui + loadMd -.->|"reads directly into plain objects"| md +``` + +## Data Storage + +`@tanstack/cms-core` owns all domain logic and talks to storage through the `StorageAdapter` interface, so an adapter only implements reads and writes. Configure it for the whole CMS with `defineCms({ storage })` or per collection with `collection.storage`. Media bytes go through a separate `MediaAdapter`. See [Storage & Media](./storage). + +```mermaid +flowchart TB + core["@tanstack/cms-core engine
validation, hooks, access,
review queue, scheduling"] + + subgraph storageBoundary["StorageAdapter: defineCms({ storage }) or collection.storage"] + sqlite["sqliteStorage()
node:sqlite (default)"] + d1["d1Storage()
Cloudflare D1 (experimental)"] + markdown["markdownStorage()
.md files (read-only)"] + postgres["postgresStorage()
@tanstack/cms-store-postgres"] + libsql["libsqlStorage()
@tanstack/cms-store-libsql"] + end + + subgraph mediaBoundary["MediaAdapter: defineCms({ media })"] + local["localMedia()
local disk (default)"] + s3["s3Media()
@tanstack/cms-media-s3 (S3 / R2)"] + end + + sharp["sharp (optional)
resizing, WebP / AVIF"] + + core --> sqlite + core --> d1 + core --> markdown + core --> postgres + core --> libsql + core --> local + core --> s3 + core -.->|"image variants"| sharp +``` + +## Mutations + +An optimistic `products.update()` on a collection from `createCmsCollectionOptions()` runs `saveDoc` with `publish: true` and the row's `_revision` as `expectedRevision`. The CMS operation checks access, then either stages the change for review or commits it. If the operation throws, TanStack DB rolls back the optimistic change. [Events & Hooks](./events-and-hooks) describes each hook stage. + +```mermaid +sequenceDiagram + autonumber + participant C as Component + participant DB as TanStack DB collection + participant Op as saveDoc (ops) + participant Core as cms-core store + participant Out as Outbox dispatcher + + C->>DB: products.update('acme-anvil', draft => ...) + DB-->>C: optimistic row (useLiveQuery re-renders) + DB->>Op: onUpdate: cms.op('saveDoc', publish: true, expectedRevision) + Op->>Op: access rules (update), protected fields, can publish? + alt caller cannot publish + Op->>Core: stageWrite() into the review queue + Core-->>Op: pending change + Op-->>DB: staged: true + DB->>Op: refetch listPublishedDocuments + Op-->>DB: live row keeps its old value + else caller can publish + Op->>Core: commitDocument() + Core->>Core: preValidate hooks, schema validation, field rules, postValidate hooks + Core->>Core: transaction: re-check access and expectedRevision, write document, record revision, enqueue events and webhooks + Core->>Core: afterChange hook, emit change / publish + Core-->>Op: committed document + Op-->>DB: staged: false + DB->>Op: refetch listPublishedDocuments + Core-)Out: deliver webhooks and event handlers after commit + end + opt validation, access, or CONFLICT error + Op--xDB: CmsError + DB-->>C: roll back the optimistic change + end +``` + +## Agent Writes and Review + +Agents never commit. The in-app content agent, MCP clients, and WebMCP tools call the same operations through `cmsTools()`, and every write is staged with `stageWrite()` as one pending change per document. An editor approves or rejects it at `/review`. Approving can publish immediately or keep the document as a draft, and a draft with a `publishAt` date is published by `runScheduledPublishing()`. See [Review Queue](../agents/review-queue). + +```mermaid +flowchart LR + subgraph agents["Agents (actor recorded)"] + contentAgent["content-agent
agentChat"] + mcp["mcp
/api/mcp"] + webmcp["webmcp
/api/cms"] + end + + author["Author without
publish rights (saveDoc)"] + tools["cmsTools() / ops"] + stage["stageWrite()
validate, field rules,
coalesce per document"] + queue[("Review queue
/review")] + decide{"Editor decides
(review.decide)"} + reject["rejectChange
pending dropped, audited"] + live["Published
(revision recorded)"] + draft["Unpublished draft
with publishAt"] + sched["runScheduledPublishing()
when publishAt is due"] + + contentAgent --> tools + mcp --> tools + webmcp --> tools + tools --> stage + author --> stage + stage --> queue --> decide + decide -- "approveChange (publish)" --> live + decide -- "approveChange (as draft)" --> draft + decide -- "reject" --> reject + draft --> sched --> live +``` diff --git a/docs/architecture/storage.md b/docs/architecture/storage.md index 3e86e715..5bfc46f7 100644 --- a/docs/architecture/storage.md +++ b/docs/architecture/storage.md @@ -25,6 +25,8 @@ TanStack CMS separates the content engine from where bytes live: The engine in `@tanstack/cms-core/server` owns all domain logic (validation, hooks, the review gate, scheduling, access control), so an adapter only implements plain reads and writes. Adapters share the engine and query contract; retention of deleted-document history can differ by backend. Documents always reference media by the portable URL `/api/media/`. +For a diagram of the adapter boundary, see [Data Storage](./overview#data-storage) in the architecture overview. + ## Defaults With no `storage` or `media` configured, everything lives under one data directory: diff --git a/docs/config.json b/docs/config.json index 51df9ed6..e6df9663 100644 --- a/docs/config.json +++ b/docs/config.json @@ -207,6 +207,11 @@ "label": "Architecture & Auth", "tab": "guides", "children": [ + { + "label": "Architecture Overview", + "to": "architecture/overview", + "addedAt": "2026-10-29" + }, { "label": "Routes in a Package", "to": "architecture/routes-in-a-package",