typeorm/docs/update-query-builder.md
Kim_ByungChul bbbf07b021
delete single quote from "'age' +1" (#5500)
single quote makes error ER_TRUNCATED_WRONG_VALUE: Truncated incorrect DOUBLE value
2020-02-13 14:38:06 +05:00

897 B

Update using Query Builder

You can create UPDATE queries using QueryBuilder. Examples:

import {getConnection} from "typeorm";

await getConnection()
    .createQueryBuilder()
    .update(User)
    .set({ firstName: "Timber", lastName: "Saw" })
    .where("id = :id", { id: 1 })
    .execute();

This is the most efficient way in terms of performance to update entities in your database.

Raw SQL support

In some cases when you need to execute SQL queries you need to use function style value:

import {getConnection} from "typeorm";

await getConnection()
    .createQueryBuilder()
    .update(User)
    .set({ 
        firstName: "Timber", 
        lastName: "Saw",
        age: () => "age + 1"
    })
    .where("id = :id", { id: 1 })
    .execute();

This syntax doesn't escape your values, you need to handle escape on your own.