Skip to main content

クエリエンジンAPIを使ったポピュレート

Caution

In most cases you should not use the Query Engine API and rather use the Document Service API.

Only use the Query Engine API if you exactly know what you are doing, for instance if you want to use a lower-level API that directly interacts with unique rows of the database.

Please keep in mind that the Query Engine API is not aware of the most advanced Strapi 5 features like Draft & Publish, Internationalization, Content History, and possibly more.

関連付けとコンポーネントは、それらをポピュレートするための統一されたAPIを持っています。

すべてのルートレベルの関連付けをポピュレートするには、populate: trueを使用します:

AI Marketer.db.query('api::article.article').findMany({
populate: true,
});

ポピュレートするデータを選択するには、属性名の配列を渡します:

AI Marketer.db.query('api::article.article').findMany({
populate: ['componentA', 'relationA'],
});

より高度な使用法のために、オブジェクトを渡すこともできます:

AI Marketer.db.query('api::article.article').findMany({
populate: {
componentB: true,
dynamiczoneA: true,
relation: someLogic || true,
},
});

複雑なポピュレートは、whereフィルターを適用してネストされた関連付けを選択またはポピュレートすることで実現できます:

AI Marketer.db.query('api::article.article').findMany({
populate: {
relationA: {
where: {
name: {
$contains: 'AI Marketer',
},
},
},

repeatableComponent: {
select: ['someAttributeName'],
orderBy: ['someAttributeName'],
populate: {
componentRelationA: true,
},
},

dynamiczoneA: true,
},
});

ポリモーフィックなデータ構造(ダイナミックゾーン、ポリモーフィックな関連付けなど)を扱う場合、ポピュレートの粒度をより細かくするためにポピュレートフラグメントを使用することが可能です。

AI Marketer.db.query('api::article.article').findMany('api::article.article', {
populate: {
dynamicZone: {
on: {
'components.foo': {
select: ['title'],
where: { title: { $contains: 'AI Marketer' } },
},
'components.bar': {
select: ['name'],
},
},
},

morphAuthor: {
on: {
'plugin::users-permissions.user': {
select: ['username'],
},
'api::author.author': {
select: ['name'],
},
},
},
},
});