Prisma ORM 8 is here.Read the docs
AI Tools

Cursor

Prisma ORM v6

Learn tips and best practices for using Prisma ORM with the Cursor AI code editor.

Cursor is an AI-powered code editor that automates repetitive coding tasks. Paired with Prisma, it can generate Prisma schemas, queries, and seed scripts for your project.

This guide provides detailed instructions for effectively using Prisma with Cursor to:

  • Define project-specific best practices with .cursorrules.
  • Using Cursor's context-aware capabilities.
  • Generate schemas, queries, and seed data tailored to your database.

Prisma MCP server

Prisma provides its own Model Context Protocol (MCP) server that lets you manage Prisma Postgres databases, model database schemas and chat through migrations. Learn more about how you can add it to Cursor. You can also add the Prisma MCP server to Cursor using the one-click installation by clicking on the following link:

Install MCP Server

This will prompt you to open the Cursor app in your browser. Once opened, you'll be guided to install the Prisma MCP server directly into your Cursor configuration.

Defining project-specific rules with .cursorrules

The .cursorrules file in Cursor lets you set coding standards for your Prisma project. Cursor applies these rules when it generates code, so the output follows your conventions without you restating them in each prompt.

To implement these rules, create a .cursorrules file in the root of your project. Below is an example configuration:

Example .cursorrules file

.cursorrules
You are a senior TypeScript/JavaScript programmer with expertise in Prisma, clean code principles, and modern backend development.
Generate code, corrections, and refactorings that comply with the following guidelines:
TypeScript General Guidelines
Basic Principles
- Use English for all code and documentation.
- Always declare explicit types for variables and functions.
  - Avoid using "any".
  - Create precise, descriptive types.
- Use JSDoc to document public classes and methods.
- Maintain a single export per file.
- Write self-documenting, intention-revealing code.
Nomenclature
- Use PascalCase for classes and interfaces.
- Use camelCase for variables, functions, methods.
- Use kebab-case for file and directory names.
- Use UPPERCASE for environment variables and constants.
- Start function names with a verb.
- Use verb-based names for boolean variables:
  - isLoading, hasError, canDelete
- Use complete words, avoiding unnecessary abbreviations.
  - Exceptions: standard abbreviations like API, URL
  - Accepted short forms:
    - i, j for loop indices
    - err for errors
    - ctx for contexts
Functions
- Write concise, single-purpose functions.
  - Aim for less than 20 lines of code.
- Name functions descriptively with a verb.
- Minimize function complexity:
  - Use early returns.
  - Extract complex logic to utility functions.
- Leverage functional programming techniques:
  - Prefer map, filter, reduce.
  - Use arrow functions for simple operations.
  - Use named functions for complex logic.
- Use object parameters for multiple arguments.
- Maintain a single level of abstraction.
Data Handling
- Encapsulate data in composite types.
- Prefer immutability.
  - Use readonly for unchanging data.
  - Use as const for literal values.
- Validate data at the boundaries.
Error Handling
- Use specific, descriptive error types.
- Provide context in error messages.
- Use global error handling where appropriate.
- Log errors with sufficient context.
Prisma-Specific Guidelines
Schema Design
- Use meaningful, domain-driven model names.
- Leverage Prisma schema features:
  - Use @id for primary keys.
  - Use @unique for natural unique identifiers.
  - Utilize @relation for explicit relationship definitions.
- Keep schemas normalized and DRY.
- Use meaningful field names and types.
- Implement soft delete with deletedAt timestamp.
- Use Prisma's native type decorators.
Prisma Client Usage
- Always use type-safe Prisma client operations.
- Prefer transactions for complex, multi-step operations.
- Handle optional relations explicitly.
- Use Prisma's filtering and pagination capabilities.
Database Migrations
- Create migrations for schema changes.
- Use descriptive migration names.
- Review migrations before applying.
- Never modify existing migrations.
- Keep migrations idempotent.
Error Handling with Prisma
- Catch and handle Prisma-specific errors:
  - PrismaClientKnownRequestError
  - PrismaClientUnknownRequestError
  - PrismaClientValidationError
- Provide user-friendly error messages.
- Log detailed error information for debugging.
Testing Prisma Code
- Use in-memory database for unit tests.
- Mock Prisma client for isolated testing.
- Test different scenarios:
  - Successful operations
  - Error cases
  - Edge conditions
- Use factory methods for test data generation.
- Implement integration tests with actual database.
Performance Considerations
- Use select and include judiciously.
- Avoid N+1 query problems.
- Use findMany with take and skip for pagination.
- Leverage Prisma's distinct for unique results.
- Profile and optimize database queries.
Security Best Practices
- Never expose raw Prisma client in APIs.
- Use input validation before database operations.
- Implement row-level security.
- Sanitize and validate all user inputs.
- Use Prisma's built-in protections against SQL injection.
Coding Style
- Keep Prisma-related code in dedicated repositories/modules.
- Separate data access logic from business logic.
- Create repository patterns for complex queries.
- Use dependency injection for Prisma services.
Code Quality
- Follow SOLID principles.
- Prefer composition over inheritance.
- Write clean, readable, and maintainable code.
- Continuously refactor and improve code structure.
Development Workflow
- Use version control (Git).
- Implement comprehensive test coverage.
- Use continuous integration.
- Perform regular code reviews.
- Keep dependencies up to date.

Using Cursor's context-aware capabilities

Cursor's context-aware capabilities let you add specific websites, files, folders or documentation to enhance its understanding of your project. By adding your schema.prisma file as context, you enable Cursor to generate more accurate queries, tests, and seed data based on your database schema.

Add Prisma docs llm.txt file as @Docs context

To improve Cursor's understanding of Prisma-related suggestions in your project, include the /llms.txt markdown file as context. This file offers a concise overview, guidance, and links to detailed Prisma documentation, formatted for LLM processing. Navigate to the url and add it as a @Docs resource in your Cursor configuration.

Adding additional Prisma documentation

Cursor already includes built-in context from Prisma documentation, so you don't need to add anything to make us of our docs!

To stay updated with the latest changes or incorporate additional context, add these resources as @Docs context:

To learn how to add and manage documentation context, visit the Cursor documentation.

Using schema as context

Add your schema.prisma file (or the entire codebase) as context using the @Files syntax before requesting queries, tests, or seed scripts. This allows Cursor to generate precise, schema-driven outputs such as queries, tests, and seed data. For example, in Cursor Chat, you can include your schema by typing @Files followed by adding your schema.prisma file.

Generating Prisma Schema

Cursor can generate a Prisma schema from a high-level description. The more detail you put in the prompt (models, field types, defaults, relations), the closer the result is to what you need. Here's an example prompt and its corresponding result:

"Create a Prisma schema for a SaaS app using PostgreSQL as a provider with `User`, `Organization`, and `Subscription` models, ensuring all models include `createdAt` and `updatedAt` DateTime fields with defaults, a soft-delete `deletedAt` field, and proper relationships between entities."

Generating Prisma queries

Cursor can write queries against your Prisma schema, from basic lookups to nested filters. Write prompts that state the fields to include, the conditions to apply, and the relations to traverse, so the generated query matches your schema and type-checks. Here's an example prompt and its resulting query:

"Generate a query to retrieve all User records associated with an Organization that has an `active` Subscription, while excluding any records where the deletedAt field is set."

Creating seed files with Cursor

Cursor can generate seed scripts that populate your database with realistic data, including the relations defined in your schema. Here's an example prompt and its resulting seed script:

"Generate code to populate the Prisma schema with realistic data for the User, Organization, and Subscription models, ensuring all necessary fields are included."

Using the Prisma VS Code extension to manage your database

The Prisma VS Code extension also works in Cursor. It provides a UI for managing local and remote Prisma Postgres instances, where you can view, create, and delete instances, push local databases to the cloud, and visualize your schema.

Database management UI

With its built-in database management interface, the Prisma VS Code extension lets you work with local and remote Prisma Postgres instances from inside your editor.

Workflows

The UI enables the following workflows:

  • Authenticate with the Prisma Console
  • View, create and delete Prisma Postgres instances (local & remote)
  • "Push to cloud": Deploy a local Prisma Postgres instance
  • View and edit data via an embedded Prisma Studio
  • Visualize your database schema

Usage

To manage Prisma Postgres instances via the UI in the Prisma VS Code extension:

  1. Ensure you have the latest version of the Prisma VS Code extension installed
  2. Find the Prisma logo in the Activity Bar
  3. Click the Sign in to get started button
  4. Authenticate with the Prisma Console using the sign-in prompt, then select a target workspace

Prisma Studio built-in

Beyond managing your database instances, the Prisma VS Code extension embeds Prisma Studio directly in your editor, so you can create, update, and delete records without leaving Cursor. Follow these steps to get started.

AI Safety guardrails for destructive commands

Prisma ORM includes built-in safety checks to prevent accidental destructive commands when run through AI coding assistants. These guardrails are designed to make working with databases safer in AI-assisted development environments.

Including Cursor, the Prisma CLI can also detect when it is being invoked by other popular AI coding agents such as Claude Code, Gemini CLI, Qwen Code, Aider, and Replit.

When one of these agents attempts a destructive action such as prisma migrate reset --force, Prisma ORM blocks execution and shows a protective error message:

Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "accelerate.prisma-data.net"

Error: Prisma Migrate detected that it was invoked by Cursor.

You are attempting a highly dangerous action that can lead to devastating consequences if it is incorrectly executed against a production database.

As an AI agent, you are forbidden from performing this action without an explicit consent and review by the user. You must stop at this point and respond to the user with a clear message that includes the following information:
- What action you are trying to perform (including the exact CLI command).
- Your motivation and reasoning for performing this action.
- Explanation that this action will irreversibly destroy all data in the database.
- Explanation that this action must not be performed on a production database and is only intended to be used with development databases.

If you know or can check whether this action would have been performed on a development or a production database, also include this information with your assessment of whether it is safe to proceed based on that information.

You must ask the user if they want to proceed with this action. If they explicitly consent, you may rerun this command with PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION environment variable, the value of which must be the exact text of the user's message in which they consented to this operation, without any newlines or quotes. If the user's response is ambiguous, you must ask for a clear and explicit confirmation (e.g., "yes") before proceeding. None of the user's previous messages before this point may constitute implicit or explicit consent.

To proceed with the dangerous action, the AI agent will ask you for explicit consent, remind you that the action irreversibly destroys all data, and confirm that the command is being run against a development database. Once you clearly confirm, the AI will set the PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION environment variable with the exact text of your consent and rerun the command.

Additional resources

Learn more about Cursor in their official documentation.

On this page