Skip to content

Commit c4c810f

Browse files
cursoragentjherr
andcommitted
docs: populated get/list, related(), and the agent tools' ids-only list/search default; changeset
Co-authored-by: jherr <jherr@pobox.com>
1 parent ec73eb2 commit c4c810f

7 files changed

Lines changed: 86 additions & 21 deletions

File tree

‎.changeset/populate-sidecar.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@tanstack/cms-core': minor
3+
'@tanstack/cms-api': minor
4+
'@tanstack/cms-client': minor
5+
---
6+
7+
`get` and `list` populate relations by default. The related documents come back beside each document under `relations`, keyed by relation field path (`author`, `blocks.2.author`), while `doc` keeps the ids. Population is one level deep, so cycles and self-references can't recurse, and it is batched: a page costs one storage read per target collection. Read a relation with the new `related(result, path)` (from `@tanstack/cms-core` and `@tanstack/cms-client`), typed from `Register`. `get`/`list` results are typed with their collection name.
8+
9+
`populate` takes `false` (ids only), `true`, or a list of paths (`['author', 'blocks']`), on every verb that accepts it. The agent tools `list_documents` and `search_documents` return ids unless the agent passes `populate`; `get_document` populates like `get`. The older verbs still populate only when asked.
10+
11+
**Breaking:**
12+
13+
- `get`, `list`, `client.get/list`, the `get`/`list` operations and `get_document` return `relations` unless called with `populate: false`.
14+
- Populated relations skip trashed documents: a relation to one is `null` (dropped from a `many` array), like a missing document.

‎docs/api/cms-client.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,22 @@ console.log(`${items.length} of ${total} posts`)
7070
The canonical reads, the `get` and `list` operations. They take the same bags as [`getAs` and `listAs`](./cms-core-server#getasctx-options-and-listasctx-options) on the server, and their document types come from `Register`.
7171

7272
```ts validate=type
73-
import { createCmsClient } from '@tanstack/cms-client'
73+
import { createCmsClient, related } from '@tanstack/cms-client'
7474

7575
const cms = createCmsClient({ baseUrl: 'https://cms.example.com', mode: 'live' })
7676

7777
const about = await cms.get({ collection: 'page', locator: 'about' })
78+
const author = about && related(about, 'author')
7879
const first = await cms.list({ collection: 'post', sort: { field: 'title' }, limit: 20 })
7980
const next = first.nextCursor
8081
? await cms.list({ collection: 'post', sort: { field: 'title' }, limit: 20, cursor: first.nextCursor })
8182
: undefined
82-
console.log(about?.doc, first.items.length, next?.total)
83+
console.log(about?.doc, author, first.items.length, next?.total)
8384
```
8485

85-
`get` resolves to `{ id, collection, doc }`, or `null` when the document is missing or the caller can't see it. `list` resolves to `{ items: [{ id, collection, doc }], total, nextCursor? }`. Both are public: anonymous callers read published documents, and callers who may read drafts see the working copy unless the client's `mode` is `live`.
86+
`get` resolves to `{ id, collection, doc, relations }`, or `null` when the document is missing or the caller can't see it. `list` resolves to `{ items: [{ id, collection, doc, relations }], total, nextCursor? }`. Both are public: anonymous callers read published documents, and callers who may read drafts see the working copy unless the client's `mode` is `live`.
87+
88+
Relations are populated by default, one level deep, under `relations` keyed by field path; `doc` keeps the ids. `related(result, path)` reads one back, typed from `Register` (the target's document type, an array for `many: true`, `null` when missing or hidden). Pass `populate: false` for ids only, or an array of paths (`populate: ['author']`) to populate just those.
8689

8790
### `op(name, input)` and `call<T>(name, input?)`
8891

‎docs/api/cms-core-server.md‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ function listAs(ctx: AccessContext, options?: ListOptions): Promise<ListResult>
140140
type GetOptions = {
141141
collection: string
142142
locale?: string // with `locator`: match the document in this locale
143-
populate?: boolean // default false
143+
populate?: PopulateSelection // default true
144144
} & ({ id: string } | { locator: string }) // exactly one
145145
146+
type PopulateSelection = boolean | ReadonlyArray<string> // every relation, none, or these paths
147+
146148
interface ListOptions {
147149
collection?: string // omit to list every collection ctx may read
148150
locale?: string // only documents in this locale
@@ -151,18 +153,18 @@ interface ListOptions {
151153
limit?: number // 1–500, default 100
152154
offset?: number
153155
cursor?: string // `nextCursor` from the previous page
154-
populate?: boolean // default false
156+
populate?: PopulateSelection // default true
155157
}
156158
157-
interface ReadResult<TDoc = Doc> {
159+
interface ReadResult<TDoc = Doc, C extends string = string> {
158160
id: string
159-
collection: string
161+
collection: C
160162
doc: TDoc
161-
relations?: Record<string, Doc | Array<Doc> | null> // with `populate`
163+
relations?: Record<string, Doc | Array<Doc> | null> // unless `populate: false`
162164
}
163165
164-
interface ListResult<TDoc = Doc> {
165-
items: Array<ReadResult<TDoc>>
166+
interface ListResult<TDoc = Doc, C extends string = string> {
167+
items: Array<ReadResult<TDoc, C>>
166168
total: number
167169
nextCursor?: string
168170
}
@@ -175,16 +177,24 @@ The canonical reads. One bag each, the same at every layer: the store (`cmsAs(cm
175177

176178
A `locator` is the document's slug or its id, with leading slashes ignored. A document that is missing, or that `ctx` can't see, is `null` from `get` and absent from `list`. Documents are redacted for `ctx`, and filtering or sorting on a field it can't read is `FORBIDDEN`. Both functions validate the bag (`getInput`, `listInput` from `@tanstack/cms-core`) and throw `BAD_REQUEST` for an invalid one.
177179

178-
Full-text search stays its own verb, `queryDocumentsAs` with `query`, and `search_documents` for agents. The older read verbs below keep their behaviour.
180+
Both verbs populate relations by default. The related documents come back beside the document, under `relations`, keyed by relation field path (`author`, `seo.image`, `blocks.2.author`); `doc` keeps the ids. Population is one level deep: a related document's own relation fields stay ids, so cycles and self-references can't recurse. A related document is read and redacted as `ctx` reads it. A missing, trashed or hidden target is `null`, or dropped from a `many` array, and a relation into a collection `ctx` can't read is left out. `list` batches the reads: one storage read per target collection for the whole page, not one per row. Pass `populate: false` for ids only, or an array of paths to populate just those (and the relations under them). `related(result, path)` from `@tanstack/cms-core` reads one relation back, typed from `Register`: a registered relation field gives the target's document type, or an array for `many: true`.
181+
182+
Full-text search stays its own verb, `queryDocumentsAs` with `query`, and `search_documents` for agents. The older read verbs below keep their behaviour, populating only when asked.
179183

180184
```ts validate=type
181-
import { listAs, publicContext } from '@tanstack/cms-core/server'
185+
import { related } from '@tanstack/cms-core'
186+
import { getAs, listAs, publicContext } from '@tanstack/cms-core/server'
187+
188+
export async function postWithAuthor(slug: string) {
189+
const post = await getAs(publicContext(), { collection: 'post', locator: slug })
190+
return post && { post: post.doc, author: related(post, 'author') }
191+
}
182192
183193
export async function allPosts() {
184194
const ids: Array<string> = []
185195
let cursor: string | undefined
186196
do {
187-
const page = await listAs(publicContext(), { collection: 'post', limit: 500, cursor })
197+
const page = await listAs(publicContext(), { collection: 'post', limit: 500, cursor, populate: false })
188198
ids.push(...page.items.map((i) => i.id))
189199
cursor = page.nextCursor
190200
} while (cursor)
@@ -203,7 +213,7 @@ interface QueryDocumentsOptions {
203213
collections?: Array<string> // restrict a cross-collection query
204214
query?: string // full-text search over title and content
205215
locale?: string
206-
populate?: boolean // attach `relations` to each item
216+
populate?: PopulateSelection // attach `relations` to each item (default false)
207217
where?: Record<string, unknown> | ReadonlyArray<WhereClause> // see "Filters" below
208218
sort?: { field: string; dir?: 'asc' | 'desc' } // a system column or a field path (`seo.score`)
209219
limit?: number
@@ -380,7 +390,7 @@ function populateRelationsAs(ctx: AccessContext, collectionName: string, doc: Do
380390
function populateRelations(collectionName: string, doc: Doc): Promise<Record<string, Doc | Array<Doc> | null>> // deprecated: reads as publicContext()
381391
```
382392

383-
Resolves every `relation` field of `doc` to the target document(s) `ctx` can read, keyed by field name. A `publicRead` context sees published targets the way it sees published sources. Fields whose target collection `ctx` can't read are left out.
393+
Resolves every `relation` field of `doc` to the target document(s) `ctx` can read, keyed by field name. A missing, trashed or hidden target is `null` (dropped from a `many` array). A `publicRead` context sees published targets the way it sees published sources. Fields whose target collection `ctx` can't read are left out.
384394

385395
### `listDocumentsAs(ctx, collection?, options?)`
386396

‎docs/api/cms-core.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,17 @@ The agent tool set, shared by the content agent, the HTTP MCP server, and WebMCP
646646
647647
These agent tools are separate from the HTTP API operations (`saveDoc`, `getPublicDocument`, …) in [`@tanstack/cms-api`](./cms-api).
648648
649+
### `related(result, path)`
650+
651+
```ts
652+
function related<C extends string, P extends RelationPath<C>>(
653+
result: { collection: C; relations?: Relations },
654+
path: P,
655+
): RelatedAt<C, P>
656+
```
657+
658+
The related document(s) at `path` of a populated `get`/`list` result (see [`getAs` and `listAs`](./cms-core-server#getasctx-options-and-listasctx-options)). Paths are relation field paths as `relations` keys them (`author`, `seo.image`, `blocks.2.author`). For a top-level relation field of a registered collection, the result is typed as the target's document (an array for `many: true`). It is `null` when the path wasn't populated, or the target is missing or hidden. `@tanstack/cms-client` exports the same function.
659+
649660
---
650661

651662
## Events

‎docs/api/routes-and-endpoints.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ Every operation is also a TanStack Start server function in [`@tanstack/cms-api/
214214
| Operation | Method | Access | Input | Result |
215215
| --- | --- | --- | --- | --- |
216216
| `getAuthSession` | GET | public | `{}` | The caller's `CmsSession` plus `ai: { text, image }` capabilities. |
217-
| `get` | GET | public | `{ collection, id \| locator, locale?, populate? }` | `{ id, collection, doc, relations? }` or `null`. The canonical read ([`getAs`](./cms-core-server#getasctx-options-and-listasctx-options)): published only, except for callers who may read drafts of the collection and don't ask for `live`; drafts are read with staged changes overlaid. |
218-
| `list` | GET | public | `{ collection?, locale?, where?, sort?: { field, dir? }, limit? (1–500, default 100), offset?, cursor?, populate? }` | `{ items: [{ id, collection, doc, relations? }], total, nextCursor? }`. The canonical list, with the same visibility as `get`. |
217+
| `get` | GET | public | `{ collection, id \| locator, locale?, populate? }` | `{ id, collection, doc, relations? }` or `null`. The canonical read ([`getAs`](./cms-core-server#getasctx-options-and-listasctx-options)): published only, except for callers who may read drafts of the collection and don't ask for `live`; drafts are read with staged changes overlaid. Relations are populated one level deep unless `populate` is `false` (or a list of paths). |
218+
| `list` | GET | public | `{ collection?, locale?, where?, sort?: { field, dir? }, limit? (1–500, default 100), offset?, cursor?, populate? }` | `{ items: [{ id, collection, doc, relations? }], total, nextCursor? }`. The canonical list, with the same visibility and populate default as `get`. |
219219
| `listDocumentSummaries` | GET | `content.read` | `{ collection, search?, status?: 'all' \| 'published' \| 'draft' \| 'scheduled', locale?, ids? (≤ 500), sort?: { field, dir? }, limit? (1–500, default 50), offset?, columns? (≤ 20) }` | `{ items: DocSummary[], total }` |
220220
| `getDocument` | GET | `content.read` | `{ collection, id }` | `{ doc, isPending, pendingId?, pendingSource?, livePublished }`, including any staged change. |
221221
| `resolveRedirect` | GET | public | `{ path }` | `{ to, status }` or `null` |

‎docs/deployment/upgrading.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,33 @@ and a later alpha moves the starters and docs over.
326326
- **`get` and `list` are reserved operation names**: an app op with either
327327
name now fails as a duplicate.
328328

329+
### 22. `get` and `list` populate relations by default
330+
331+
`get` and `list` (on the server, over HTTP and Start, on the client, and the
332+
`get_document` tool) return the related documents beside each document,
333+
under `relations`, keyed by relation field path. `doc` keeps the ids.
334+
Population is one level deep and batched per target collection. Read a
335+
relation with `related(result, path)`, typed from `Register`:
336+
337+
```ts
338+
import { related } from '@tanstack/cms-core'
339+
340+
const post = await cmsAs(cms, ctx).get({ collection: 'post', locator: 'hello' })
341+
const author = post && related(post, 'author')
342+
const ids = await cmsAs(cms, ctx).list({ collection: 'post', populate: false })
343+
```
344+
345+
Pass `populate: false` for ids only, or a list of paths
346+
(`populate: ['author']`). The agent tools `list_documents` and
347+
`search_documents` stay ids-only unless the agent passes `populate`. The
348+
older verbs still populate only when asked.
349+
350+
- **Populated relations skip trashed targets.** A relation to a trashed
351+
document is `null` (dropped from a `many` array), like a missing one; it
352+
used to come back with its `deletedAt` stamp.
353+
- **`populate` takes a list of paths** as well as a boolean, on every verb
354+
that accepts it.
355+
329356
## Versioning policy
330357

331358
From 1.0, TanStack CMS follows [semantic versioning](https://semver.org). The

‎docs/mcp/operations-registry.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ Agents get a smaller, task-shaped set of tools. `opSchemas` (`packages/core/src/
163163
| Tool | Kind | Behavior |
164164
| --- | --- | --- |
165165
| `list_collections` | read | Collections and their fields |
166-
| `list_documents` | read | The canonical `list`: documents of one collection (or every readable one) with `locale`, `where`, `sort`, paging (`limit`, `offset`, `cursor`) and `populate`. Returns `{ items: [{ id, collection, doc }], total, nextCursor? }` |
167-
| `get_document` | read | The canonical `get`: one document by `id` or `locator`, including staged creates and updates in the agent's `edit` mode. Returns `{ id, collection, doc }` or `null` |
166+
| `list_documents` | read | The canonical `list`: documents of one collection (or every readable one) with `locale`, `where`, `sort`, paging (`limit`, `offset`, `cursor`) and `populate` (default `false`: relation fields stay ids). Returns `{ items: [{ id, collection, doc, relations? }], total, nextCursor? }` |
167+
| `get_document` | read | The canonical `get`: one document by `id` or `locator`, including staged creates and updates in the agent's `edit` mode, with its relations populated (`populate: false` for ids only). Returns `{ id, collection, doc, relations }` or `null` |
168168
| `read_document` | read | Deprecated, use `get_document`. One document by id, including staged creates and updates; `NOT_FOUND` when missing |
169-
| `search_documents` | read | Full-text search and structured `where` / `sort` queries with collection, locale, a read `mode` (`live` for published only; never wider than the caller's), and pagination (`limit`, `offset`, `cursor`) |
169+
| `search_documents` | read | Full-text search and structured `where` / `sort` queries with collection, locale, a read `mode` (`live` for published only; never wider than the caller's), pagination (`limit`, `offset`, `cursor`), and `populate` (default `false`) |
170170
| `list_revisions` | read | Revision history, newest first |
171171
| `list_translations` | read | A document's locale variants |
172172
| `create_document` | write | Stage a new document |
@@ -176,7 +176,7 @@ Agents get a smaller, task-shaped set of tools. `opSchemas` (`packages/core/src/
176176
| `restore_revision` | write | Stage restoring a revision |
177177
| `schedule_document` | write | Stage setting or clearing `publishAt` |
178178

179-
The read tools come from the canonical read vocabulary, `readVerbs`: each verb's agent tool name is derived as `<verb>_<noun>` (`get` → `get_document`, `list` → `list_documents`, see `agentToolName`), and its input schema is the verb's bag (`getInput`, `listInput`), the same one the `get` and `list` operations, `client.get/list` and `cmsAs(cms, ctx).get/list` take. Full-text search stays its own tool, `search_documents`.
179+
The read tools come from the canonical read vocabulary, `readVerbs`: each verb's agent tool name is derived as `<verb>_<noun>` (`get` → `get_document`, `list` → `list_documents`, see `agentToolName`), and its input schema is the verb's bag (`getInput`, `listInput`), the same one the `get` and `list` operations, `client.get/list` and `cmsAs(cms, ctx).get/list` take. The one difference is the populate default: `get_document` populates like `get`, while `list_documents` and `search_documents` return ids unless the agent asks (`populate: true`, or the paths it needs), to keep tool results small. Full-text search stays its own tool, `search_documents`.
180180

181181
Two server-side pieces in `@tanstack/cms-core/server` attach behavior:
182182

0 commit comments

Comments
 (0)