TypeORM:更新项目并返回

编程入门 行业动态 更新时间:2024-10-11 19:25:29
本文介绍了TypeORM:更新项目并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

据我所知,最佳做法是在项目更新后退回.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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 03:31:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/928259.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   TypeORM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!