Prisma Client
Prisma Client is an auto-generated and type-safe query builder that's tailored to your data. The easiest way to get started with Prisma Client is by following the Quickstart.
Quickstart (5 min)The setup instructions below provide a high-level overview of the steps needed to set up Prisma Client. If you want to get started using Prisma Client with your own database, follow one of these guides:
Set up a new project from scratchAdd Prisma to an existing project
Set up
1. Prerequisites
In order to set up Prisma Client, you need a Prisma schema file with your database connection, the Prisma Client generator, and at least one model:
schema.prisma
1datasource db {2 url = env("DATABASE_URL")3 provider = "postgresql"4}56generator client {7 provider = "prisma-client-js"8}910model User {11 id Int @id @default(autoincrement())12 createdAt DateTime @default(now())13 email String @unique14 name String?15}
Also make sure to install the Prisma CLI:
npm install prisma --save-devnpx prisma
2. Installation
Install Prisma Client in your project with the following command:
npm install @prisma/client
This command also runs the prisma generate
command, which generates Prisma Client into the node_modules/.prisma/client
directory.
3. Importing Prisma Client
There are multiple ways to import Prisma Client in your project depending on your use case:
import { PrismaClient } from '@prisma/client'const prisma = new PrismaClient()// use `prisma` in your application to read and write data in your DB
For edge environments, you can import Prisma Client as follows:
import { PrismaClient } from '@prisma/client/edge'const prisma = new PrismaClient()// use `prisma` in your application to read and write data in your DB
For Deno, you can import Prisma Client as follows:
lib/prisma.ts
1import { PrismaClient } from '../prisma/generated/client/edge.ts'23const prisma = new PrismaClient()4// use `prisma` in your application to read and write data in your DB
The import path will depend on the custom output
specified in Prisma Client's generator
block in your Prisma schema.
4. Use Prisma Client to send queries to your database
Once you have instantiated PrismaClient
, you can start sending queries in your code:
// run inside `async` functionconst newUser = await prisma.user.create({data: {name: 'Alice',email: 'alice@prisma.io',},})const users = await prisma.user.findMany()
All Prisma Client methods return an instance of PrismaPromise
which only executes when you call await
or .then()
or .catch()
.
5. Evolving your application
Whenever you make changes to your database that are reflected in the Prisma schema, you need to manually re-generate Prisma Client to update the generated code in the node_modules/.prisma/client
directory:
prisma generate
In this section
- Working with PrismaClient
- CRUD
- Select fields
- Relation queries
- Filtering and Sorting
- Working with fields
- Advanced type safety
- Extensions
- Middleware
- Pagination
- Aggregation, grouping, and summarizing
- Composite types
- Transactions and batch queries
- Full-text search
- Custom validation
- Computed fields
- Excluding fields
- Custom models
- Read replicas
- Case sensitivity
- Null and undefined
- Raw database access
- Debugging
- Module bundlers
- Database polyfills
- Handling exceptions and errors
- Metrics
- OpenTelemetry tracing