AGENT SKILL

nextjs-add-prisma-db

Setup Prisma ORM with PostgreSQL in a Next.js project with schema, migrations, and seed data

License 许可
MIT
所属仓库
zuzu59/z-skills
最近更新
2026-09-30
Stars
★ 0
Forks
1

01 前言 Front Matter

name
nextjs-add-prisma-db
description
Setup Prisma ORM with PostgreSQL in a Next.js project with schema, migrations, and seed data

03 SKILL.md 正文

<objective> Setup Prisma in the current Next.js project with a production-ready database configuration.

This creates a complete database layer including schema, migrations, client helper, seed script, and API routes for verification. Designed for projects using Neon PostgreSQL. </objective>

<context> Project structure: !ls -la Package manager: !cat package.json | grep -A2 '"packageManager"' Existing prisma config: !ls prisma/ 2>/dev/null || echo "No prisma folder yet" TypeScript config: !cat tsconfig.json | head -30 </context>

<process>

Phase 1: Information Gathering

  1. Ask for table structure - Prompt user to describe ONE simple table to verify Prisma works:

    • Table name and purpose
    • Fields with types (String, Int, DateTime, Boolean)
    • Example: "Post table with id, title, content, published, createdAt"
  2. Inspect existing structure - Before coding:

    • Check if src/ folder exists
    • Read tsconfig.json for path aliases
    • Identify where lib/ folder should be created

Phase 2: Installation

  1. Install dependencies:

    pnpm add prisma @prisma/client
    pnpm add -D tsx
    npx prisma init
    
  2. Configure database provider - Update prisma/schema.prisma:

    • Set provider to "postgresql"
    • Configure generator output path based on project structure

Phase 3: Schema & Configuration

  1. Create schema - Add the user-defined table with:

    • id as String with @id @default(cuid())
    • User-specified fields
    • createdAt DateTime @default(now())
    • updatedAt DateTime @updatedAt
  2. Configure exclusions:

    • Add generated folder to .eslintignore
    • Add generated folder to tsconfig.json exclude array
  3. Create Prisma client helper - Create singleton pattern in lib/prisma.ts:

    import { PrismaClient } from "@/generated/client";
    
    const globalForPrisma = globalThis as unknown as {
      prisma: PrismaClient | undefined;
    };
    
    export const prisma = globalForPrisma.prisma ?? new PrismaClient();
    
    if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
    
  4. Create prisma.config.ts - Root configuration:

    import path from "node:path";
    import { defineConfig } from "prisma/config";
    
    export default defineConfig({
      schema: path.join("prisma", "schema.prisma"),
      migrations: {
        seed: `tsx prisma/seed.ts`,
      },
    });
    

Phase 4: Data & Verification

  1. Create seed script - prisma/seed.ts with 5-10 realistic records

  2. Run migrations:

    npx prisma generate
    npx prisma migrate dev --name init
    npx prisma db seed
    
  3. Create example API route - app/api/example/route.ts:

    • GET endpoint fetching all records
    • POST endpoint creating new record
    • Proper error handling
  4. Add package.json scripts:

    {
      "db:generate": "prisma generate",
      "db:push": "prisma db push",
      "db:migrate": "prisma migrate dev",
      "db:studio": "prisma studio",
      "db:seed": "prisma db seed"
    }
    

Phase 5: Documentation

  1. Update CLAUDE.md with:
    • Database provider and connection info
    • Schema overview
    • Available db scripts </process>

<constraints> FORBIDDEN - Do not create:

  • User, Account, Session, VerificationToken tables (auth tables)
  • Any user-related fields or foreign keys
  • More than ONE table (keep it minimal for verification)

REQUIRED:

  • Adapt paths to existing project structure
  • Use cuid() for IDs, not uuid or auto-increment
  • Configure generator output path correctly
  • Create seed data with realistic values </constraints>

<success_criteria>

  • pnpm prisma generate completes without errors
  • pnpm prisma migrate dev creates migration successfully
  • pnpm prisma db seed populates database
  • API route returns data when called
  • No TypeScript or ESLint errors in generated files </success_criteria>

04 同仓库更多 Skills