我想创建动态模型、存储库和控制器
export async function dynamicModelsDemo(app: any, modelData: any): Promise<boolean> {
console.log("ModelData",modelData);
// assume that this def can be created dynamically (at runtime), e.g. from database info
const modelDef = new ModelDefinition({
name: 'contact',
properties: {
id: {
type: 'Number',
required: true,
length: null,
precision: 10,
scale: 0,
id: 1,
},
name: {
type: 'String',
required: false,
length: 512,
precision: null,
scale: null,
},
},
});
// tryin' to extend Entity with new fields
const DynamicModel = defineModelClass<typeof Entity, {id: number; title?: string}>(
Entity,
modelDef,
);
const BookRepository = defineCrudRepositoryClass(DynamicModel);
inject(`datasources.memory`)(BookRepository, undefined, 0);
const repoBinding = app.repository(BookRepository);
const basePath = '/contact';
const DynamicController0 = defineCrudRestController(DynamicModel, {basePath});
inject(repoBinding.key)(DynamicController0, undefined, 0);
app.controller(DynamicController0);
console.log(basePath);
return new Promise(function (resolve, reject) {
resolve(true);
});
}
我需要帮助,我应该如何创建Post方法,它将接收请求体,该体将传递到我上面提到的函数,
目前我正在调用这个端点的dynamicModelsDemo函数,
@get('/ping/build', {
modelData : {},
responses: {
'200': {
description: 'Test models assemble',
},
},
})
async build(): Promise<boolean> {
return dynamicModelsDemo(this.localApp,this.modelData);
}
我想把这个@get转换成@post,这样我就可以把我请求的主体传递给这个函数。
这个工作很好,我想这就是我要找的:
@post('ping/createobject')
async createObject(
@requestBody() model: any
):Promise<boolean> {
return dynamicModelsDemo(this.localApp,model);
}