Introduction to Prisma Client
Source URL: https://docs.prisma.io/docs/orm/prisma-client/setup-and-configuration/introduction
Introduction to Prisma Client
Section titled “Introduction to Prisma Client”Learn how to set up and configure Prisma Client in your project
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.
Prerequisites
Section titled “Prerequisites”In order to set up Prisma Client, you need a Prisma Config and a Prisma schema file:
Prisma Config
Prisma Schema
prisma.config.ts
import 'dotenv/config'; import { defineConfig, env } from 'prisma/config';
export default defineConfig({ schema: './prisma/schema.prisma', datasource: { url: env('DATABASE_URL'), }, });Installation
Section titled “Installation”Install the Prisma CLI, the Prisma Client library, and the driver adapter for your database:
PostgreSQL
MySQL / MariaDB
SQLite
npm
pnpm
yarn
bun
npm install prisma --save-dev npm install @prisma/client @prisma/adapter-pg pgPrisma 7 requires a driver adapter to connect to your database. Make sure your package.json includes "type": "module" for ESM support. See the upgrade guide for details.
Generate the Client API
Section titled “Generate the Client API”Prisma Client is based on the models in Prisma Schema. To provide the correct types, you need generate the client code:
npm
pnpm
yarn
bun
npx prisma generateThis will create a generated directory based on where you set the output to in the Prisma Schema. Any time your import Prisma Client, it will need to come from this generated client API.
Importing Prisma Client
Section titled “Importing Prisma Client”With the client generated, import it along with your driver adapter and create a new instance:
PostgreSQL
MySQL / MariaDB
SQLite
PostgreSQL (Edge)
import { PrismaClient } from "./path/to/generated/prisma"; import { PrismaPg } from "@prisma/adapter-pg";
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL!, });
export const prisma = new PrismaClient({ adapter });PrismaClient requires a driver adapter in Prisma 7. Calling new PrismaClient() without an adapter will result in an error.
Find out what driver adapter is needed for your database.
Your application should generally only create one instance of PrismaClient. How to achieve this depends on whether you are using Prisma ORM in a long-running application or in a serverless environment.
Creating multiple instances of PrismaClient will create multiple connection pools and can hit the connection limit for your database. Too many connections may start to slow down your database and eventually lead to errors such as:
Error in connector: Error querying the database: db error: FATAL: sorry, too many clients already at PrismaClientFetcher.requestUse Prisma Client to send queries to your database
Section titled “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` function const newUser = await prisma.user.create({ data: { name: "Alice", email: "alice@prisma.io", }, });
const users = await prisma.user.findMany();Evolving your application
Section titled “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 your output directory:
npm
pnpm
yarn
bun
npx prisma generate