All jobs

Add Pagination to a Prisma Query

Yes. An agent paginates the Prisma query behind your list page with skip and take, adds a pager and a tie-break order, proves it on MySQL and opens a pull request.

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 starts from the query that loads everything. In a Next.js app like shadcn-ui/taxonomy, app/(dashboard)/dashboard/page.tsx calls db.post.findMany with where: { authorId: user.id }, a select of id, title, published and createdAt, and orderBy: { updatedAt: "desc" }, and no take, so a user's whole post list renders on one page. app/api/posts/route.ts has the same query in its GET handler, with no order at all. prisma/schema.prisma declares a mysql datasource, Post mapped to posts with authorId, createdAt and updatedAt, and prisma and @prisma/client at ^4.13.0; two migrations already sit under prisma/migrations. config/subscriptions.ts caps the free plan at 3 posts, but the PRO plan is unlimited, which is where the list grows. components/post-item.tsx renders one row and a PostItem.Skeleton, and components/pager.tsx is a prev and next pager for the docs (DocsPager), built on buttonVariants and Icons.chevronLeft, which the agent copies in shape rather than inventing a new control.

It moves the query into lib/posts.ts as getPostsPage(userId, page, pageSize), which runs db.$transaction with a count and a findMany using skip and take, so the total and the page come from one read. The order becomes [{ updatedAt: "desc" }, { id: "desc" }]: updatedAt defaults to now() and two posts saved in the same second would otherwise swap places between pages. The page reads searchParams.page, clamps it to a whole number of at least 1, and when it is past the last page redirects to the last one instead of rendering an empty list, so the existing EmptyPlaceholder still means a user has no posts at all. A new components/post-pager.tsx links to ?page= for the previous and next page in the DocsPager style and renders nothing when there is one page.

prisma/schema.prisma gains @@index([authorId, updatedAt]) on Post, because the per-user list now reads a bounded window in updatedAt order, and a prisma migrate dev --name post_author_updated_at_index migration lands beside the two existing ones. app/(dashboard)/dashboard/loading.tsx keeps its skeletons, now as many as one page holds. The GET /api/posts handler is left as it is and named in the pull request as the next place to paginate, since changing its response shape would change what any caller of that route receives.

Then it proves the query. The sandbox has no Docker, so the agent installs MySQL with apt-get, points DATABASE_URL at it and runs pnpm prisma migrate deploy, which applies the two existing migrations and the new one. A short tsx script inserts a user and more posts than one page holds, then calls getPostsPage for the first page, the last page and one past the end, printing the total, the ids on each page and that no id appears twice. pnpm lint (next lint) and pnpm tsc --noEmit run last; tsconfig.json has strict: false with strictNullChecks: true, so the clamp and the redirect are typed against a possibly absent page.

Start it with one API call

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

{
  "agent": "claude",
  "input": "The dashboard posts list loads every post of the user with one findMany and no take. Paginate it: move the query into lib/posts.ts as a function that returns the total and one page using skip and take, order by updatedAt desc with id desc as a tie-break, read the page number from searchParams, clamp it, and redirect past-the-end pages to the last page. Add a prev/next pager component in the style of components/pager.tsx that renders nothing for a single page. Add an index on (authorId, updatedAt) to Post with a migration. Leave GET /api/posts unchanged and say so. Prove it against a local MySQL: run prisma migrate deploy, seed more posts than one page, and print the total and the ids on the first page, the last page and one past the end. Run lint and tsc. Open a pull request quoting the 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 adds lib/posts.ts and components/post-pager.tsx, changes app/(dashboard)/dashboard/page.tsx to read searchParams and render the pager, adds the index to prisma/schema.prisma with its migration directory, and adjusts the skeleton count in loading.tsx. Its description quotes the script output: the total, the ids on the first and last page, the redirect for a page past the end, and the next lint and tsc results.

A reviewer checks three things: that the orderBy carries the id tie-break, that the redirect uses the clamped page so ?page=0 and ?page=abc both land on page 1, and that the pager is hidden with a single page so an account with two posts looks exactly as before. The transcript at GET /o/{org}/sessions/{id}/messages has the MySQL install, the migration run and the script in full, and the session's changes read lists each file with its diff.

Nothing touches your database. The migration is applied to your own with pnpm prisma migrate deploy after the merge, and the index is the only schema change, so it can be deployed without downtime.

Run it on every list page that loads a whole table

Each list that reads a table without take is the same job on another query: a sessions list, a comments list, an admin table. Send POST /o/{org}/sessions with this body and the page path and model named in the prompt, one run per list, with a request_id that is a UUID v5 of the page path so a retried send returns the run it already started rather than a second pull request.

A webhook URL on the API key delivers run.completed when each run finishes, with the session_url to read its pull request from, so a batch over several pages needs no polling. A key runs max_concurrent runs at once, default 4, and the rest start as slots free.

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

Why run it on Rush?

The change itself is a few lines of skip and take. What makes it safe is the tie-break order, the clamp, the redirect past the end and an index that matches the new query, and those are only convincing when the query has run against a real database with more rows than a page. The sandbox gets its own MySQL for the length of the run, the script output is in the transcript, and your .env and your database are never involved.

Several list pages, or several apps built from the same starter, are separate runs with separate proofs, each on its own branch. A reviewer reads one diff per page rather than one wide change, and the ids printed per page are the evidence that the pager does not skip or repeat a row.

Frequently asked questions

Offset pagination or cursor pagination?

Offset, because the page is a per-user list with page links, and skip on a few thousand rows per author is cheap once the (authorId, updatedAt) index exists. For an endless feed ordered by time across all users, ask for a cursor in the prompt and the agent uses Prisma's cursor with take on the id tie-break instead.

Will GET /api/posts change?

Not in this run. The route returns a bare array today, and paginating it means a new response shape for every caller. The pull request names it as the next step; a follow-up message to the same session, POST /o/{org}/sessions/{id}/messages, adds page and take query parameters there and returns { posts, total }.

Does it work with PlanetScale?

The schema change is one index, and the migration file is plain SQL from prisma migrate dev. If you deploy schema changes to a PlanetScale branch with prisma db push instead of migration files, the same @@index line applies that way; the page code does not care how the index arrived.

What about the free plan's three-post limit?

It is unchanged. app/api/posts/route.ts still counts a free user's posts before creating one and answers 402 past three. Pagination only matters for PRO users, whose lists have no cap, and the pager hides itself for anyone with a single page.

More Prisma jobs

The same job on other stacks

Keep reading