All jobs

Add Soft Delete to TypeORM Entities

Yes. The agent adds a delete-date column, switches deletes to softDelete, adds restore, and fixes the joined relations that still return deleted rows.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent first checks that the installed version can do this without a library. In a NestJS app like lujakob/nestjs-realworld-example-app both lockfiles pin typeorm 0.2.24, and that release already ships @DeleteDateColumn, softDelete, softRemove, restore, recover and the withDeleted option. It then finds the deletes. ArticleService.delete calls articleRepository.delete({ slug }), deleteComment calls commentRepository.delete(...) after splicing the comment out of the loaded article, and UserService.delete removes a user by email. The issue decides which of those become soft; for articles and comments the agent adds @DeleteDateColumn() deletedAt: Date to ArticleEntity and Comment and replaces the two calls with softDelete.

The part that goes wrong is reading, and it depends on the version. With a delete-date column present, 0.2.24 appends a not-deleted condition to select queries, but its query builder applies that condition to the main alias only. findAll and findFeed build from createQueryBuilder('article'), so the listings and their getCount drop deleted articles with no further change, and so does every findOne({ slug }). Relations are another matter. comments on ArticleEntity is declared with eager: true, which loads comments through a join, and findComments returns article.comments as they arrive. A soft-deleted comment would still be in that list. The same holds for articles reached through UserEntity.favorites. TypeORM's changelog records the fix for joined relations under 0.2.31, so on this version the agent closes the gap in the service: comments are loaded through commentRepository with the article as a condition, where the filter does apply, and the eager flag comes off.

It then follows a deleted article through the other endpoints. update, addComment, favorite and unFavorite all begin with findOne({ slug }) and use the result without checking it, so once the row is filtered out they would fail on a missing object instead of answering 404. The agent calls each one against a missing slug before the change to record what happens today, and adds a not-found response only where the issue asks for one. favoriteCount is a stored number, so favorites of a deleted article need a decision the agent states instead of guessing: left in place, they come back intact on restore. It adds the restore path the issue describes, restore({ slug }) behind the same AuthMiddleware routes in article.module.ts, plus a lookup with withDeleted: true so a restore of something that was never deleted can be told apart from a wrong slug.

The schema change rides on how this project makes its schema. ormconfig.json.example has synchronize on, so the nullable deletedAt columns appear at the next start and existing rows read as live. The agent installs a MySQL-compatible server from system packages, creates ormconfig.json and src/config.ts from their examples, and writes specs against the real database: delete an article, see it leave the listing, the count and the feed; delete a comment and read the article's comments; restore both; delete twice. Before any of that it tries the current hard delete on an article that has comments and keeps the server's answer, because the comment table holds a foreign key to article and the outcome belongs in the description.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "The linked issue asks for soft delete on articles and comments in this NestJS and TypeORM application, with a way to restore them. Stay on the installed TypeORM version and add no library. Read package.json, both lockfiles, ormconfig.json.example, every entity, every service and controller, and the route list in each module first. Confirm which soft-delete features the installed version has by reading its type declarations in node_modules. Install a database server of the kind the config names from system packages, say which server and version, and create the config files from their committed examples. Before changing anything, call the delete endpoints, including on an article that has comments, and call the other article endpoints with a slug that does not exist; record each response. Add a delete-date column to the entities the issue names, switch their deletes to soft deletes and add restore endpoints behind the same authentication as delete. Then check every read path: for each query and each relation that can return these entities, write a test that soft-deletes a row and asserts it is no longer returned, and fix the paths where the installed version does not filter, in particular joined and eager relations. Do not change how users are deleted unless the issue says so. Say what you decided for stored counters and join rows that refer to a deleted article, and why. Add tests for delete, list, count, feed, comments, restore and deleting twice, run them against the real database, and open a pull request that closes the issue with the before and after responses and the test output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request with two new columns on two entities, softDelete and restore calls in src/article/article.service.ts, new routes in the controller and module, and specs. The description opens with a table of read paths: each query or relation that can return an article or a comment, whether TypeORM 0.2.24 filters it automatically, and what the agent changed where it does not. The row for the eager comments relation is the one to look at first.

Below that are the responses recorded before the change, including the hard delete of an article with comments and each endpoint's behaviour on a missing slug, next to the responses after. A short section states the decisions that were not in the issue: what happens to favorites and to favoriteCount, whether a deleted article's slug stays reserved, and that users are still deleted for real.

A reviewer checks that list of decisions against what the product wants, and reads the test that deletes a comment and then fetches the article, since that is the case an upgrade past 0.2.31 would make redundant and a downgrade would break. They also decide when deleted rows are purged, which this change deliberately leaves open.

Run it on every Linear issue labelled data-retention

Requests to keep deleted records arrive per entity, from support or compliance. Add an automation to the Project in the console with a linear trigger on linear.issue.labeled and data-retention in its labels. The saved prompt holds the method above without naming an entity; the issue is placed after it and says which records must survive a delete. The trigger needs a Linear connection for whoever the automation runs as, made under Settings → Connections.

A Linear trigger fires only when the person who added the label is in your Rush org, and never for a change made by an app integration. Two issues for two entities start two runs on separate branches, and because both edit the same service file it is worth merging the first pull request before reviewing the second.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

Soft delete fails quietly: the delete works, and a deleted row keeps appearing through some relation nobody tested. Finding those paths means deleting rows in a real database and calling every endpoint that could return them. The agent does that in its own sandbox, on the TypeORM version the lockfile pins, and keeps each request and response in the run's record. The gap in joined relations on 0.2.24 is found by a failing test, not by reading release notes.

It is also work that touches entities, services, routes and tests at once, which is awkward to do in the gaps of a day. As a run it arrives as one branch with the decisions written down, and the person who labelled the issue can read those decisions without reading the diff.

Frequently asked questions

What is the difference between softDelete and softRemove?

softDelete takes criteria and issues one UPDATE that sets the delete date, without loading anything. softRemove takes loaded entities and goes through the save pipeline, so it can cascade to relations marked for it. The agent uses softDelete for deletes by slug or id and says so in the description.

How do I query deleted rows on purpose?

Pass withDeleted: true in the find options, or call .withDeleted() on a query builder. The restore endpoint uses it to tell a deleted article from one that never existed. An admin listing of deleted articles is a few lines on top of that if the issue asks for it.

Will this work the same after upgrading TypeORM?

The decorator and the methods keep their names in later releases. What changes is filtering through relations, which later versions do themselves; the explicit comment query the agent writes stays correct either way. The specs pin the behaviour, so an upgrade that alters it fails a test.

What about unique columns?

A soft-deleted row still occupies its unique values. MySQL has no partial indexes, so a unique slug or email stays taken until the row is purged or the value is rewritten at delete time. This repository declares no unique index on the slug, and the agent notes the consequence in case one is added later.

Does it delete old rows for good after some time?

Not unless asked. A purge is a scheduled hard delete of rows whose delete date is older than a retention period, and it needs that period from you. The description names it as the natural next change.

More TypeORM jobs

The same job on other stacks

Keep reading