tidbx-nextjs

Build and deploy Next.js (App Router) apps that connect to TiDB. Covers Route Handlers (app/api/*/route.ts), Node vs Edge runtime selection for database access, environment variable handling, and production-safe DB patterns on Vercel/serverless. Prefer Prisma/Kysely integration, with optional mysql2 for minimal examples. Includes guides and TypeScript templates.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "tidbx-nextjs" with this command: npx skills add pingcap/agent-rules/pingcap-agent-rules-tidbx-nextjs

TiDB Next.js Integration

Set up a Next.js app (App Router) that connects to TiDB, with clear guidance on where database code can run (Node runtime) and how to avoid common serverless pitfalls.

What is different about Next.js (vs plain Node scripts)

  • Full-stack framework: UI and server code live in one project.
  • Server/client split: database code must stay server-side (route handlers, server components, server actions).
  • Multiple runtimes: some routes can run on Edge; TCP database clients must run on Node runtime.
  • File-based routing: API endpoints are files like app/api/*/route.ts.

When to use this skill

  • Build a new Next.js app that reads/writes TiDB
  • Add TiDB to an existing Next.js App Router project
  • Deploy to Vercel (or other serverless platforms) and avoid connection/caching/runtime issues
  • Decide between Prisma/Kysely vs direct mysql2

Key decisions (recommended defaults)

  1. Prefer an ORM/query builder for application code:
    • Prisma ORM: tidbx-prisma
    • Kysely query builder: tidbx-kysely
  2. Use a direct driver (mysql2) only for minimal demos or when you need raw SQL without an ORM.
  3. Run TCP database clients in the Node.js runtime (not Edge).

TiDB Cloud pool settings (mysql2)

  • If you use a connection pool against TiDB Cloud, set the pool idle timeout to <= 300s (e.g. idleTimeout: 300_000).
  • Keep the pool small in serverless environments (often connectionLimit: 1 is enough per function/container).

Available guides

  • guides/quickstart.md -- create a Next.js app and add TiDB (Prisma-first; mysql2 alternative)
  • guides/troubleshooting.md -- runtime/env/caching/connection issues

Quick examples

  • "Add a /api/health route that queries TiDB" -> route handler + pooled connection
  • "Use Prisma in Next.js and deploy to Vercel" -> prisma singleton + route handlers
  • "My route handler fails on Vercel Edge" -> force Node runtime or switch to serverless HTTP driver

Templates

  • templates/route-handler-mysql2.ts -- App Router Route Handler that queries TiDB with mysql2 (Node runtime)
  • templates/tidb-mysql2-pool.ts -- mysql2 pool helper with TLS and serverless-friendly defaults
  • templates/prisma-client.ts -- PrismaClient singleton for Next.js (dev hot reload safe)
  • templates/route-handler-prisma.ts -- Route Handler using Prisma ($queryRaw + CRUD)

Related skills

  • tidbx-prisma -- Prisma ORM (models + migrations + typed client)
  • tidbx-kysely -- Kysely (typed query builder)
  • tidbx-serverless-driver -- TiDB Cloud serverless HTTP driver (for edge-like runtimes)
  • tidbx -- provision/manage TiDB Cloud clusters

Workflow

I will:

  1. Confirm your Next.js routing mode (App Router) and runtime target (Node serverless vs Edge)
  2. Confirm your TiDB deployment (TiDB Cloud vs self-managed) and endpoint type (public vs private)
  3. Recommend Prisma/Kysely by default, unless you explicitly want mysql2
  4. Generate the minimal files (route handler + db client helper) and env vars to get you running

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Automation

tidb-sql

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

pytidb

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

tidbx-serverless-driver

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

tidbx

No summary provided by upstream source.

Repository SourceNeeds Review