san*_*oco 7 rest orm api-design typeorm
据我所知,最佳做法是在更新项目后返回它。TypeORM的updateById
returns void
,但不是更新的项目。
我的问题:是否可以在一行中更新并返回修改后的项目?
到目前为止我尝试过的是:
await this.taskRepository.updateById(id, { state, dueDate });
return this.taskRepository.findOne({ id });
Run Code Online (Sandbox Code Playgroud)
我在寻找什么:
return this.taskRepository.updateById(id, { state, dueDate }); // returns updated task
Run Code Online (Sandbox Code Playgroud)
san*_*oco 47
我刚刚发现我可以用这个.save
方法做到这一点:
return this.taskRepository.save({
id: task.id,
state,
dueDate
});
Run Code Online (Sandbox Code Playgroud)
根据文档(部分save
),也支持部分更新:
还支持部分更新,因为所有未定义的属性都被跳过。
小智 19
为了扩展 Sandrooco 的答案,这就是我所做的:
const property = await this.propertyRepository.findOne({
where: { id }
});
return this.propertyRepository.save({
...property, // existing fields
...updatePropertyDto // updated fields
});
Run Code Online (Sandbox Code Playgroud)
Joe*_*oel 12
虽然我想await Table.update({}, {})
退货,Table
但它没有。我发现使用 更容易,QueryBuilder
因为它总体上给了我更多的控制,但是如果你不喜欢QueryBuilder
或不需要它,你可以做这样的事情:
const post = await Post.update({id}, {...input}).then(response => response.raw[0]);
return post; // returns post of type Post
Run Code Online (Sandbox Code Playgroud)
但是,如果您确实想使用,QueryBuilder
我建议采用如下方法。上面的其他人已经提到了Repository
and Table.save()
which的用法并没有真正在type
任何地方返回原始内容,所以这种方法对我来说是不合适的。
的一个例子QueryBuilder
和Table.update({}, {})
:
@Mutation(() => PostResponse, { nullable: true })
@UseMiddleware(isAuthorized)
async updatePost(
@Arg("id", () => Int) id: number,
@Arg("input") input: PostInput,
@Ctx() { req }: Context
): Promise<PostResponse | null> {
const { userId } = req.session;
const errors = validatePost(userId, ...input, await Post.findOne(id));
if (errors) {
return { errors };
}
// THIS
const post = await Post.update({id}, {...input}).then(response => response.raw[0]);
// OR THIS (depending on what approach you want to use)
const post = await getConnection()
.createQueryBuilder()
.update(Post)
.set({ ...input })
.where('id = :id and "creatorId" = :creatorId', {
id,
creatorId: userId,
})
.returning("*")
.execute()
.then((response) => {
return response.raw[0];
});
return { post };
}
Run Code Online (Sandbox Code Playgroud)
response.raw[0]
以获取类型。注意:我在这里使用 TypeORM 和 Type-GraphQL。
.returning("*")
不适用于 MySQL,请参阅下面的评论。
归档时间: |
|
查看次数: |
5011 次 |
最近记录: |