The following example demonstrates how to import and instantiate your generated client from the default path:

TypeScript
Javascript
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB

PrismaClient accepts parameters that determine:

Use a single shared instance of PrismaClient

In long-running applications, we recommend that you:

Note: You do not need to explicitly $disconnect() in the context of a long-running application, such as a GraphQL server.

Multiple instances of PrismaClient results in multiple instances of the query engine. Each query engine creates a connection pool with a default pool size of num_physical_cpus * 2 + 1. Too many connections may start to slow down your database and eventually lead to the following error:

Error in connector: Error querying the database: db error: FATAL: sorry, too many clients already
at PrismaClientFetcher.request

To re-use a single instance, create a module that exports a PrismaClient object:

client.ts
1import { PrismaClient } from "@prisma/client"
2
3let prisma = new PrismaClient()
4
5export default prisma

The object is cached the first time the module is imported. Subsequent requests return the cached object rather than creating a new PrismaClient:

app.ts
1import prisma from './client'
2
3async function main() {
4 const allUsers = await prisma.user.findMany();
5}
6
7main();

Prevent hot reloading from creating new instances of PrismaClient

Frameworks like Next.js support hot reloading of changed files, which enables you to see changes to your application without restarting. However, if the framework refreshes the module responsible for exporting PrismaClient, this can result in additional, unwanted instances of PrismaClient in a development environment.

As a workaround, you can store PrismaClient as a global variable in development environments only, as global variables are not reloaded:

client.ts
1import { PrismaClient } from "@prisma/client"
2
3declare global {
4 namespace NodeJS {
5 interface Global {
6 prisma: PrismaClient;
7 }
8 }
9}
10
11let prisma: PrismaClient;
12
13if (process.env.NODE_ENV === "production") {
14 prisma = new PrismaClient()
15} else {
16 if (!global.prisma) {
17 global.prisma = new PrismaClient()
18 }
19
20 prisma = global.prisma
21}
22
23export default prisma

The way that you import and use the client does not change:

app.ts
1import prisma from './client'
2
3async function main() {
4 const allUsers = await prisma.user.findMany();
5}
6
7main();
Edit this page on GitHub