All jobs

Add Soft Delete to a tRPC and Prisma App

Yes. An agent adds deletedAt to your Prisma model, hides deleted rows from every tRPC procedure, adds remove and restore mutations with tests, and opens a PR.

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 reads the data path end to end before it touches the schema. In an app laid out like trpc/examples-next-prisma-starter, prisma/schema.prisma has one model, Post, with a UUID id, title, text, createdAt and updatedAt on a Postgres datasource, and four migration folders, two of which add and then revert a unique index on createdAt. src/server/routers/post.ts exports a defaultPostSelect so no query leaks columns by accident, and three procedures: list, with cursor pagination that takes limit + 1 rows ordered by createdAt descending and pops the extra row as nextCursor, over a where: {} that is empty today; byId, a findUnique that throws TRPCError with code NOT_FOUND; and add, validated with zod to a title of 1 to 32 characters. Every procedure is a publicProcedure from src/server/trpc.ts, and createContextInner in context.ts returns an empty object. src/server/routers/post.test.ts is a Vitest integration test that calls the router through createCaller against the real database named by DATABASE_URL in .env, and src/pages/index.tsx renders post.list with useInfiniteQuery at a limit of five. CI in .github/workflows/main.yml runs a Postgres service, pnpm prebuild for prisma generate and migrate deploy, then test-unit, test-e2e with Playwright and tsc.

The schema change is one nullable column: deletedAt DateTime? on Post, written with pnpm migrate-dev so Prisma generates the migration folder and its ALTER TABLE "Post" ADD COLUMN "deletedAt" SQL rather than the agent writing SQL by hand. defaultPostSelect does not gain the new column, so the shape every existing client receives is unchanged. In the router, the empty where in list becomes { deletedAt: null }; byId switches from findUnique to findFirst with the same deletedAt: null filter, because Prisma's unique lookup cannot filter on a non-unique column, and keeps the same NOT_FOUND error so a deleted post looks to the client like one that never existed. Two mutations join the router: remove, which sets deletedAt to the current time and returns NOT_FOUND for a post already gone, and restore, which clears it. Both validate { id: z.string().uuid() } the way add already validates its optional id.

Tests come before the UI. post.test.ts gains cases that add a post, remove it, assert that byId now rejects with NOT_FOUND, assert that list no longer returns it while a second post still appears, and restore it to show it comes back; a last case removes twice and expects the second call to fail. src/pages/post/[id].tsx gets a delete button wired to post.remove with an onSuccess that invalidates post.list through utils, the same pattern index.tsx uses after add, and then navigates home. prisma/seed.ts, which upserts one post by a fixed id, needs no change. The Playwright smoke test in playwright/smoke.test.ts still adds a post through the form and finds it on reload.

To run all of this the agent needs a database, and its sandbox has no Docker, so instead of the postgres service container CI uses it installs Postgres with apt-get, starts it, creates the database .env points at and runs pnpm install, which triggers prisma generate through the postinstall script, then pnpm migrate-dev, pnpm test-unit, pnpm typecheck, pnpm lint and pnpm build. The migration SQL, the Vitest output and the type check all go into the pull request description.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Add soft delete for posts to this tRPC and Prisma app. Read prisma/schema.prisma, the prisma/migrations folders, src/server/routers/post.ts, src/server/routers/post.test.ts, src/server/trpc.ts, src/server/context.ts, src/pages/index.tsx, src/pages/post/[id].tsx, prisma/seed.ts, vitest.config.mts and .github/workflows/main.yml first. Add a nullable deletedAt DateTime to Post and create the migration with prisma migrate dev; do not write the SQL by hand. Keep defaultPostSelect as it is. Filter deletedAt null in list and byId, switching byId to findFirst, and keep the NOT_FOUND error. Add remove and restore mutations that validate a UUID id, with remove returning NOT_FOUND for a post already deleted. Extend post.test.ts to cover remove, byId after removal, list exclusion, restore and a double remove. Add a delete button to the post page that calls remove, invalidates post.list and returns home. Install Postgres in your sandbox with apt, not Docker, run pnpm install, pnpm migrate-dev, pnpm test-unit, pnpm typecheck, pnpm lint and pnpm build, and open a pull request with the migration SQL and the test output in its description.",
  "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

The pull request changes prisma/schema.prisma, adds one migration folder with its migration.sql, changes src/server/routers/post.ts and post.test.ts, and adds the delete button to src/pages/post/[id].tsx. Its description pastes the generated SQL, the Vitest run with each new case named, and the tsc result, and lists the three procedures whose output shape is unchanged because defaultPostSelect is unchanged.

The run's record holds every command, including the Postgres install and the migrate dev that produced the SQL, so a reviewer can see the migration was generated and not typed.

Before merging, a reviewer decides whether a hard-delete path is still wanted for operators, whether deletedAt should get an index once the table is large, and what list should do when a client's cursor points at a post deleted between two pages, since the filter now excludes the cursor row itself; the description names this case and the agent leaves the choice to the team.

Run it on every repository

A team with several tRPC services sends this body once per repository from its own server, each a POST /o/{org}/sessions with that repository in resources and a request_id that is a UUID v5 of the repository's full name, so a retried call returns the run already started. Each run reads its own schema: a repository whose procedures are protectedProcedure with a user in context gets remove restricted to the post's owner, and one on SQLite gets the migration Prisma generates for that provider.

Once the column exists, an automation on the Project with a github trigger on pull_request.opened and the prompt "if this pull request adds a Prisma query on Post without a deletedAt filter, comment with the file and line" starts one run per pull request. The trigger adds the pull request after the prompt, and only work by an owner, member or collaborator fires it.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Soft delete fails quietly: one procedure keeps returning deleted rows because its where was never updated, or a unique lookup keeps findUnique and the filter is silently impossible. The agent reads every procedure that touches the model, changes each one, and proves it with tests that run against a real Postgres rather than a mock.

The migration is generated by Prisma in the run's sandbox and its SQL is in the pull request, with the Vitest and type-check output beside it. A team with many services runs the same request across all of them at once, each in its own sandbox.

Frequently asked questions

Why not a Prisma client extension that filters everywhere?

An extension that rewrites every findMany is harder to see in review and still needs per-model exceptions the moment an admin screen wants deleted rows. With one model and three procedures, an explicit deletedAt: null in each query is shorter, visible in the diff, and covered by the tests. The description says how to move to an extension if the model count grows.

Does the API response change for existing clients?

No. defaultPostSelect does not include deletedAt, so list, byId and add return the same fields as before. The only visible difference is that a removed post disappears from list and byId answers NOT_FOUND for it.

Why switch byId from findUnique to findFirst?

Prisma's findUnique accepts only unique fields in its where, and deletedAt is not one. findFirst with { id, deletedAt: null } does the same primary-key lookup with the filter applied, and the test for a removed post proves it returns nothing.

What about the Playwright test and CI?

The smoke test adds a post and finds it after reload, which still passes. CI's pnpm prebuild runs migrate deploy, so the new migration applies on the service container before the tests. The agent runs the unit tests and the build in its sandbox; the end-to-end job runs on GitHub as before.

More tRPC jobs

The same job on other stacks

Keep reading