Max*_*Max 5 node.js bulkupdate typeorm
如何通过 typeorm 中的原始查询进行批量更新?
例如,我们有一个带有属性名称的模型User
如何在一次事务中更改少数用户的名称?
typeorm 版本:0.2.7
数据库:postgress
小智 12
您还可以通过以下方式使用存储库 api 更新多行:
await repository.update(
{
id: In([1,2,3,4,5]),
},
{ selected: true },
);
Run Code Online (Sandbox Code Playgroud)
ait*_*han 11
要批量更新,您可以使用 set 方法更新,当您可以使用 orm 函数时,始终建议不要使用原始查询。
import {getConnection, In} from "typeorm";
const userIds = [1,2,3];
await getConnection()
.createQueryBuilder()
.update(User)
.set({ isaSeniorCitizen: true })
.where({ id: In(userIds) })
.execute();
Run Code Online (Sandbox Code Playgroud)
不支持使用不同的值更新多个列。Typeorm 存储库中有一个关于此的开放问题:https ://github.com/typeorm/typeorm/issues/7326
如何使用原始 psql 执行此操作如 @Roman-Pekar 对Update multiple rows in same query using PostgreSQL的回答所示。
另一个“解决方案”是在 postgres 数据库中创建一个存储函数并调用它。在对另一个类似问题的评论之一中,我展示了一个有关如何执行此操作的示例:typeorm高效批量更新
| 归档时间: |
|
| 查看次数: |
10624 次 |
| 最近记录: |