npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

prisma-rest

v0.1.31

Published

Generate REST API routes for Next.js from Prisma schema

Downloads

7

Readme

prisma-rest

Generate REST API routes automatically from your Prisma schema for Next.js App Router.

Features

  • 🚀 Instant REST API - Generate CRUD endpoints from your Prisma models
  • 📁 Next.js App Router - Built for the modern Next.js app directory structure
  • 🔧 Flexible Configuration - Customize paths, imports, and behavior
  • 🎯 Smart Defaults - Auto-detects project structure and follows conventions
  • 🔄 Safe Regeneration - Multiple strategies for updating existing routes
  • 📝 TypeScript First - Fully typed route handlers with proper Next.js types
  • 💬 Comment Directives - Control generation with special comments in your schema

Installation

npm install -D prisma-rest
# or
yarn add -D prisma-rest
# or
pnpm add -D prisma-rest

Quick Start

  1. Ensure you have a Prisma schema in your Next.js project:
// prisma/schema.prisma
model User {
  id        String   @id @default(cuid())
  email     String   @unique
  name      String?
  posts     Post[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Post {
  id        String   @id @default(cuid())
  title     String
  content   String?
  published Boolean  @default(false)
  author    User?    @relation(fields: [authorId], references: [id])
  authorId  String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
  1. Generate your REST API routes:
npx prisma-rest generate
  1. Create your Prisma client instance:
// lib/prisma.ts (or src/lib/prisma.ts)
import { PrismaClient } from '@prisma/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;

That's it! Your REST API is ready at:

  • GET /api/users - List all users with pagination
  • POST /api/users - Create a new user
  • GET /api/users/[id] - Get a specific user
  • PUT /api/users/[id] - Update a user
  • DELETE /api/users/[id] - Delete a user

Generated Routes

For each Prisma model, the generator creates:

List & Create Routes (/api/[model]s/route.ts)

// GET /api/users - List with pagination
// Query params: ?page=1&limit=10
{
  "items": [...],
  "total": 100,
  "page": 1,
  "limit": 10,
  "totalPages": 10
}

// POST /api/users - Create new resource
// Body: { "email": "[email protected]", "name": "John Doe" }

Individual Resource Routes (/api/[model]s/[id]/route.ts)

// GET /api/users/[id] - Get single resource
// PUT /api/users/[id] - Update resource
// DELETE /api/users/[id] - Delete resource

CLI Options

prisma-rest generate [options]

Options:
  -s, --schema <path>         Path to Prisma schema file (default: "./prisma/schema.prisma")
  -o, --output <path>         Output directory for generated routes
  -b, --base-url <url>        Base URL for API routes (default: "/api")
  --api-prefix <prefix>       Additional path prefix (e.g., "rest" for /api/rest)
  -p, --prisma-import <path>  Import path for Prisma client (default: "@/lib/prisma")
  --include <models...>       Include only specific models
  --exclude <models...>       Exclude specific models
  --force                     Overwrite existing route files
  --skip-existing             Only generate routes for new models
  --dry-run                   Preview what would be generated
  -h, --help                  Display help

Examples

Custom Output Directory

# Generate in a custom directory
prisma-rest generate --output ./src/app/api/v2

# Generate with a prefix
prisma-rest generate --api-prefix rest
# Creates routes at /api/rest/users, /api/rest/posts, etc.

Custom Prisma Import

# If your Prisma client is in a different location
prisma-rest generate --prisma-import "@/server/db"

Selective Generation

# Only generate routes for specific models
prisma-rest generate --include User Post

# Generate all except certain models
prisma-rest generate --exclude Log Audit

Safe Regeneration

# Preview changes without creating files
prisma-rest generate --dry-run

# Skip existing files (only add new models)
prisma-rest generate --skip-existing

# Force overwrite all files
prisma-rest generate --force

Project Structure Support

The generator automatically detects your project structure:

  • ✅ Supports /src directory layouts
  • ✅ Detects App Router at /app or /src/app
  • ✅ Understands Next.js path aliases (@/)

Generated Code Features

  • TypeScript - Fully typed with Next.js route handler types
  • Error Handling - Consistent error responses with appropriate status codes
  • Pagination - Built-in pagination for list endpoints
  • Async/Await - Modern async patterns throughout
  • Prisma Best Practices - Efficient queries and proper error handling

Requirements

  • Next.js 13+ with App Router
  • Prisma 4+
  • TypeScript (recommended)

Comment Directives

Control route generation using special comments in your Prisma schema:

@rest-skip

Skip generation for specific models:

/// This model won't have REST routes generated
/// @rest-skip
model InternalLog {
  id        String   @id @default(cuid())
  message   String
  level     String
  createdAt DateTime @default(now())
}

This is useful for:

  • Internal models that shouldn't be exposed via API
  • Models that require custom handling
  • Temporary exclusion during development

Advanced Usage

Customizing Generated Routes

After generation, you can modify the routes to add:

  • Authentication middleware
  • Input validation
  • Custom business logic
  • Response transformations

The generated files include a header comment to track generation metadata.

Integration with Authentication

Example of adding auth to generated routes:

import { withAuth } from '@/lib/auth';

// Wrap the generated handler
export const GET = withAuth(async (request: NextRequest) => {
  // Generated code...
});

Troubleshooting

"Cannot find module '@/lib/prisma'"

Ensure you've created the Prisma client instance at the correct location. The default import path is @/lib/prisma, but you can customize it with the --prisma-import flag.

Routes Already Exist

By default, the generator won't overwrite existing files. Use:

  • --force to overwrite
  • --skip-existing to only generate new models
  • --dry-run to preview changes

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT © [Your Name]


Made with ❤️ for the Next.js + Prisma community