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
In order to set up Prisma Client, you need a Prisma Config and a Prisma schema file:
import 'dotenv/config';
import { defineConfig, env } from 'prisma/config';
export default defineConfig({
schema: './prisma/schema.prisma',
datasource: {
url: env('DATABASE_URL'),
},
});Installation
Install the Prisma CLI and the Prisma Client library in your project:
npm install prisma --save-dev
npm install @prisma/clientGenerate the Client API
Prisma Client is based on the models in Prisma Schema. To provide the correct types, you need generate the client code:
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
With the client generated, import the version for the given environment and create a new instance of it:
import { PrismaClient } from "./path/to/generated/prisma";
export const prisma = new PrismaClient({});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.requestDriver Adapters
Depending on the database being used, an adapter is needed in order for Prisma Client to be able send the appropriate SQL queries:
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});Find out what driver adapter is needed for your database
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
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:
npx prisma generatePostgreSQL extensions
How to install and manage PostgreSQL extensions with Prisma ORM using customized migrations, and how to use them in Prisma Client
Custom model and field names
Learn how you can decouple the naming of Prisma models from database tables to improve the ergonomics of the generated Prisma Client API