环回本身对我来说是新的,我看到版本4和版本3太不一样了。我的要求是,每次创建到REST端点的POST时,都需要在mongoDB文档中有一个自定义的自动递增id,类似于MySQL数据库中的运行id。
我查过了(
auto-increment using loopback.js and MongoDB
)以及(
https://gist.github.com/drmikecrowe/5a5568930bad567d4148aad75c94de5a
)有了版本3的设置,但是我没有找到合适的文档在版本4上复制相同的内容。
目前,我正在使用一个基本的应用程序,其中包含loopback 4提供的开箱即用REST实现。下面是我的模型的一个例子。
export class Test extends Entity {
@property({
type: 'string',
id: true,
})
_id?: string;
@property({
type: 'number',
generated: true,
required: false
})
id: number;
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'boolean',
required: true,
})
val: boolean;
constructor(data?: Partial<Test>) {
super(data);
}
}
我的mongodb文档应该如下所示:
{
"_id" : ObjectId("5c373c1168d18c18c4382e00"),
"id" : 1
"name" : "aaaa",
"val" : true
}
{
"_id" : ObjectId("5c3869a55548141c0c27f298"),
"id" : 2
"name" : "bbbbb",
"val" : false
}