Labsco
prisma logo

prisma-client-api-constructor

✓ Official8

by prisma · part of prisma/cursor-plugin

PrismaClient Constructor

🔥🔥🔥✓ VerifiedFreeQuick setup
🧩 One of 7 skills in the prisma/cursor-plugin package — works on its own, and pairs well with its siblings.

PrismaClient Constructor

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates — shown here so you can audit it before installing. You don't need to read it to use the skill.

by prisma

PrismaClient Constructor npx skills add https://github.com/prisma/cursor-plugin --skill prisma-client-api-constructor Download ZIPGitHub8

PrismaClient Constructor

Configure Prisma Client when instantiating.

Basic Instantiation (v7)

Copy & paste — that's it
import { PrismaClient } from '../generated/client'
import { PrismaPg } from '@prisma/adapter-pg'

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

const prisma = new PrismaClient({ adapter })

Constructor Options

adapter (Required in v7)

Driver adapter instance:

Copy & paste — that's it
import { PrismaPg } from '@prisma/adapter-pg'

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

const prisma = new PrismaClient({ adapter })

accelerateUrl (For Accelerate users)

Copy & paste — that's it
import { withAccelerate } from '@prisma/extension-accelerate'

const prisma = new PrismaClient({
 accelerateUrl: process.env.DATABASE_URL, // prisma:// URL
}).$extends(withAccelerate())

log

Configure logging:

Copy & paste — that's it
const prisma = new PrismaClient({
 adapter,
 log: ['query', 'info', 'warn', 'error'],
})

Log levels

Level Description query All SQL queries info Informational messages warn Warnings error Errors

Log to events

Copy & paste — that's it
const prisma = new PrismaClient({
 adapter,
 log: [
 { level: 'query', emit: 'event' },
 { level: 'error', emit: 'stdout' },
 ],
})

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

errorFormat

Control error formatting:

Copy & paste — that's it
const prisma = new PrismaClient({
 adapter,
 errorFormat: 'pretty', // 'pretty' | 'colorless' | 'minimal'
})

transactionOptions

Default transaction settings:

Copy & paste — that's it
const prisma = new PrismaClient({
 adapter,
 transactionOptions: {
 maxWait: 5000, // Max wait to acquire transaction (ms)
 timeout: 10000, // Max transaction duration (ms)
 isolationLevel: 'Serializable',
 },
})

Singleton Pattern

Prevent multiple client instances in development:

Copy & paste — that's it
// lib/prisma.ts
import { PrismaClient } from '../generated/client'
import { PrismaPg } from '@prisma/adapter-pg'

const globalForPrisma = globalThis as unknown as {
 prisma: PrismaClient | undefined
}

function createPrismaClient() {
 const adapter = new PrismaPg({
 connectionString: process.env.DATABASE_URL!
 })
 return new PrismaClient({ adapter })
}

export const prisma = globalForPrisma.prisma ?? createPrismaClient()

if (process.env.NODE_ENV !== 'production') {
 globalForPrisma.prisma = prisma
}

Next.js Pattern

Copy & paste — that's it
// lib/prisma.ts
import { PrismaClient } from '@/generated/client'
import { PrismaPg } from '@prisma/adapter-pg'

const createAdapter = () => new PrismaPg({
 connectionString: process.env.DATABASE_URL!
})

const prismaClientSingleton = () => {
 return new PrismaClient({ adapter: createAdapter() })
}

declare const globalThis: {
 prismaGlobal: ReturnType 
} & typeof global

const prisma = globalThis.prismaGlobal ?? prismaClientSingleton()

export default prisma

if (process.env.NODE_ENV !== 'production') {
 globalThis.prismaGlobal = prisma
}

Query Events

Listen to query events:

Copy & paste — that's it
const prisma = new PrismaClient({
 adapter,
 log: [{ level: 'query', emit: 'event' }],
})

prisma.$on('query', (e) => {
 console.log('Query:', e.query)
 console.log('Params:', e.params)
 console.log('Duration:', e.duration)
})

Log Events

Copy & paste — that's it
prisma.$on('info', (e) => console.log(e.message))
prisma.$on('warn', (e) => console.warn(e.message))
prisma.$on('error', (e) => console.error(e.message))