我有两个实体一个是过滤器Dataset,两者有一一的关系让我知道如何使用Repository on promise更新基于Dataset的过滤器实体代码编写在node.ts上我的API调用是.put("/数据集:id/过滤器")
`@Entity('dataset')
export class DatasetEntity {
@PrimaryGeneratedColumn('increment')
id!: number
@Column()
program!: string
@Column()
description!: string
@Column()
name!: string
}
@Entity('filter')
export class FilterEntity {
@PrimaryGeneratedColumn('increment')
id!: number
@Column({ type: 'json' })
attributes!: { [key: string]: any }
@OneToOne(() => DatasetEntity)
@JoinColumn()
dataset!: DatasetEntity
}`
你可以这样做。
const filterRepo = getRepository(FilterEntity);
const savedFilter = await filterRepo.findOne({
//Choose one according your situation
id: req.params.id //if id comming from params is filter id,
dataset: req.params.id //if id comming from params is filter id
});
if(!savedFilter.dataset)
throw "Data set not found";
//Now pass the updated value like
savedFilter.attributes = req.body.attributes;
await filterRepo.save(savedFilter);
在Filter repos
中可能需要更改的一件事@OneToOne(() => DatasetEntity)
@JoinColumn({name: "id"})
dataset!: DatasetEntity