You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 282f2a9
Browse filesBrowse the repository at this point in the historyBrowse files
feat!: get/list populate relations by default — depth 1, batched, cycle-safe, with typed related() (3b) (#35) (#35)
Chain step 18 of the linear merge chain onto main 3b4a112: this PR's own changes, resolved against the PRs before it in the merge order.
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
`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.
15
+
- A relation into a collection the reader can't read is `null` (or `[]`) instead of being left out of `relations`, so hidden and missing targets look the same.
Copy file name to clipboardExpand all lines: docs/api/cms-client.md
+6-3Lines changed: 6 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,19 +70,22 @@ console.log(`${items.length} of ${total} posts`)
70
70
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`.
`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.
A`locator`isthedocument's slug or its id, with leading slashes ignored. A document that is missing, or that `ctx` can'tsee, is`null`from`get`andabsentfrom`list`. Documentsareredactedfor`ctx`, andfilteringorsortingonafielditcan't read is `FORBIDDEN`. Both functions validate the bag (`getInput`, `listInput` from `@tanstack/cms-core`) and throw `BAD_REQUEST` for an invalid one.
Bothverbspopulaterelationsbydefault. Therelateddocumentscomebackbesidethedocument, under`relations`, keyedbyrelationfieldpath (`author`, `seo.image`, `blocks.2.author`); `doc`keepstheids. Populationisoneleveldeep: arelateddocument's own relation fields stay ids, so cycles and self-references can'trecurse. Arelateddocumentisreadandredactedas`ctx`readsit. Amissing, trashedorhiddentargetis`null`, ordroppedfroma`many`array, whetherthedocumentoritswholecollectionishidden, soareadercan't tell hidden from missing. `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'sdocumenttype, oranarrayfor`many: true`.
collections?: Array<string> // restrict a cross-collection query
223
233
query?: string // full-text search over title and content
224
234
locale?: string
225
-
populate?: boolean // attach `relations` to each item
235
+
populate?: PopulateSelection // attach `relations` to each item (default false)
226
236
where?: Record<string, unknown> | ReadonlyArray<WhereClause> // see "Filters" below
227
237
sort?: { field: string; dir?: 'asc' | 'desc' } // a system column or a field path (`seo.score`)
228
238
limit?: number
@@ -399,7 +409,7 @@ function populateRelationsAs(ctx: AccessContext, collectionName: string, doc: Do
399
409
function populateRelations(collectionName: string, doc: Doc): Promise<Record<string, Doc | Array<Doc> | null>> // deprecated: reads as publicContext()
400
410
```
401
411
402
-
Resolvesevery`relation`fieldof`doc`tothetargetdocument(s) `ctx`canread, keyedbyfieldname. A`publicRead`contextseespublishedtargetsthewayitseespublishedsources. Fieldswhosetargetcollection`ctx`can't read are left out.
412
+
Resolvesevery`relation`fieldof`doc`tothetargetdocument(s) `ctx`canread, keyedbyfieldname. Amissing, trashedorhiddentargetis`null` (droppedfroma`many`array), includingoneinacollection`ctx`can't read. A `publicRead` context sees published targets the way it sees published sources.
Copy file name to clipboardExpand all lines: docs/api/cms-core.md
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -813,6 +813,17 @@ The agent tool set, shared by the content agent, the HTTP MCP server, and WebMCP
813
813
814
814
These agent tools are separate from the HTTP API operations (`saveDoc`, `getPublicDocument`, …) in [`@tanstack/cms-api`](./cms-api).
815
815
816
+
### `related(result, path)`
817
+
818
+
```ts
819
+
function related<Cextendsstring, PextendsRelationPath<C>>(
820
+
result: { collection:C; relations?:Relations },
821
+
path:P,
822
+
):RelatedAt<C, P>
823
+
```
824
+
825
+
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 (anarrayfor`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.
Copy file name to clipboardExpand all lines: docs/api/routes-and-endpoints.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -237,8 +237,8 @@ Every operation is also a TanStack Start server function in [`@tanstack/cms-api/
237
237
| Operation | Method | Access | Input | Result |
238
238
| --- | --- | --- | --- | --- |
239
239
|`getAuthSession`| GET | public |`{}`| The caller's `CmsSession` plus `ai: { text, image }` capabilities. |
240
-
|`get`| GET | public |`{ collection, id \| locator, staged?, 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, unless `staged` is `false`. |
241
-
|`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`. |
240
+
|`get`| GET | public |`{ collection, id \| locator, staged?, 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, unless `staged` is `false`. Relations are populated one level deep unless `populate` is `false` (or a list of paths). |
241
+
|`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`. |
242
242
|`save`| POST |`content.write`|`{ collection, id, doc, create?, publish?, expectedRevision?, expectedPendingVersion?, instruction? }`|`{ id, collection, doc, state: { label, published, revision, changeSet }, availableActions }`. The canonical write ([`saveAs`](./cms-core-server#saveasctx-options-and-actasctx-options)): `doc` is a patch merged onto the caller's working copy. It commits for publishers (a draft of a published document unless `publish` is set) and is submitted for review otherwise. `CONFLICT` for a stale `expectedRevision` / `expectedPendingVersion`, or `create` on an existing id. |
243
243
|`act`| POST |`content.read`|`{ collection, id, action, publishAt?: string \| null, reason?, expectedVersion? }`| As `save`, with `doc``null` once the document is gone. Runs an action the document's strategy offers the caller (`FORBIDDEN` otherwise); `edit`, `submit`, `draft` and `import` are `save`'s (`BAD_REQUEST`). |
Copy file name to clipboardExpand all lines: docs/mcp/operations-registry.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -164,10 +164,10 @@ Agents get a smaller, task-shaped set of tools. `opSchemas` (`packages/core/src/
164
164
| Tool | Kind | Behavior |
165
165
| --- | --- | --- |
166
166
|`list_collections`| read | Collections and their fields |
167
-
|`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? }`|
168
-
|`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`|
167
+
|`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? }`|
168
+
|`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`|
169
169
|`read_document`| read | Deprecated, use `get_document`. One document by id, including staged creates and updates; `NOT_FOUND` when missing |
170
-
|`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`) |
170
+
|`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`) |
171
171
|`list_revisions`| read | Revision history, newest first |
172
172
|`list_translations`| read | A document's locale variants |
173
173
|`save_document`| write | Stage a new document or a change: `doc` holds only the fields to change, `create: true` refuses an existing id. Returns `{ id, collection, doc, state, availableActions }`|
@@ -178,7 +178,7 @@ Agents get a smaller, task-shaped set of tools. `opSchemas` (`packages/core/src/
178
178
|`restore_revision`| write | Stage restoring a revision |
179
179
|`schedule_document`| write | Stage setting or clearing `publishAt`|
180
180
181
-
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`.
181
+
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`.
182
182
183
183
The write tool comes from the canonical write vocabulary, `writeVerbs`, the same way: `save` → `save_document`, with the `saveInput` bag the `save` operation, `client.save` and `cmsAs(cms, ctx).save` take. An agent's save is always the strategy's `submit`, so it is staged for review. `act` (approve, reject, discard, …) is not an agent tool: review decisions stay with people.
0 commit comments