查询引擎 API
¥Query Engine API
在深入了解查询引擎 API 文档之前,建议你阅读以下介绍:
¥Before diving deeper into the Query Engine API documentation, it is recommended that you read the following introductions:
和 内容 API 介绍。
¥and the Content API introduction.
Strapi 后端提供了一个查询引擎 API 来与更底层的数据库层进行交互。查询引擎 API 主要应由插件开发者和向其应用添加自定义业务逻辑的开发者使用。
¥The Strapi backend provides a Query Engine API to interact with the database layer at a lower level. The Query Engine API should mostly be used by plugin developers and developers adding custom business logic to their applications.
👉 在大多数用例中,建议使用 实体服务 API 而不是查询引擎 API。
¥👉 In most use cases, it's recommended to use the Entity Service API instead of the Query Engine API.
Strapi v4 提供了多个层来与后端交互并构建查询:
¥Strapi v4 offers several layers to interact with the backend and build your queries:
实体服务 API 是推荐使用的 API,用于与应用的数据库进行交互。实体服务是处理 Strapi 复杂数据结构(如组件和动态区域)的层,而较低层不知道这些数据结构。
¥The Entity Service API is the recommended API to interact with your application's database. The Entity Service is the layer that handles Strapi's complex data structures like components and dynamic zones, which the lower-level layers are not aware of.
查询引擎 API 与更底层的数据库层交互,并在幕后用于执行数据库查询。它提供对数据库层的不受限制的内部访问,但仅当实体服务 API 不涵盖你的用例时才应使用。
¥The Query Engine API interacts with the database layer at a lower level and is used under the hood to execute database queries. It gives unrestricted internal access to the database layer, but should be used only if the Entity Service API does not cover your use case.
如果你需要直接访问
knex
功能,请使用strapi.db.connection
。¥If you need direct access to
knex
functions, usestrapi.db.connection
.
基本用法
¥Basic usage
查询引擎可通过 strapi.db.query
获得:
¥The Query Engine is available through strapi.db.query
:
strapi.db.query('api::blog.article').findMany({ // uid syntax: 'api::api-name.content-type-name'
where: {
title: {
$startsWith: '2021',
$endsWith: 'v4',
},
},
populate: {
category: true,
},
});
可用操作
¥Available operations
查询引擎允许对数据库条目执行以下操作:
¥The Query Engine allows the following operations on database entries:
📄️ Single operations
Create, read, update, and delete single database entries with the Query Engine API.
📄️ Bulk operations
Create, read, update, and delete multiple database entries with the Query Engine API.
📄️ Filters
Get exactly what you need by filtering database entries with the Query Engine API.
📄️ Populate
Get additional data with your Query Engine API queries by populating relations.
📄️ Order & Pagination
Sort and paginate the results of your Query Engine API queries.