Skip to content

Soft delete columns

ArteMerlow edited this page May 28, 2025 · 1 revision

Soft delete columns

ModularORM has support for automatic soft deletes - when rows are not deleted, but only marked as deleted by changing the deleted_at column from NULL to the current date and time (the column name can be anything). You can implement this quite easily

@Table()
@NamedTable("users")
class Users extends Module {
    
    @AutoIncrementId()
    public id!: number;
    
    @Column({ type: ColumnType.VARCHAR(32) })
    public nickname!: string;
    
    @SoftDeleteColumn()
    public deletedAt!: QueryIs | Date;
    
}

Now let's imagine that you have multiple users in your table.

const usersRepository = new Repository(Users)
await usersRepository.insert({ nickname: 'nick1' })
await usersRepository.insert({ nickname: 'nick2' })
await usersRepository.insert({ nickname: 'nick3' })

And you want to delete a user, while preserving his data. For this you can use SoftDelete.

await usersRepository.softDelete({ nickname: 'nick2' })

The database still stores information about this user, but using softFind you can kind of hide it

const users = await usersRepository.softFind();
users.length // to be 2

This is essentially equivalent to the following query:

const users = await usersRepository.find({ deletedAd: QueryIs.NULL });

Clone this wiki locally