Prisma
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
Install / Use
/learn @prisma/PrismaREADME

What is Prisma?
Prisma ORM is a next-generation ORM that consists of these tools:
- Prisma Client: Auto-generated and type-safe query builder for Node.js & TypeScript
- Prisma Migrate: Declarative data modeling & migration system
- Prisma Studio: GUI to view and edit data in your database
Prisma Client can be used in any Node.js or TypeScript backend application (including serverless applications and microservices). This can be a REST API, a GraphQL API, a gRPC API, or anything else that needs a database.
If you need a database to use with Prisma ORM, check out Prisma Postgres or if you are looking for our MCP Server, head here.
Getting started
Quickstart (5min)
The fastest way to get started with Prisma is by following the quickstart guides. You can choose either of two databases:
Bring your own database
If you already have your own database, you can follow these guides:
How Prisma ORM works
This section provides a high-level overview of how Prisma ORM works and its most important technical components. For a more thorough introduction, visit the Prisma documentation.
The Prisma schema
Every project that uses a tool from the Prisma toolkit starts with a Prisma schema file. The Prisma schema allows developers to define their application models in an intuitive data modeling language and configure generators.
// Data source
datasource db {
provider = "postgresql"
}
// Generator
generator client {
provider = "prisma-client"
output = "../generated"
}
// Data model
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
In this schema, you configure three things:
- Data source: Specifies your database type and thus defines the features and data types you can use in the schema
- Generator: Indicates that you want to generate Prisma Client
- Data model: Defines your application models
prisma.config.ts
Database connection details are defined via prisma.config.ts.
import { defineConfig } from 'prisma/config'
export default defineConfig({
datasource: {
url: 'postgres://...',
},
})
If you store the database connection string in process.env, an env function can help you access it in a type safe way and throw an error if it is missing at run time:
import { defineConfig, env } from 'prisma/config'
export default defineConfig({
datasource: {
url: env('DATABASE_URL'),
},
})
Prisma ORM does not load the .env files for you automatically. If you want to populate the environment variables from a .env file, consider using a package such as dotenv or @dotenvx/dotenvx.
The configuration file may look like this in that case:
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config'
export default defineConfig({
datasource: {
url: env('DATABASE_URL'),
},
})
To start a local PostgreSQL development server without using Docker and without any configuration, run prisma dev:
npx prisma dev
Alternatively, spin up an instant Prisma Postgres® database in the cloud:
npx create-db --interactive
The Prisma data model
On this page, the focus is on the data model. You can learn more about Data sources and Generators on the respective docs pages.
Functions of Prisma models
The data model is a collection of models. A model has two major functions:
- Represent a table in the underlying database
- Provide the foundation for the queries in the Prisma Client API
Getting a data model
There are two major workflows for "getting" a data model into your Prisma schema:
- Generate the data model from introspecting a database
- Manually writing the data model and mapping it to the database with Prisma Migrate
Once the data model is defined, you can generate Prisma Client which will expose CRUD and more queries for the defined models. If you're using TypeScript, you'll get full type-safety for all queries (even when only retrieving the subsets of a model's fields).
Accessing your database with Prisma Client
Step 1: Install Prisma
First, install Prisma CLI as a development dependency and Prisma Client:
npm install prisma --save-dev
npm install @prisma/client
Step 2: Set up your Prisma schema
Ensure your Prisma schema includes a generator block with an output path specified:
generator client {
provider = "prisma-client"
output = "../generated"
}
datasource db {
provider = "postgresql" // mysql, sqlite, sqlserver, mongodb or cockroachdb
}
Step 3: Configure Prisma Config
Configure the Prisma CLI using a prisma.config.ts file. This file configures Prisma CLI subcommands like migrate and studio. Create a prisma.config.ts file in your project root:
import { defineConfig, env } from 'prisma/config'
type Env = {
DATABASE_URL: string
}
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env<Env>('DATABASE_URL'),
},
})
Note: Environment variables from .env files are not automatically loaded when using prisma.config.ts. You can use dotenv by importing dotenv/config at the top of your config file. For Bun, .env files are automatically loaded.
Learn more about Prisma Config and all available configuration options.
Step 4: Generate Prisma Client
Generate Prisma Client with the following command:
npx prisma generate
This command reads your Prisma schema and generates the Prisma Client code in the location specified by the output path in your generator configuration.
After you change your data model, you'll need to manually re-generate Prisma Client to ensure the generated code gets updated:
npx prisma generate
Refer to the documentation for more information about "generating the Prisma client".
Step 5: Use Prisma Client to send queries to your database
Once the Prisma Client is generated, you can import it in your code and send queries to your database.
Import and instantiate Prisma Client
You can import and instantiate Prisma Client from the output path specified in your generator configuration. When instantiating the Client, you need to provide a [driver adapter](https://www.prisma.io/docs/orm/core-concepts/supported-databases/database-drivers
Related Skills
Writing Hookify Rules
83.4kThis skill should be used when the user asks to "create a hookify rule", "write a hook rule", "configure hookify", "add a hookify rule", or needs guidance on hookify rule syntax and patterns.
review-duplication
99.3kUse this skill during code reviews to proactively investigate the codebase for duplicated functionality, reinvented wheels, or failure to reuse existing project best practices and shared utilities.
notion
338.0kNotion API for creating and managing pages, databases, and blocks.
feishu-drive
338.0k|
