Soft Delete Patterns
How Gauzy implements soft deletes to preserve data integrity.
Overviewβ
Instead of permanently deleting records, Gauzy marks them as inactive/archived:
@MultiORMEntity("employee")
export class Employee extends TenantOrganizationBaseEntity {
@MultiORMColumn({ default: true })
isActive: boolean;
@MultiORMColumn({ default: false })
isArchived: boolean;
@DeleteDateColumn()
deletedAt?: Date;
}
Soft Delete Behaviorβ
Implementationβ
Service Layerβ
// Soft delete - sets deletedAt timestamp
async softDelete(id: string): Promise<void> {
await this.repository.softDelete(id);
}
// Restore soft-deleted entity
async restore(id: string): Promise<void> {
await this.repository.restore(id);
}
// Hard delete (permanent)
async hardDelete(id: string): Promise<void> {
await this.repository.delete(id);
}
Querying (Excluding Deleted)β
TypeORM's @DeleteDateColumn automatically filters soft-deleted records:
// This automatically excludes soft-deleted records
const employees = await this.repository.find();
// To include soft-deleted records
const all = await this.repository.find({ withDeleted: true });
// To find only soft-deleted records
const deleted = await this.repository
.createQueryBuilder("e")
.withDeleted()
.where("e.deletedAt IS NOT NULL")
.getMany();
Related Pagesβ
- Database Schema β schema overview
- Entity Inheritance β base entity
- Bulk Operations β bulk delete