有没有办法获得 Strapi CMS 内容类型的结构?
Is there a way to get a structure of a Strapi CMS Content Type?
一个 content-type“产品”具有以下字段:
string
标题
int
数量
string
描述
double
价格
是否有 API 端点来检索“产品”的结构或架构 content-type 而不是获取值?
例如:在端点 localhost:1337/products
上,响应可以是这样的:
[
{
field: "title",
type: "string",
other: "col-xs-12, col-5"
},
{
field: "qty",
type: "int"
},
{
field: "description",
type: "string"
},
{
field: "price",
type: "double"
}
]
模式结构或 table 发送到哪里而不是实际值?
如果不在 Strapi CMS 中,这在其他无头 CMS(例如 Hasura 和 Sanity)上是否可行?
您需要使用 Models,来自 link:
Models are a representation of the database's structure. They are split into two separate files. A JavaScript file that contains the model options (e.g: lifecycle hooks), and a JSON file that represents the data structure stored in the database.
这正是您所追求的。
我获取此信息的方式是添加自定义端点 - 请在此处查看我的答案以了解如何执行此操作 - & .
对于处理程序,您可以执行以下操作:
async getProductModel(ctx) {
return strapi.models['product'].allAttributes;
}
我需要所有内容类型的解决方案,所以我制作了一个带有 /modelStructure/*
端点的插件,您可以在其中提供模型名称,然后传递给处理程序:
//more generic wrapper
async getModel(ctx) {
const { model } = ctx.params;
let data = strapi.models[model].allAttributes;
return data;
},
async getProductModel(ctx) {
ctx.params['model'] = "product"
return this.getModel(ctx)
},
//define all endpoints you need, like maybe a Page content type
async getPageModel(ctx) {
ctx.params['model'] = "page"
return this.getModel(ctx)
},
//finally I ended up writing a `allModels` handler
async getAllModels(ctx) {
Object.keys(strapi.models).forEach(key => {
//iterate through all models
//possibly filter some models
//iterate through all fields
Object.keys(strapi.models[key].allAttributes).forEach(fieldKey => {
//build the response - iterate through models and all their fields
}
}
//return your desired custom response
}
欢迎提出意见和问题
一个 content-type“产品”具有以下字段:
string
标题int
数量string
描述double
价格
是否有 API 端点来检索“产品”的结构或架构 content-type 而不是获取值?
例如:在端点 localhost:1337/products
上,响应可以是这样的:
[
{
field: "title",
type: "string",
other: "col-xs-12, col-5"
},
{
field: "qty",
type: "int"
},
{
field: "description",
type: "string"
},
{
field: "price",
type: "double"
}
]
模式结构或 table 发送到哪里而不是实际值?
如果不在 Strapi CMS 中,这在其他无头 CMS(例如 Hasura 和 Sanity)上是否可行?
您需要使用 Models,来自 link:
Models are a representation of the database's structure. They are split into two separate files. A JavaScript file that contains the model options (e.g: lifecycle hooks), and a JSON file that represents the data structure stored in the database.
这正是您所追求的。
我获取此信息的方式是添加自定义端点 - 请在此处查看我的答案以了解如何执行此操作 -
对于处理程序,您可以执行以下操作:
async getProductModel(ctx) {
return strapi.models['product'].allAttributes;
}
我需要所有内容类型的解决方案,所以我制作了一个带有 /modelStructure/*
端点的插件,您可以在其中提供模型名称,然后传递给处理程序:
//more generic wrapper
async getModel(ctx) {
const { model } = ctx.params;
let data = strapi.models[model].allAttributes;
return data;
},
async getProductModel(ctx) {
ctx.params['model'] = "product"
return this.getModel(ctx)
},
//define all endpoints you need, like maybe a Page content type
async getPageModel(ctx) {
ctx.params['model'] = "page"
return this.getModel(ctx)
},
//finally I ended up writing a `allModels` handler
async getAllModels(ctx) {
Object.keys(strapi.models).forEach(key => {
//iterate through all models
//possibly filter some models
//iterate through all fields
Object.keys(strapi.models[key].allAttributes).forEach(fieldKey => {
//build the response - iterate through models and all their fields
}
}
//return your desired custom response
}
欢迎提出意见和问题