Skip to content

Commit 2cd860d

Browse files
Document Service API intro rework: more details, updated structure (#2880)
* Rework Document Service API intro w/ more information and different structure * Improve wording a bit * Re-organize a bit the Document objects paragraph * Fix i18n broken link due to capitalization --------- Co-authored-by: Pierre Wizla <[email protected]> Co-authored-by: Pierre Wizla <[email protected]>
1 parent 053a1ff commit 2cd860d

File tree

2 files changed

+98
-69
lines changed

2 files changed

+98
-69
lines changed

docusaurus/docs/cms/api/document-service.md

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tags:
88
- Documents
99
- documentId
1010
- Document Service API
11+
toc_max_heading_level: 4
1112
---
1213

1314
# Document Service API
@@ -44,13 +45,41 @@ Additional information on how to migrate from the Entity Service API to the Docu
4445
Relations can also be connected, disconnected, and set through the Document Service API, just like with the REST API (see the [REST API relations documentation](/cms/api/rest/relations) for examples).
4546
:::
4647

47-
## `findOne()`
48+
## Document objects
49+
50+
Document methods return a document object or a list of document objects, which represent a version of a content entry grouped under a stable `documentId`. Returned objects typically include:
51+
52+
- `documentId`: Persistent identifier for the entry across locales and draft/published versions.
53+
- `id`: Database identifier for the specific locale/version record.
54+
- model fields: All fields defined in the content-type schema. Relations, components, and dynamic zones are not populated unless you opt in with `populate` (see [Populating fields](/cms/api/document-service/populate)) or limit fields with `fields` (see [Selecting fields](/cms/api/document-service/fields)).
55+
- metadata: `publishedAt`, `createdAt`, `updatedAt`, and `createdBy`/`updatedBy` when available.
56+
57+
Optionally, document objects can also include a `status` and `locale` property if [Draft & Publish](/cms/features/draft-and-publish) and [Internationalization](/cms/features/internationalization) are enabled for the content-type.
58+
59+
## Method overview
60+
61+
Each section below documents the parameters and examples for a specific method:
62+
63+
| Method | Purpose |
64+
| --- | --- |
65+
| [`findOne()`](#findone) | Fetch a document by `documentId`, optionally scoping to a locale or status. |
66+
| [`findFirst()`](#findfirst) | Return the first document that matches filters. |
67+
| [`findMany()`](#findmany) | List documents with filters, sorting, and pagination. |
68+
| [`create()`](#create) | Create a document, optionally targeting a locale. |
69+
| [`update()`](#update) | Update a document by `documentId`. |
70+
| [`delete()`](#delete) | Delete a document or a specific locale version. |
71+
| [`publish()`](#publish) | Publish the draft version of a document. |
72+
| [`unpublish()`](#unpublish) | Move a published document back to draft. |
73+
| [`discardDraft()`](#discarddraft) | Drop draft data and keep only the published version. |
74+
| [`count()`](#count) | Count how many documents match the parameters. |
75+
76+
### `findOne()`
4877

4978
Find a document matching the passed `documentId` and parameters.
5079

5180
Syntax: `findOne(parameters: Params) => Document`
5281

53-
### Parameters
82+
#### Parameters
5483

5584
| Parameter | Description | Default | Type |
5685
|-----------|-------------|---------|------|
@@ -60,7 +89,7 @@ Syntax: `findOne(parameters: Params) => Document`
6089
| [`fields`](/cms/api/document-service/fields#findone) | [Select fields](/cms/api/document-service/fields#findone) to return | All fields<br/>(except those not populated by default) | Object |
6190
| [`populate`](/cms/api/document-service/populate) | [Populate](/cms/api/document-service/populate) results with additional fields. | `null` | Object |
6291

63-
### Example
92+
#### Example
6493

6594
If only a `documentId` is passed without any other parameters, `findOne()` returns the draft version of a document in the default locale:
6695

@@ -94,13 +123,13 @@ await strapi.documents('api::restaurant.restaurant').findOne({
94123

95124
The `findOne()` method returns the matching document if found, otherwise returns `null`.
96125

97-
## `findFirst()`
126+
### `findFirst()`
98127

99128
Find the first document matching the parameters.
100129

101130
Syntax: `findFirst(parameters: Params) => Document`
102131

103-
### Parameters
132+
#### Parameters
104133

105134
| Parameter | Description | Default | Type |
106135
|-----------|-------------|---------|------|
@@ -110,11 +139,11 @@ Syntax: `findFirst(parameters: Params) => Document`
110139
| [`fields`](/cms/api/document-service/fields#findfirst) | [Select fields](/cms/api/document-service/fields#findfirst) to return | All fields<br/>(except those not populate by default) | Object |
111140
| [`populate`](/cms/api/document-service/populate) | [Populate](/cms/api/document-service/populate) results with additional fields. | `null` | Object |
112141

113-
### Examples
142+
#### Examples
114143

115144
<br />
116145

117-
#### Generic example
146+
##### Generic example
118147

119148
By default, `findFirst()` returns the draft version, in the default locale, of the first document for the passed unique identifier (collection type id or single type id):
120149

@@ -144,7 +173,7 @@ await strapi.documents('api::restaurant.restaurant').findFirst()
144173

145174
</ApiCall>
146175

147-
#### Find the first document matching parameters
176+
##### Find the first document matching parameters
148177

149178
Pass some parameters to `findFirst()` to return the first document matching them.
150179

@@ -184,13 +213,13 @@ await strapi.documents('api::restaurant.restaurant').findFirst(
184213

185214
</ApiCall>
186215

187-
## `findMany()`
216+
### `findMany()`
188217

189218
Find documents matching the parameters.
190219

191220
Syntax: `findMany(parameters: Params) => Document[]`
192221

193-
### Parameters
222+
#### Parameters
194223

195224
| Parameter | Description | Default | Type |
196225
|-----------|-------------|---------|------|
@@ -202,11 +231,11 @@ Syntax: `findMany(parameters: Params) => Document[]`
202231
| [`pagination`](/cms/api/document-service/sort-pagination#pagination) | [Paginate](/cms/api/document-service/sort-pagination#pagination) results |
203232
| [`sort`](/cms/api/document-service/sort-pagination#sort) | [Sort](/cms/api/document-service/sort-pagination#sort) results | | |
204233

205-
### Examples
234+
#### Examples
206235

207236
<br />
208237

209-
#### Generic example
238+
##### Generic example
210239

211240
When no parameter is passed, `findMany()` returns the draft version in the default locale for each document:
212241

@@ -245,7 +274,7 @@ await strapi.documents('api::restaurant.restaurant').findMany()
245274

246275
</ApiCall>
247276

248-
#### Find documents matching parameters
277+
##### Find documents matching parameters
249278

250279
Available filters are detailed in the [filters](/cms/api/document-service/filters) page of the Document Service API reference.
251280

@@ -326,15 +355,15 @@ await documents('api:restaurant.restaurant').findMany({ locale: ['en', 'it'] } )
326355
327356
</Request> -->
328357

329-
## `create()`
358+
### `create()`
330359

331360
Creates a drafted document and returns it.
332361

333362
Pass fields for the content to create in a `data` object.
334363

335364
Syntax: `create(parameters: Params) => Document`
336365

337-
### Parameters
366+
#### Parameters
338367

339368
| Parameter | Description | Default | Type |
340369
|-----------|-------------|---------|------|
@@ -343,7 +372,7 @@ Syntax: `create(parameters: Params) => Document`
343372
| [`status`](/cms/api/document-service/status#create) | _If [Draft & Publish](/cms/features/draft-and-publish) is enabled for the content-type_:<br/>Can be set to `'published'` to automatically publish the draft version of a document while creating it | -| `'published'` |
344373
| [`populate`](/cms/api/document-service/populate) | [Populate](/cms/api/document-service/populate) results with additional fields. | `null` | Object |
345374

346-
### Example
375+
#### Example
347376

348377
If no `locale` parameter is passed, `create()` creates the draft version of the document for the default locale:
349378

@@ -379,13 +408,13 @@ await strapi.documents('api::restaurant.restaurant').create({
379408
If the [Draft & Publish](/cms/features/draft-and-publish) feature is enabled on the content-type, you can automatically publish a document while creating it (see [`status` documentation](/cms/api/document-service/status#create)).
380409
:::
381410

382-
## `update()`
411+
### `update()`
383412

384413
Updates document versions and returns them.
385414

386415
Syntax: `update(parameters: Params) => Promise<Document>`
387416

388-
### Parameters
417+
#### Parameters
389418

390419
| Parameter | Description | Default | Type |
391420
|-----------|-------------|---------|------|
@@ -408,7 +437,7 @@ To update a document and publish the new version right away, you can:
408437
It's not recommended to update repeatable components with the Document Service API (see the related [breaking change entry](/cms/migration/v4-to-v5/breaking-changes/do-not-update-repeatable-components-with-document-service-api.md) for more details).
409438
:::
410439

411-
### Example
440+
#### Example
412441

413442
If no `locale` parameter is passed, `update()` updates the document for the default locale:
414443

@@ -449,13 +478,13 @@ await strapi.documents('api::restaurant.restaurant').update({
449478
await documents('api:restaurant.restaurant').update(documentId, {locale: ['es', 'en'], data: {name: "updatedName" }}
450479
``` -->
451480

452-
## `delete()`
481+
### `delete()`
453482

454483
Deletes one document, or a specific locale of it.
455484

456485
Syntax: `delete(parameters: Params): Promise<{ documentId: ID, entries: Number }>`
457486

458-
### Parameters
487+
#### Parameters
459488

460489
| Parameter | Description | Default | Type |
461490
|-----------|-------------|---------|------|
@@ -465,7 +494,7 @@ Syntax: `delete(parameters: Params): Promise<{ documentId: ID, entries: Number }
465494
| [`fields`](/cms/api/document-service/fields#delete) | [Select fields](/cms/api/document-service/fields#delete) to return | All fields<br/>(except those not populate by default) | Object |
466495
| [`populate`](/cms/api/document-service/populate) | [Populate](/cms/api/document-service/populate) results with additional fields. | `null` | Object |
467496

468-
### Example
497+
#### Example
469498

470499
If no `locale` parameter is passed, `delete()` only deletes the default locale version of a document. This deletes both the draft and published versions:
471500

@@ -516,15 +545,15 @@ await strapi.documents('api::restaurant.restaurant').delete(
516545
517546
</Request> -->
518547

519-
## `publish()`
548+
### `publish()`
520549

521550
Publishes one or multiple locales of a document.
522551

523552
This method is only available if [Draft & Publish](/cms/features/draft-and-publish) is enabled on the content-type.
524553

525554
Syntax: `publish(parameters: Params): Promise<{ documentId: ID, entries: Number }>`
526555

527-
### Parameters
556+
#### Parameters
528557

529558
| Parameter | Description | Default | Type |
530559
|-----------|-------------|---------|------|
@@ -534,7 +563,7 @@ Syntax: `publish(parameters: Params): Promise<{ documentId: ID, entries: Number
534563
| [`fields`](/cms/api/document-service/fields#publish) | [Select fields](/cms/api/document-service/fields#publish) to return | All fields<br/>(except those not populate by default) | Object |
535564
| [`populate`](/cms/api/document-service/populate) | [Populate](/cms/api/document-service/populate) results with additional fields. | `null` | Object |
536565

537-
### Example
566+
#### Example
538567

539568
If no `locale` parameter is passed, `publish()` only publishes the default locale version of the document:
540569

@@ -581,15 +610,15 @@ await strapi.documents('api::restaurant.restaurant').publish(
581610
);
582611
``` -->
583612

584-
## `unpublish()`
613+
### `unpublish()`
585614

586615
Unpublishes one or all locale versions of a document, and returns how many locale versions were unpublished.
587616

588617
This method is only available if [Draft & Publish](/cms/features/draft-and-publish) is enabled on the content-type.
589618

590619
Syntax: `unpublish(parameters: Params): Promise<{ documentId: ID, entries: Number }>`
591620

592-
### Parameters
621+
#### Parameters
593622

594623
| Parameter | Description | Default | Type |
595624
|-----------|-------------|---------|------|
@@ -599,7 +628,7 @@ Syntax: `unpublish(parameters: Params): Promise<{ documentId: ID, entries: Numbe
599628
| [`fields`](/cms/api/document-service/fields#unpublish) | [Select fields](/cms/api/document-service/fields#unpublish) to return | All fields<br/>(except those not populate by default) | Object |
600629
| [`populate`](/cms/api/document-service/populate) | [Populate](/cms/api/document-service/populate) results with additional fields. | `null` | Object |
601630

602-
### Example
631+
#### Example
603632

604633
If no `locale` parameter is passed, `unpublish()` only unpublishes the default locale version of the document:
605634

@@ -636,15 +665,15 @@ await strapi.documents('api::restaurant.restaurant').unpublish({
636665

637666
</ApiCall>
638667

639-
## `discardDraft()`
668+
### `discardDraft()`
640669

641670
Discards draft data and overrides it with the published version.
642671

643672
This method is only available if [Draft & Publish](/cms/features/draft-and-publish) is enabled on the content-type.
644673

645674
Syntax: `discardDraft(parameters: Params): Promise<{ documentId: ID, entries: Number }>`
646675

647-
### Parameters
676+
#### Parameters
648677

649678
| Parameter | Description | Default | Type |
650679
|-----------|-------------|---------|------|
@@ -654,7 +683,7 @@ Syntax: `discardDraft(parameters: Params): Promise<{ documentId: ID, entries: Nu
654683
| [`fields`](/cms/api/document-service/fields#discarddraft) | [Select fields](/cms/api/document-service/fields#discarddraft) to return | All fields<br/>(except those not populate by default) | Object |
655684
| [`populate`](/cms/api/document-service/populate) | [Populate](/cms/api/document-service/populate) results with additional fields. | `null` | Object |
656685

657-
### Example
686+
#### Example
658687

659688
If no `locale` parameter is passed, `discardDraft()` discards draft data and overrides it with the published version only for the default locale:
660689

@@ -691,13 +720,13 @@ strapi.documents.discardDraft({
691720

692721
</ApiCall>
693722

694-
## `count()`
723+
### `count()`
695724

696725
Count the number of documents that match the provided parameters.
697726

698727
Syntax: `count(parameters: Params) => number`
699728

700-
### Parameters
729+
#### Parameters
701730

702731
| Parameter | Description | Default | Type |
703732
|-----------|-------------|---------|------|
@@ -711,11 +740,11 @@ Since published documents necessarily also have a draft counterpart, a published
711740
This means that counting with the `status: 'draft'` parameter still returns the total number of documents matching other parameters, even if some documents have already been published and are not displayed as "draft" or "modified" in the Content Manager anymore. There currently is no way to prevent already published documents from being counted.
712741
:::
713742

714-
### Examples
743+
#### Examples
715744

716745
<br />
717746

718-
#### Generic example
747+
##### Generic example
719748

720749
If no parameter is passed, the `count()` method the total number of documents for the default locale:
721750
<ApiCall>
@@ -730,7 +759,7 @@ await strapi.documents('api::restaurant.restaurant').count()
730759

731760
</ApiCall>
732761

733-
#### Count published documents
762+
##### Count published documents
734763

735764
To count only published documents, pass `status: 'published'` along with other parameters to the `count()` method.
736765

@@ -744,7 +773,7 @@ strapi.documents('api::restaurant.restaurant').count({ status: 'published' })
744773

745774
</Request>
746775

747-
#### Count documents with filters
776+
##### Count documents with filters
748777

749778
Any [filters](/cms/api/document-service/filters) can be passed to the `count()` method.
750779

0 commit comments

Comments
 (0)