SoftDelete

In the src/common/database/base-model.ts file, you need to import DeleteDateColumn and add the following field:

 ...
 @DeleteDateColumn()
 typublic deletedAt?: Date;
 ...

You must have something like this:

import {
  CreateDateColumn,
  DeleteDateColumn,
  Generated,
  PrimaryColumn,
  UpdateDateColumn,
} from 'typeorm';

export class BaseModel {
  @PrimaryColumn({ type: 'uuid' })
  @Generated('uuid')
  public id?: string;

  @CreateDateColumn()
  public createdAt?: Date;

  @UpdateDateColumn()
  public updatedAt?: Date;

  @DeleteDateColumn()
  public deletedAt?: Date;
}

Now, you need to use the softDelete method on each of your repositories.

If you want to restore a record, you can use the restore method like shown below.

If you need to include deleted records in a search, you must include the field withDeleted: true in the query you are performing.

For example:

Migration

Now in the migrations you create, you must add a deleted_at field of type Timestamp to record the date the record was deleted.

Example:

If you already have tables created in your database, you must do an alter table to add a new column deleted_at.

Last updated

Was this helpful?