typeorm/docs/delete-query-builder.md
seungwonleee 4173e51dd8
docs: update delete-query-builder.md (#10519)
For previous examples typescript error occurs in createQueryBuilder that says “Argument of type 'string' is not assignable to parameter of type 'QueryRunner'.ts(2345)”.
If you check the type of createQueryBuilder, the string cannot come as the first argument.
The types of createQueryBuilder are as follows.
myDataSource.createQueryBuilder<ObjectLiteral>(entityClass: EntityTarget<ObjectLiteral>, alias: string, queryRunner?: QueryRunner | undefined):
2023-12-29 14:52:03 +05:00

1.1 KiB

Delete using Query Builder

Delete

You can create DELETE queries using QueryBuilder. Examples:

await myDataSource
    .createQueryBuilder()
    .delete()
    .from(User)
    .where("id = :id", { id: 1 })
    .execute()

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

Soft-Delete

Applying Soft Delete to QueryBuilder

await dataSource.getRepository(Entity).createQueryBuilder().softDelete()

Examples:

await myDataSource
  .createQueryBuilder('users')
  .softDelete()
  .where("id = :id", { id: 1 })
  .execute();

Restore-Soft-Delete

Alternatively, You can recover the soft deleted rows by using the restore() method:

await dataSource.getRepository(Entity).createQueryBuilder().restore()

Examples:

await myDataSource
  .createQueryBuilder('users')
  .restore()
  .where("id = :id", { id: 1 })
  .execute();