Files
outline/server/models/base/ParanoidModel.ts
Tom MoorandClaude Opus 5 def3a42752 feat: Record who deleted a document (#13484)
* feat: Record who deleted a document

The trash inferred the deleting user from lastModifiedById, so a nested page
kept whoever last edited it and deleting a document rewrote its edit history.
Add a dedicated deletedById column and set it from the base model.

- Migration adds the column, backfills from lastModifiedById, and validates the
  foreign key without holding an exclusive lock. The backfill runs in batches
  against a temporary index covering only the rows still to write, so it stays
  linear on a large table, and every statement is idempotent because the
  migration cannot run in a transaction.
- ParanoidModel records the acting user on destroy and clears it on restore,
  for any model that has the column. Sequelize soft deletes with the same UPDATE
  that stamps deletedAt, so the value costs no extra query.
- Deleting a document no longer touches lastModifiedById on its descendants.
- Present the deleting user, batched in presentDocuments, and use it for the
  trash notice and the document meta line rather than the last editor.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Do not overwrite an eager loaded deleting user when batching

The batch lookup in presentDocuments skipped documents that already carried the
association, but the assignment loop ran for all of them, so a document that
arrived with deletedBy loaded was reset to null and fell back to a per-document
query. Assign only where the association is missing, and deduplicate the ids so
a page deleted by one person asks for one row rather than one per document.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 21:49:31 -04:00

57 lines
1.8 KiB
TypeScript

import { BeforeDestroy, BeforeRestore, DeletedAt } from "sequelize-typescript";
import IdModel from "./IdModel";
import type { HookContext } from "./Model";
/** The optional column recording the user that deleted a row. */
const DeletedByField = "deletedById";
class ParanoidModel<
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mirrors Model base; tightening to object resolves Attributes<M> to never inside Sequelize helpers.
TModelAttributes extends object = any,
TCreationAttributes extends object = TModelAttributes,
> extends IdModel<TModelAttributes, TCreationAttributes> {
@DeletedAt
deletedAt: Date | null;
/**
* Whether the model has been deleted.
*
* @returns True if the model has been deleted
*/
get isDeleted() {
return !!this.deletedAt;
}
/**
* Records the acting user against a model that has a `deletedById` column.
* Sequelize soft deletes with the same UPDATE that writes `deletedAt`, so the
* value set here is persisted without an additional query.
*
* @param model The model being deleted.
* @param context The hook context, which provides the acting user.
*/
@BeforeDestroy
static async setDeletedBy(model: ParanoidModel, context: HookContext) {
const userId = context.auth?.user?.id;
if (userId && DeletedByField in this.rawAttributes) {
model.setDataValue(DeletedByField, userId);
}
}
/**
* Clears the recorded deleting user when a model is restored, so that a row
* which is no longer deleted carries no attribution.
*
* @param model The model being restored.
*/
@BeforeRestore
static async unsetDeletedBy(model: ParanoidModel) {
if (DeletedByField in this.rawAttributes) {
model.setDataValue(DeletedByField, null);
}
}
}
export default ParanoidModel;