Get a Quote Right Now

Edit Template

Querying Deleted rows

If the model you are querying has a column with the attribute @DeleteDateColumn set, the query builder will automatically query rows which are ‘soft deleted’.

Let’s say you have the following entity:

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    name: string

    @DeleteDateColumn()
    deletedAt?: Date
}

Using a standard find or query, you will not receive the rows which have a value in that row. However, if you do the following:

const users = await dataSource
    .getRepository(User)
    .createQueryBuilder()
    .select("user.id", "id")
    .withDeleted()
    .getMany()

You will get all the rows, including the ones which are Soft-deleted.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *