Skip to content

Documentation · IGNIS

SoftDeletableRepository ​

Reference for SoftDeletableRepository - delete methods set a deletedAt timestamp instead of removing the row, with matching restore methods. For the common tasks, start with the Repositories overview.

Files:

Setup ​

1. Define the model with a deletedAt column ​

Use the PostgresTzEnricher.generateColumnDefs enricher with the deleted option enabled:

typescript
import { pgTable, text } from 'drizzle-orm/pg-core';
import {
  BaseEntity,
  model,
  PostgresIdEnricher,
  PostgresTzEnricher,
} from '@venizia/ignis';

@model({
  settings: {
    properties: { hidden: ['deletedAt'] },
    query: { filter: { where: { deletedAt: null } } },
  },
})
export class Category extends BaseEntity<typeof Category.schema> {
  static override schema = pgTable('Category', {
    ...PostgresIdEnricher.generateColumnDefs({ id: { dataType: 'string' } }),
    ...PostgresTzEnricher.generateColumnDefs({
      deleted: { enable: true, columnName: 'deleted_at', withTimezone: true },
    }),
    name: text('name').notNull(),
  });
}

IMPORTANT

  • The model must have a deletedAt column. SoftDeletableRepository requires TSoftDeletableTableSchema, which enforces { deletedAt: AnyColumn<{ data: Date | string | null }> }. The bound is Drizzle's dialect-free AnyColumn, the exact twin of AnyPgColumn, so the same column satisfies it on SQLite too.
  • Set query: { filter: { where: { deletedAt: null } } } in @model settings so soft-deleted records are excluded by default.
  • Optionally add deletedAt to properties.hidden to hide it from API responses.
  • Use PostgresTzEnricher.generateColumnDefs with deleted: { enable: true, ... } to add the column, or define it manually with timestamp('deleted_at', { mode: 'date', withTimezone: true }).

2. Extend SoftDeletableRepository ​

typescript
import { repository, SoftDeletableRepository } from '@venizia/ignis';
import { Category } from '@/models/category.model';
import { PostgresDataSource } from '@/datasources/postgres.datasource';

@repository({ model: Category, dataSource: PostgresDataSource })
export class CategoryRepository extends SoftDeletableRepository<typeof Category.schema> {}

Delete Operations ​

  • Delete sets a timestamp, not a DELETE. All delete methods set deletedAt = new Date() instead of removing the row, in one UPDATE. They do not go through updateById or updateBy, which refuse deletedAt.
  • Extra columns in the same statement. deleteById, deleteBy take an optional data, written with deletedAt in that one UPDATE. See Writing other columns with a soft delete.
  • Bypass with shouldHardDelete. Pass options.shouldHardDelete: true to skip soft delete and delegate to the parent's real DELETE.

deleteById ​

typescript
// Soft delete - sets deletedAt timestamp
const result = await repository.deleteById({ id: '123' });
// { count: 1, data: { id: '123', name: 'Electronics', deletedAt: '2026-03-06T...' } }

// Without returning data
const result = await repository.deleteById({
  id: '123',
  options: { shouldReturn: false },
});

// Soft delete and set another column in the same statement
const result = await repository.deleteById({ id: '123', data: { status: 'ARCHIVED' } });

// Hard delete - physically removes the row
const result = await repository.deleteById({
  id: '123',
  options: { shouldHardDelete: true },
});

deleteBy ​

where only needs options.force: true when where is empty - a non-empty condition never needs force.

typescript
// Soft delete matching records - non-empty where, no force needed
const result = await repository.deleteBy({
  where: { status: 'archived' },
});

// Soft delete every row - empty where requires force
const result = await repository.deleteBy({
  where: {},
  options: { force: true },
});

// Hard delete matching records
const result = await repository.deleteBy({
  where: { status: 'archived' },
  options: { shouldHardDelete: true },
});

deleteBy ​

Same behavior as deleteBy, but where is a required parameter instead of optional.

typescript
// Soft delete by where condition
const result = await repository.deleteBy({
  where: { name: 'Obsolete' },
});

// Hard delete by where condition
const result = await repository.deleteBy({
  where: { name: 'Obsolete' },
  options: { shouldHardDelete: true },
});

Restore Operations ​

  • Restore clears the timestamp. Restore methods set deletedAt = null.
  • Default filter is bypassed automatically. Restore methods internally pass shouldSkipDefaultFilter: true, so they can find the soft-deleted records the default filter would otherwise hide. You don't need to set this yourself.

restoreById ​

typescript
const result = await repository.restoreById({ id: '123' });
// { count: 1, data: { id: '123', name: 'Electronics', deletedAt: null } }

// Without returning data
const result = await repository.restoreById({
  id: '123',
  options: { shouldReturn: false },
});

restoreBy ​

typescript
// Restore all soft-deleted records (empty where requires force)
const result = await repository.restoreBy({
  where: {},
  options: { force: true },
});

// Restore matching records
const result = await repository.restoreBy({
  where: { name: 'Electronics' },
});

restoreBy ​

typescript
// Calls restoreBy internally
const result = await repository.restoreBy({
  where: { status: 'archived' },
});

Read Operations ​

findById with isStrict ​

SoftDeletableRepository overrides findById to support an isStrict option that throws a 404 Not Found error when the record doesn't exist:

typescript
// Returns null if not found (default)
const category = await repository.findById({ id: '123' });

// Throws 404 if not found
const category = await repository.findById({
  id: '123',
  options: { isStrict: true },
});
// Throws: [CategoryRepository][findById] Entity with id 123 not found (HTTP 404)
  • isStrict is the only thing the override adds. Every other findById option - retry, transaction, log, shouldSkipDefaultFilter, lock - behaves exactly as on DefaultCRUDRepository. So does the filter argument.
  • Everything else is inherited unchanged. find, findOne, count, existsWith behave exactly as on DefaultCRUDRepository.
  • The default filter excludes soft-deleted rows. IGNIS applies { deletedAt: null } automatically - pass options: { shouldSkipDefaultFilter: true } to include them.

You can combine isStrict with read retry. The retry loop runs first, so a strict read waits out replica lag before it throws:

typescript
const category = await repository.findById({
  id: '123',
  options: { retry: { maxAttempts: 4 }, isStrict: true },
});

Options Reference ​

Delete Options ​

OptionTypeDefaultDescription
shouldHardDeletebooleanfalseBypass soft delete and physically remove the row. Refused when the call also passes data
shouldReturnbooleantrueReturn the updated/deleted record
forcebooleanfalseAllow an empty where condition (deleteBy)
transactionITransaction-Transaction context
log{ use: boolean; level?: TLogLevel }-Enable operation logging
shouldSkipDefaultFilterbooleanfalseBypass the default filter

Restore Options ​

OptionTypeDefaultDescription
shouldReturnbooleantrueReturn the restored record
forcebooleanfalseAllow an empty where condition (restoreBy only - restoreById has no force option)
transactionITransaction-Transaction context

How It Works ​

OperationSQL Behavior
deleteByIdUPDATE SET deletedAt = NOW() [, ...data] WHERE id = ?
deleteByUPDATE SET deletedAt = NOW() [, ...data] WHERE ...
restoreByIdUPDATE SET deletedAt = NULL WHERE id = ? (with shouldSkipDefaultFilter: true)
restoreByUPDATE SET deletedAt = NULL WHERE ... (with shouldSkipDefaultFilter: true)
find / findOne / countDefault filter automatically adds WHERE deletedAt IS NULL
deleteById({ shouldHardDelete: true })DELETE FROM ... WHERE id = ? (delegates to the parent repository)

TIP

shouldHardDelete bypasses soft delete entirely and calls super, which is DefaultRelationalRepository's delete implementation - a real SQL DELETE.

Writing deletedAt ​

deletedAt changes only through deleteById, deleteBy and the restore methods. updateById, updateBy, updateEach and upsert refuse a data that carries it, with any value including null: the call fails with a 400 and writes nothing.

deletedAt is in the default properties.stamped list of a model, so the default POST and PATCH bodies of CrudControllerFactory.build leave it out. A route you give your own request.body keeps whatever that body declares.

NOTE

The default bodies follow the model, not the repository. The 400 is raised by SoftDeletableRepository and SoftDeletableSqliteRepository; another repository over the same table still accepts deletedAt in an update.

Writing other columns with a soft delete ​

A call that must change another column when it deletes passes data. It is written in the same UPDATE as deletedAt, so the two cannot be torn apart, for example to move a row out of a partial unique index in one statement:

typescript
await repository.deleteById({ id: '123', data: { status: 'ARCHIVED' } });
await repository.deleteBy({ where: { status: 'DRAFT' }, data: { status: 'ARCHIVED' } });

A soft delete is a delete to the row scope, so the row selection and the values in data are checked against the DELETE scope, not the UPDATE one. A value outside that scope is refused with a 403, and a row outside it is not touched.

  • data may not carry deletedAt or id. The call fails with a 400.
  • data and shouldHardDelete do not mix. A hard delete writes no columns, so the call fails with a 400.
  • Restore takes no data.

With Transactions ​

typescript
const tx = await this.dataSource.beginTransaction();
try {
  await this.categoryRepository.deleteById({ id: '123', options: { transaction: tx } });
  await this.auditRepository.create({
    data: { action: 'soft_delete', entityId: '123' },
    options: { transaction: tx },
  });
  await tx.commit();
} catch (error) {
  await tx.rollback();
  throw error;
}

Type Constraint ​

SoftDeletableRepository enforces that the schema includes a deletedAt column at the type level:

typescript
export type TDeletedAtColumn = AnyColumn<{ data: Date | string | null }>;

export type TSoftDeletableTableSchema = TTableSchemaWithId & {
  deletedAt: TDeletedAtColumn;
};

export class SoftDeletableRelationalRepository<
  EntitySchema extends TSoftDeletableTableSchema = TSoftDeletableTableSchema,
  // ...
> extends DefaultRelationalRepository<EntitySchema, ...> { }

If your schema does not have a deletedAt column, you get a TypeScript compilation error when extending SoftDeletableRepository.

Class Hierarchy ​

The behavior lives on the engine-neutral SoftDeletableRelationalRepository, the last rung of the relational chain:

AbstractRepository (engine-neutral, @venizia/ignis-kernel)
  -> RelationalBaseRepository
    -> ReadableRelationalRepository
      -> PersistableRelationalRepository
        -> DefaultRelationalRepository
          -> SoftDeletableRelationalRepository

SoftDeletableRepository is the Postgres binding of that last rung - a subclass, not an alias:

SoftDeletableRelationalRepository
  -> SoftDeletableRepository (Postgres)        <-- you are here
  -> SoftDeletableSqliteRepository (SQLite)

It rebinds ExtraOptions to IDatabaseExtraOptions and TDataSource to IPostgresDataSource, and adds nothing else. It does not extend DefaultCRUDRepository, even though the neutral class it extends does extend DefaultRelationalRepository.

Quick Reference ​

Want to...Code
Soft delete by IDrepository.deleteById({ id })
Hard delete by IDrepository.deleteById({ id, options: { shouldHardDelete: true } })
Soft delete by conditionrepository.deleteBy({ where })
Soft delete and set a columnrepository.deleteById({ id, data: { status } })
Restore by IDrepository.restoreById({ id })
Restore by conditionrepository.restoreBy({ where })
Find including deletedrepository.find({ filter, options: { shouldSkipDefaultFilter: true } })
Strict findById (404)repository.findById({ id, options: { isStrict: true } })

See also ​