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
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]>
Copy file name to clipboardExpand all lines: docusaurus/docs/cms/api/document-service.md
+66-37Lines changed: 66 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,7 @@ tags:
8
8
- Documents
9
9
- documentId
10
10
- Document Service API
11
+
toc_max_heading_level: 4
11
12
---
12
13
13
14
# Document Service API
@@ -44,13 +45,41 @@ Additional information on how to migrate from the Entity Service API to the Docu
44
45
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).
45
46
:::
46
47
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()`
48
77
49
78
Find a document matching the passed `documentId` and parameters.
|[`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 |
61
90
|[`populate`](/cms/api/document-service/populate)|[Populate](/cms/api/document-service/populate) results with additional fields. |`null`| Object |
62
91
63
-
### Example
92
+
####Example
64
93
65
94
If only a `documentId` is passed without any other parameters, `findOne()` returns the draft version of a document in the default locale:
|[`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 |
111
140
|[`populate`](/cms/api/document-service/populate)|[Populate](/cms/api/document-service/populate) results with additional fields. |`null`| Object |
112
141
113
-
### Examples
142
+
####Examples
114
143
115
144
<br />
116
145
117
-
#### Generic example
146
+
#####Generic example
118
147
119
148
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):
|[`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'`|
344
373
|[`populate`](/cms/api/document-service/populate)|[Populate](/cms/api/document-service/populate) results with additional fields. |`null`| Object |
345
374
346
-
### Example
375
+
####Example
347
376
348
377
If no `locale` parameter is passed, `create()` creates the draft version of the document for the default locale:
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)).
@@ -408,7 +437,7 @@ To update a document and publish the new version right away, you can:
408
437
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).
409
438
:::
410
439
411
-
### Example
440
+
####Example
412
441
413
442
If no `locale` parameter is passed, `update()` updates the document for the default locale:
|[`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 |
466
495
|[`populate`](/cms/api/document-service/populate)|[Populate](/cms/api/document-service/populate) results with additional fields. |`null`| Object |
467
496
468
-
### Example
497
+
####Example
469
498
470
499
If no `locale` parameter is passed, `delete()` only deletes the default locale version of a document. This deletes both the draft and published versions:
|[`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 |
535
564
|[`populate`](/cms/api/document-service/populate)|[Populate](/cms/api/document-service/populate) results with additional fields. |`null`| Object |
536
565
537
-
### Example
566
+
####Example
538
567
539
568
If no `locale` parameter is passed, `publish()` only publishes the default locale version of the document:
|[`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 |
600
629
|[`populate`](/cms/api/document-service/populate)|[Populate](/cms/api/document-service/populate) results with additional fields. |`null`| Object |
601
630
602
-
### Example
631
+
####Example
603
632
604
633
If no `locale` parameter is passed, `unpublish()` only unpublishes the default locale version of the document:
|[`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 |
655
684
|[`populate`](/cms/api/document-service/populate)|[Populate](/cms/api/document-service/populate) results with additional fields. |`null`| Object |
656
685
657
-
### Example
686
+
####Example
658
687
659
688
If no `locale` parameter is passed, `discardDraft()` discards draft data and overrides it with the published version only for the default locale:
Count the number of documents that match the provided parameters.
697
726
698
727
Syntax: `count(parameters: Params) => number`
699
728
700
-
### Parameters
729
+
####Parameters
701
730
702
731
| Parameter | Description | Default | Type |
703
732
|-----------|-------------|---------|------|
@@ -711,11 +740,11 @@ Since published documents necessarily also have a draft counterpart, a published
711
740
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.
712
741
:::
713
742
714
-
### Examples
743
+
####Examples
715
744
716
745
<br />
717
746
718
-
#### Generic example
747
+
#####Generic example
719
748
720
749
If no parameter is passed, the `count()` method the total number of documents for the default locale:
0 commit comments