NestJS - How to properly update with TypeOrm
There are multiple ways to update record in typeorm:
- use
Query Builder
import {getConnection} from "typeorm";await getConnection() .createQueryBuilder() .update(User) .set({ firstName: "Timber", lastName: "Saw" }) .where("id = :id", { id: 1 }) .execute();
- use
save
Saves a given entity or array of entities. If the entity already exist in the database, it is updated. If the entity does not exist in the database, it is inserted. It saves all given entities in a single transaction (in the case of entity, manager is not transactional). Also supports partial updating since all undefined properties are skipped. Returns the saved entity/entities.
import { getMongoRepository } from 'typeorm';repo = getMongoRepository(User);await repository.save(user);
- use
update
as you mentioned
Specially, there are updateOne
and findOneAndUpdate
, updateMany
api in MongoRepository API.For more detail you can referenced here