Soft-Delete for entities
Soft-Delete for entities
Section titled “Soft-Delete for entities”Soft-delete allows you to mark records as “deleted” instead of physically removing them from the database. This is useful for auditing, data recovery, and maintaining relational integrity.
Configuring Soft-Delete
Section titled “Configuring Soft-Delete”To enable soft-delete, set softDelete: true in the @Entity decorator and specify which field should be used as the deletion mark with onDelete.
import { Entity, Id, Field } from '@uql/core';
@Entity({ softDelete: true })export class MeasureUnitCategory { @Id() id?: number;
@Field() name?: string;
/** * The 'onDelete' callback instructs UQL what value to set when deleting. * Common values are a boolean true or a timestamp. */ @Field({ onDelete: () => new Date() }) deletedAt?: Date;}
How it works
Section titled “How it works”When soft-delete is enabled, UQL automatically transforms delete operations into update operations and adds filters to find operations.
Deleting a record
Section titled “Deleting a record”await querier.deleteOneById(MeasureUnitCategory, 1);Resulting SQL:
UPDATE "MeasureUnitCategory" SET "deletedAt" = '2025-12-30T12:00:00Z' WHERE "id" = 1Querying records
Section titled “Querying records”By default, soft-deleted records are excluded from all queries.
await querier.findMany(MeasureUnitCategory, { $select: { id: true, name: true } });Resulting SQL:
SELECT "id", "name" FROM "MeasureUnitCategory" WHERE "deletedAt" IS NULLAdvanced: Including Soft-Deleted Records
Section titled “Advanced: Including Soft-Deleted Records”If you need to include soft-deleted records in a specific query (e.g., for an admin panel or trash view), you can bypass the filter in the query options.
// Note: UQL provides options to include deleted records at the querier level if needed.const allCategories = await querier.findMany(MeasureUnitCategory, { $where: { /* your filters */ }}, { includeDeleted: true });(Check the full API reference for available options on including deleted records)