Strapi v5 深插件
Strapi v5 插件, 它会自动在单个请求中填充所有嵌入式关系 populate: '*'。 。 。 .
它不会对筑巢水平施加限制,并且可以缓存聚居对象来改进性能.
特征
- 自动解决并填充所有嵌入式关系
- 支持包括动态区域在内的所有关系类型
- 处理循环参考和边缘案例
- 包括为改进业绩而进行的缓存
- 荣誉
populateCreatorFields设置 - 支持可选性 允许/ 拒绝列表, 用于人群中的特定关系或组件
安装
npm install @fourlights/strapi-plugin-deep-populate
使用量
在您的 Strapi 配置中启用深层人口 :
// config/plugins.js
module.exports = ({ env }) => ({
'deep-populate': {
enabled: true,
config: {
useCache: true, // default
replaceWildcard: true, // default
}
}
});
基本用途
// Get fully populated document
const document = await strapi.documents("api.page.page").findOne({
documentId: 'xyz',
populate: '*'
});
高级使用
// Get populate object for custom usage
const populate = await strapi.plugin("deep-populate")
.service("populate")
.get({
documentId: 'xyz',
contentType: 'api::page.page',
omitEmpty: true // optional
});
const document = await strapi.documents('api::page.page').findOne({
documentId: 'xyz',
populate
});
省略Empty和本地化
插件提供了精细的控件, 通过 : omitEmpty 财务报告和审定财务报表 localizations 选项.
全局配置
设定所有内容类型的默认行为 :
// config/plugins.js
module.exports = ({ env }) => ({
'deep-populate': {
enabled: true,
config: {
useCache: true,
replaceWildcard: true,
omitEmpty: true, // Omit empty attributes by default
localizations: false, // Exclude localizations by default
}
}
});
内容- 类型特定配置
覆盖特定内容类型的全局设置 :
// config/plugins.js
module.exports = ({ env }) => ({
'deep-populate': {
enabled: true,
config: {
useCache: true,
replaceWildcard: true,
omitEmpty: true,
localizations: false,
contentTypes: {
'api::page.page': {
omitEmpty: false, // Include all attributes for pages
localizations: true, // Always include localizations for pages
},
'api::blog-post.blog-post': {
omitEmpty: true, // Explicitly omit empty attributes
localizations: true, // But include localizations for blog posts
}
}
}
}
});
函数参数配置
每个请求覆盖全局和内容类型设置 :
// Get populate object with custom settings
const populate = await strapi.plugin("deep-populate")
.service("populate")
.get({
documentId: 'xyz',
contentType: 'api::page.page',
omitEmpty: true, // Override content-type setting
localizations: false // Override content-type setting
});
覆盖优先级
按下列优先排序(最高至最低)应用设置:
- 函数参数 - 直接传给了
get()方法 - 内容类型配置 - 具体内容类型
- 全球配置 - 默认插件设置
特殊行为
- 何时
localizations: true,即使omitEmpty: true - 何时
localizations: false中,将本地化排除在外,而不论omitEmpty设置 - 这个
localizations选项只影响已启用 i18n 的内容类型
缓存
插件缓存会填充对象以提高性能. 可禁用缓存
通过 useCache 设置。 数据库中持续保存缓存全集,并可以
在内容类型更新后变得僵化。 你可以用这个 cacheOptions > clearCacheOnStartup 以强制在服务器启动时进行缓存清理.
快取配置
// config/plugins.js
module.exports = ({ env }) => ({
'deep-populate': {
enabled: true,
config: {
useCache: true, // Enable caching (default: true)
cacheOptions: {
clearCacheOnStartup: false, // Clear cache on server startup (default: false)
warmOnWrite: false, // Re-warm cache synchronously after writes; NOTE: can be slow (default: false)
}
}
}
});
创建者字段
插件自动填充 createdBy 财务报告和审定财务报表 updatedBy 字段时 populateCreatorFields 在内容类型配置中启用.
允许/ 拒绝列表
有时,你可能想要限制某些关系或组成部分的嵌入群. 例如,如果你有一个 Page 深筑巢穴的温度 Link 组件与另一个 Page。 。 。 .
在这种情况下,您可以使用许可列表或拒绝列表来控制插件应该停止解决嵌入关系的地方.
// config/plugins.js
module.exports = ({ env }) => ({
'deep-populate': {
enabled: true,
config: {
useCache: true,
replaceWildcard: true,
contentTypes: {
// '*' would apply to all content types
'api::page.page': {
deny: {
relations: ['api::page.page'] // prevent resolving nested pages when populating a page
// alternatively we could have denied the link component in this case
// components: ['shared.link']
}
}
}
}
}
});
插件如何工作
插件递归 :
- 扭曲内容类型方案
- 获取带有一层深聚的文档
- 解决所有嵌入式关系
- 返回完整的填充对象
这一过程处理所有关系类型,包括动态区和循环引用.
解决问题
由于斯特拉皮的活泼性, 东西变得相当快 并且有时会变得很困难 通过树木看到谚语森林. 那样的话,就随便开口吧 我会尽力帮你的.
但是,首先,确保你:
- 使用该插件的最新版本
- 使用 Strapi 的最新版本
- 查看是否解除缓存解决了问题
- 检查您的内容类型是否仍然有效, 例如, 没有引用已删除组件的动态区域 .
- 请检查access-date=中的日期值 (帮助) API 响应中您没有标为私有属性/关系
如果那样的话,就把问题打开! 确保您报告所使用的版本( plugin & pidei) , 并最好共享受影响的内容类型定义 。 或更好, 复制问题.
恒星历史
感谢阅读和使用插件. 如果你喜欢的话,就考虑当主演 给我一个漂亮的多巴胺打击下一次我的工作.