我有一个具有典型模型,存储库,控制器结构的Loopback 4 REST API。创建一个实体(通过POST)时,我想向其id
字段分配一个UUID,而忽略传入的任何内容。
如果我在控制器中执行此操作,它将起作用。但是,如果我尝试通过模型构造函数执行此操作,即使从模型返回了生成的UUID,传入的值也会记录在数据库中。
import {Entity, model, property} from '@loopback/repository';
import { v4 as uuid } from 'uuid';
@model({name: 'whatever'})
export class Whatever extends Entity {
@property({
type: 'string',
length: 40,
id: true,
required: false,
})
id: string;
// other stuff
constructor(data?: Partial<Children>) {
if (data != null) {
data.id = uuid();
}
super(data);
}
}
在数据库操作之前,如何转换模型中的数据?