Skip to main content

PrismaClient Constructor

The PrismaClient constructor initializes a new Prisma Client instance with the specified configuration options.

Constructor Signature

class PrismaClient {
  constructor(options: PrismaClientOptions)
}

PrismaClientOptions

The constructor accepts a PrismaClientOptions object with the following properties:
adapter
SqlDriverAdapterFactory
required
Instance of a Driver Adapter for connecting to your database.Example:
import { PrismaPg } from '@prisma/adapter-pg'
import pg from 'pg'

const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL })
const adapter = new PrismaPg(pool)

const prisma = new PrismaClient({ adapter })
Available adapters:
  • @prisma/adapter-pg - PostgreSQL
  • @prisma/adapter-neon - Neon serverless
  • @prisma/adapter-planetscale - PlanetScale
  • @prisma/adapter-libsql - Turso/libSQL
  • @prisma/adapter-d1 - Cloudflare D1
  • @prisma/adapter-better-sqlite3 - Better SQLite3
  • @prisma/adapter-mssql - SQL Server
  • @prisma/adapter-mariadb - MariaDB
The adapter and accelerateUrl options are mutually exclusive. You must provide one or the other.
accelerateUrl
string
Prisma Accelerate URL for connecting through Accelerate instead of a direct database connection.Example:
const prisma = new PrismaClient({
  accelerateUrl: 'prisma://accelerate.prisma-data.net/?api_key=...'
})
The adapter and accelerateUrl options are mutually exclusive.
errorFormat
'pretty' | 'colorless' | 'minimal'
default:"'colorless'"
Configures the formatting of error messages returned by Prisma Client.
  • pretty - Colorized error messages with code frames (not recommended for production)
  • colorless - Plain text error messages with code frames
  • minimal - Minimal error messages without code frames (recommended for production)
Default behavior:
  • In production (NODE_ENV=production): 'minimal'
  • When NO_COLOR is set: 'colorless'
  • Otherwise: 'colorless'
Example:
const prisma = new PrismaClient({
  errorFormat: 'minimal'
})
log
Array<LogLevel | LogDefinition>
Configures logging for Prisma Client operations.Log levels: 'query', 'info', 'warn', 'error'Simple logging (to stdout):
const prisma = new PrismaClient({
  log: ['query', 'info', 'warn', 'error']
})
Event-based logging:
const prisma = new PrismaClient({
  log: [
    { emit: 'event', level: 'query' },
    { emit: 'stdout', level: 'error' },
    { emit: 'stdout', level: 'info' },
    { emit: 'stdout', level: 'warn' }
  ]
})

prisma.$on('query', (e) => {
  console.log('Query:', e.query)
  console.log('Params:', e.params)
  console.log('Duration:', e.duration, 'ms')
})
LogDefinition type:
type LogDefinition = {
  level: 'info' | 'query' | 'warn' | 'error'
  emit: 'stdout' | 'event'
}
transactionOptions
TransactionOptions
Default configuration for interactive transactions.
type TransactionOptions = {
  maxWait?: number      // default: 2000 (ms)
  timeout?: number      // default: 5000 (ms)
  isolationLevel?: IsolationLevel
}

type IsolationLevel =
  | 'ReadUncommitted'
  | 'ReadCommitted'
  | 'RepeatableRead'
  | 'Serializable'
  | 'Snapshot'
Example:
const prisma = new PrismaClient({
  transactionOptions: {
    maxWait: 5000,      // Wait up to 5s for transaction to start
    timeout: 10000,     // Allow transaction to run for up to 10s
    isolationLevel: 'Serializable'
  }
})
  • maxWait - Maximum time (in ms) to wait for a transaction to start
  • timeout - Maximum time (in ms) for the transaction to run
  • isolationLevel - Transaction isolation level (database-dependent)
omit
GlobalOmitOptions
Globally omit specific fields from all query results.Example:
const prisma = new PrismaClient({
  omit: {
    user: {
      password: true
    }
  }
})

// The `password` field will be automatically excluded from all User queries
const user = await prisma.user.findUnique({ where: { id: 1 } })
// user.password is undefined
comments
SqlCommenterPlugin[]
SQL commenter plugins that add metadata to SQL queries as comments following the sqlcommenter format.Example:
import { traceContext } from '@prisma/sqlcommenter-trace-context'
import { queryInsights } from '@prisma/sqlcommenter-query-insights'

const prisma = new PrismaClient({
  adapter,
  comments: [
    traceContext(),
    queryInsights()
  ]
})
Available plugins:
  • @prisma/sqlcommenter-trace-context - Adds W3C Trace Context
  • @prisma/sqlcommenter-query-insights - Adds query shape information
  • @prisma/sqlcommenter-query-tags - Adds custom query tags

Instance Methods

After instantiation, the PrismaClient instance provides these core methods:

$connect()

Manually establishes a connection to the database.
await prisma.$connect()
Prisma Client automatically connects on the first query, so calling $connect() is optional.

$disconnect()

Closes the database connection and cleans up resources.
await prisma.$disconnect()
Always call $disconnect() when your application shuts down to prevent connection leaks.

$on()

Registers an event listener for log events or the beforeExit event.
prisma.$on(eventType: string, callback: Function)
Example:
prisma.$on('query', (e) => {
  console.log('Query:', e.query)
  console.log('Duration:', e.duration, 'ms')
})

prisma.$on('beforeExit', async () => {
  console.log('Prisma Client is shutting down')
  // Perform cleanup
})

Complete Example

import { PrismaClient } from '@prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
import pg from 'pg'

const pool = new pg.Pool({
  connectionString: process.env.DATABASE_URL
})

const prisma = new PrismaClient({
  adapter: new PrismaPg(pool),
  errorFormat: 'minimal',
  log: [
    { emit: 'event', level: 'query' },
    { emit: 'stdout', level: 'error' }
  ],
  transactionOptions: {
    maxWait: 5000,
    timeout: 10000,
    isolationLevel: 'ReadCommitted'
  }
})

prisma.$on('query', (e) => {
  console.log(`Query took ${e.duration}ms`)
})

// Use the client
const users = await prisma.user.findMany()

// Clean up
await prisma.$disconnect()

See Also

Build docs developers (and LLMs) love