# Add Cursor Pagination to a Prisma API

Yes. An agent converts your Prisma list endpoints from skip and take to cursor pagination with a stable order and a nextCursor, tests on Postgres, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-cursor-pagination/prisma/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the queries before it changes them, because cursor pagination only works on an order that cannot tie. In an API laid out like the `orm/express` example in prisma/prisma-examples, `src/index.ts` is one Express 5 file on `@prisma/client` 7 with the `@prisma/adapter-pg` driver adapter and a `PrismaClient` generated into `prisma/generated`. Its `GET /feed` route reads `searchString`, `skip`, `take` and `orderBy` from the query string and calls `prisma.post.findMany` with `take: Number(take) || undefined`, `skip: Number(skip) || undefined` and `orderBy: { updatedAt: orderBy }`. `GET /users` and `GET /user/:id/drafts` return every row with no limit at all. `prisma/schema.prisma` gives `Post` an autoincrement `id`, `createdAt`, an `updatedAt` marked `@updatedAt`, `published`, `viewCount` and an optional `authorId`; `User` has an autoincrement `id` and a unique `email`. `prisma/seed.ts` creates three users and four posts, and `prisma.config.ts` names the schema, the migrations directory and `tsx prisma/seed.ts` as the seed command.

Two things make the current `/feed` unsafe to page through. `updatedAt` is not unique, so two posts saved in the same instant have no fixed order between pages, and `skip` grows with every page, so the database reads and discards everything before it. The agent rewrites the route the way Prisma's pagination guide describes: `take` stays, a `cursor` query parameter becomes `cursor: { id: Number(cursor) }` with `skip: 1` so the cursor row itself is not returned twice, and `orderBy` becomes a list, `updatedAt` in the requested direction followed by `id` in the same direction, so the order is total. `id` is the cursor because Prisma's `cursor` takes a unique field, and the response gains a `nextCursor`, the `id` of the last row when a full page came back and `null` otherwise. `take` is clamped to a maximum and defaults to a page size, both named constants at the top of the file. The same shape goes on `/users` and `/user/:id/drafts`, which are plain `id` order.

Changing a response from a bare array to `{ data, nextCursor }` is a contract change, so the agent updates the endpoint table and the `curl` examples in `README.md`, which currently document `/feed?searchString=prisma&take=2&orderBy=desc` and `skip`, and keeps `skip` working for one release with a deprecation note in the response, since the README is what the example's users copy. It adds an index on `Post(updatedAt, id)` in `schema.prisma` and a migration for it with `npx prisma migrate dev --name post-updated-at-index`, because the new order is what every feed query sorts by.

To check it, the agent needs a Postgres, so it installs one in its sandbox with `sudo apt-get install -y postgresql`, starts it, writes `DATABASE_URL` into `.env` as the README says, runs `npx prisma migrate dev --name init` and `npx prisma db seed`, and starts the server with `npm run dev`. Then it walks the feed: `/feed?take=2` returns two posts and a `nextCursor`, `/feed?take=2&cursor=<that id>` returns the rest with `nextCursor: null`, and the same walk with `orderBy=asc` returns the posts in the opposite order with no row repeated and none missing. It adds two more posts through `POST /post` with the same `updatedAt` second to show the `id` tiebreak holds, pastes each response into the pull request, and runs `npx tsc --noEmit` against the example's `tsconfig.json`, which has `strict` on.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Replace skip-based pagination with cursor pagination in this Prisma and Express API. Read src/index.ts, prisma/schema.prisma, prisma/seed.ts, prisma.config.ts, package.json, tsconfig.json and README.md first. On GET /feed, GET /users and GET /user/:id/drafts, accept take and cursor query parameters, use Prisma's cursor with skip: 1, order by updatedAt and then id on the feed and by id elsewhere, clamp take to a maximum, and return { data, nextCursor }. Keep skip working for one release with a deprecation note. Add an index on Post updatedAt and id with a migration. Update the README's endpoint table and curl examples. Install Postgres in the sandbox, run prisma migrate dev and prisma db seed, start the server, walk every page of the feed in both directions and show no row repeats or goes missing, run tsc --noEmit, and open a pull request with the responses and 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

The pull request changes `src/index.ts`, `prisma/schema.prisma` and `README.md`, and adds one migration directory under `prisma/migrations` with the index. Its description shows the old and new `findMany` calls for `/feed` side by side, the page walk as a sequence of request and response pairs with the `nextCursor` of each, the `tsc --noEmit` result, and the migration SQL.

This example has no workflow of its own, so no check runs on the pull request; the repository's root `vitest` suite, which boots each example in `tests/orm.test.ts`, is what a maintainer runs. The run's record keeps the Postgres install, the migration and seed output, and every `curl`.

Before merging, a reviewer decides whether the maximum page size is right for the API's clients, whether to keep the deprecated `skip` path or remove it at once, and whether the `nextCursor` should be an opaque encoded string rather than the raw `id`, which the agent offers as a follow-up.

## Run it on every new list endpoint

A team adding list endpoints as its API grows sends this body from its own server when a route lands, with the repository in `resources` and a `request_id` that is a UUID v5 of the pull request's URL, so a retried call returns the run already started. Each run reads the route it is pointed at: a list ordered by a unique column keeps a single `orderBy`, one ordered by `createdAt` gains the `id` tiebreak, and one that already takes a `cursor` gets a comment that nothing is needed.

To catch the next unpaginated `findMany` before it ships, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` and the prompt "if this pull request adds a findMany that returns a list to a client without take and cursor, comment with the cursor version of the call" reviews each change. Automation runs are read-only, so the comment is all it posts, and only work by an owner, member or collaborator fires it.

## Why run it on Rush?

Cursor pagination is a handful of lines, and the mistakes hide: a non-unique `orderBy` that repeats a row on the next page, a missing `skip: 1` that returns the cursor twice, a `take` with no ceiling. The agent reads the schema to pick the tiebreak, runs the real queries against a real Postgres in its own sandbox, and walks every page in both directions before it opens anything.

The page walk, the migration output and the type check stay with the run and are pasted into the pull request, so the review is of responses that came back rather than of a diff that looks right. Several services with the same pattern get the same change at once, each in its own sandbox.

## Frequently asked questions

### Why keep id as the cursor when the feed is ordered by updatedAt?

Because Prisma's `cursor` must name a unique field, and `updatedAt` is not one. Ordering by `updatedAt` and then `id` gives a total order, and passing the last row's `id` as the cursor lets Prisma locate that row and continue from its position in that order.

### What does skip: 1 do?

It skips the cursor row itself. Prisma's `cursor` positions the query on the row with that `id` and includes it, so without `skip: 1` the first row of every page after the first would be the last row of the page before it.

### Does this change the response for existing clients?

Yes. The three list routes return `{ data, nextCursor }` instead of a bare array, which is why the README's endpoint table and `curl` examples are updated in the same pull request, and why `skip` keeps working with a deprecation note for one release so a client can move on its own schedule.

### Why add an index?

Because every feed page now sorts by `updatedAt, id` and filters on `published`, and the database should not sort the whole table to find one page. The index goes in `schema.prisma` so it ships as a migration, and the migration SQL is in the pull request for review.

## More Prisma jobs

- [Add Database Indexes to a Prisma Schema](https://getrush.ai/run/add-database-indexes/prisma/)
- [Add Full-Text Search to a Prisma App](https://getrush.ai/run/add-full-text-search/prisma/)
- [Add a Prisma Migration Drift Check to CI](https://getrush.ai/run/add-migration-drift-check/prisma/)

## The same job on other stacks

- [Add Cursor Pagination to a Quarkus Panache REST Endpoint](https://getrush.ai/run/add-cursor-pagination/quarkus/)
- [Add Cursor Pagination to a Rocket App](https://getrush.ai/run/add-cursor-pagination/rocket/)
- [Add Cursor Pagination to a Sequelize + Express API](https://getrush.ai/run/add-cursor-pagination/sequelize/)

## Keep reading

- [What an agent can do in Prisma itself, once you connect it](https://getrush.ai/integrations/prisma/)
- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
