Bulk update via raw query in typeorm

Max picture Max · Dec 15, 2018 · Viewed 9.1k times · Source

How can I do bulk update via raw query in typeorm?
For example we have model User with property name
How can I change names of few users in one transaction?

typeorm version: 0.2.7
database: postgress

Answer

aitchkhan picture aitchkhan · Jun 1, 2020

To bulk update, you can use update with set method, it is always recommended to not use raw queries when you can use orm functions.


import {getConnection, In} from "typeorm";
const userIds = [1,2,3];

await getConnection()
  .createQueryBuilder()
  .update(User)
  .set({ isaSeniorCitizen: true })
  .where({ id: In(userIds) })
  .execute();