Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<br/>useLiveQuery"] -- "createCmsCollectionOptions()" --> http["/api/cms/v1"] --> ops
agents["MCP, WebMCP,<br/>content agent"] --> ops
ops["CMS operations<br/>@tanstack/cms-api"] --> core["@tanstack/cms-core<br/>validation, access,<br/>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(
Expand Down
2 changes: 2 additions & 0 deletions docs/architecture/data-layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
199 changes: 199 additions & 0 deletions docs/architecture/overview.md
Original file line number Diff line number Diff line change
@@ -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()<br/>@tanstack/cms-ui-react/virtual-routes"]
admin["Admin UI<br/>/content, /edit/$collection/$id,<br/>/review, /agent"]
app["App loaders and components"]
http["HTTP API<br/>/api/cms/v1"]
mcp["MCP server<br/>/api/mcp"]
webmcp["WebMCP RPC<br/>/api/cms"]
end

browserAgent["Browser agent<br/>(WebMCP tools)"]
mcpClient["External MCP client"]
client["@tanstack/cms-client"]

routes --> admin
routes --> http
routes --> mcp
routes --> webmcp

ops["@tanstack/cms-api<br/>operations registry (ops)"]
contentAgent["In-app content agent<br/>(agentChat)"]
core["@tanstack/cms-core<br/>validation, hooks, access,<br/>review queue, revisions"]
storage[("StorageAdapter")]

admin -- "server functions<br/>@tanstack/cms-api/start" --> ops
app -- "server functions<br/>@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<br/>@tanstack/cms-api/start"]
httpApi["HTTP API<br/>/api/cms/v1"]
ops["ops<br/>@tanstack/cms-api"]
core["@tanstack/cms-core"]
md[(".md files with frontmatter")]
loadMd["loadMarkdownDir()<br/>@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<br/>createCmsCollectionOptions()<br/>@tanstack/cms-client/db"]
otherColl["TanStack DB collection<br/>from another service"]
live["useLiveQuery<br/>(join on product id)"]
ui["Component"]
end

service["Other service<br/>(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<br/>validation, hooks, access,<br/>review queue, scheduling"]

subgraph storageBoundary["StorageAdapter: defineCms({ storage }) or collection.storage"]
sqlite["sqliteStorage()<br/>node:sqlite (default)"]
d1["d1Storage()<br/>Cloudflare D1 (experimental)"]
markdown["markdownStorage()<br/>.md files (read-only)"]
postgres["postgresStorage()<br/>@tanstack/cms-store-postgres"]
libsql["libsqlStorage()<br/>@tanstack/cms-store-libsql"]
end

subgraph mediaBoundary["MediaAdapter: defineCms({ media })"]
local["localMedia()<br/>local disk (default)"]
s3["s3Media()<br/>@tanstack/cms-media-s3 (S3 / R2)"]
end

sharp["sharp (optional)<br/>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<br/>agentChat"]
mcp["mcp<br/>/api/mcp"]
webmcp["webmcp<br/>/api/cms"]
end

author["Author without<br/>publish rights (saveDoc)"]
tools["cmsTools() / ops"]
stage["stageWrite()<br/>validate, field rules,<br/>coalesce per document"]
queue[("Review queue<br/>/review")]
decide{"Editor decides<br/>(review.decide)"}
reject["rejectChange<br/>pending dropped, audited"]
live["Published<br/>(revision recorded)"]
draft["Unpublished draft<br/>with publishAt"]
sched["runScheduledPublishing()<br/>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
```
2 changes: 2 additions & 0 deletions docs/architecture/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<id>`.

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:
Expand Down
5 changes: 5 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading