メインコンテンツまでスキップ

Document Service API: 下書きと公開の扱い

下書きと公開 が有効なとき、Document Service API は既定でドキュメントの下書き版を返します。このページでは status パラメータで次を行う方法を説明します。

  • ドキュメントの公開版を返す
  • ステータスに応じてドキュメントを数える
  • 作成または更新と同時に公開する
Note

Document Service API のクエリに { status: 'draft' } を渡した結果は、status を省略した場合と同じです。

findOne() で公開版を取得する

findOne() は既定でドキュメントの下書き版を返します。

Document Service API で 特定のドキュメントを取得 するときに公開版が欲しい場合は、status: 'published' を渡します。

Example request
await strapi.documents('api::restaurant.restaurant').findOne({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
status: 'published'
});
Example response
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: "2024-03-14T15:40:45.330Z",
locale: "en", // default locale
// …
}

findFirst() で公開版を取得する

findFirst() は既定でドキュメントの下書き版を返します。

Document Service API で 最初のドキュメントを取得 するときに公開版が欲しい場合は、status: 'published' を渡します。

リクエスト例
const document = await strapi.documents("api::restaurant.restaurant").findFirst({
status: 'published',
});
レスポンス例
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: "2024-03-14T15:40:45.330Z",
locale: "en", // default locale
// …
}

findMany() で公開版を取得する

findMany() は既定で各ドキュメントの下書き版を返します。

Document Service API で ドキュメント一覧を取得 するときに公開版が欲しい場合は、status: 'published' を渡します。

リクエスト例
const documents = await strapi.documents("api::restaurant.restaurant").findMany({
status: 'published'
});
レスポンス例
[
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: "2024-03-14T15:40:45.330Z",
locale: "en", // default locale
// …
}
// …
]

count() で下書きまたは公開のみを数える

Document Service API で ドキュメント数を数える とき、下書き版または公開版だけを考慮するには、対応する status を渡します。

// 下書きを数える(実際には公開済みも含まれる)
const draftsCount = await strapi.documents("api::restaurant.restaurant").count({
status: 'draft'
});
// 公開済みのみを数える
const publishedCount = await strapi.documents("api::restaurant.restaurant").count({
status: 'published'
});
Note

公開済みドキュメントには必ず下書き側の対応もあるため、公開済みでも下書き版があるものとして数えられます。

そのため status: 'draft' で数えても、他の条件に一致するドキュメントの総数が返ります。一部はすでに公開済みで、Content Manager 上では「下書き」や「変更あり」と表示されない場合でも同様です。公開済みをこのカウントから除外する方法は現状ありません。

下書きを作成して公開する

create() に渡すパラメータに status: 'published' を付けると、作成と同時にドキュメントを公開できます。

Example request
await strapi.documents('api::restaurant.restaurant').create({
data: {
name: "New Restaurant",
},
status: 'published',
})
Example response
{
documentId: "d41r46wac4xix5vpba7561at",
name: "New Restaurant",
publishedAt: "2024-03-14T17:29:03.399Z",
locale: "en" // default locale
// …
}

下書きを更新して公開する

update() に渡すパラメータに status: 'published' を付けると、更新と同時にドキュメントを公開できます。

Example request
await strapi.documents('api::restaurant.restaurant').update({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
data: {
name: "Biscotte Restaurant (closed)",
},
status: 'published',
})
Example response
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant (closed)",
publishedAt: "2024-03-14T17:29:03.399Z",
locale: "en" // default locale
// …
}