mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
The getRepository method is missing from the example. Delete the "users" written in the createQueryBuilder method because it is not used in the where method.
1.2 KiB
1.2 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
.getRepository(User)
.createQueryBuilder()
.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
.getRepository(User)
.createQueryBuilder()
.restore()
.where("id = :id", { id: 1 })
.execute();