Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 471
Max Avatar asked Dec 15 '18 09:12

Max


People also ask

How do you update multiple columns in TypeORM?

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.

What is TypeORM in node JS?

TypeORM is an Object Relational Mapper library running in node. js and written in TypeScript. TypeScript is an improvement to JavaScript with optional typing. TypeScript is a compiled language. It is not interpreted at run-time.


1 Answers

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

like image 173
aitchkhan Avatar answered Oct 04 '22 12:10

aitchkhan