TypeORM:更新项目并返回 [英] TypeORM: update item and return it

查看:41
本文介绍了TypeORM:更新项目并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,最佳做法是在项目更新后退回.TypeORM 的 updateById 返回 void,但不是更新的项目.

As far as I know, it's a best practice to return an item after it has been updated. TypeORM's updateById returns void, not the updated item though.

我的问题:是否可以在一行中更新和返回修改后的项目?

My question: Is it possible to update and return the modified item in a single line?

到目前为止我尝试过的:

What I tried so far:

await this.taskRepository.updateById(id, { state, dueDate });
return this.taskRepository.findOne({ id });

我在找什么:

return this.taskRepository.updateById(id, { state, dueDate }); // returns updated task

推荐答案

我刚刚发现我可以使用 .save 方法来做到这一点:

I just found out that I can do this with the .save method:

return this.taskRepository.save({
    id: task.id,
    state,
    dueDate
});

根据文档(部分save),部分更新也支持:

According to the docs (section save), partial updates are supported as well:

还支持部分更新,因为所有未定义的属性都会被跳过.

Also supports partial updating since all undefined properties are skipped.

这篇关于TypeORM:更新项目并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆