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

查看:144
本文介绍了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
});

根据文档(保存部分)进行部分更新也受支持:

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天全站免登陆