Insert using Query Builder

You can create INSERT queries using QueryBuilder.Examples:

  1. import {getConnection} from "typeorm";
  2. await getConnection()
  3. .createQueryBuilder()
  4. .insert()
  5. .into(User)
  6. .values([
  7. { firstName: "Timber", lastName: "Saw" },
  8. { firstName: "Phantom", lastName: "Lancer" }
  9. ])
  10. .execute();

This is the most efficient way in terms of performance to insert rows into your database.You can also perform bulk insertions this way.

Raw SQL support

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

  1. import {getConnection} from "typeorm";
  2. await getConnection()
  3. .createQueryBuilder()
  4. .insert()
  5. .into(User)
  6. .values({
  7. firstName: "Timber",
  8. lastName: () => "CONCAT('S', 'A', 'W')"
  9. })
  10. .execute();

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