node.jstypeormbulkupdate

Bulk update via raw query in TypeORM


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


Solution

  • 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();