Labsco
prisma logo

prisma-client-api-client-methods

✓ Official8

by prisma · part of prisma/cursor-plugin

Client Methods. Reference when using this Prisma feature.

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

Client Methods. Reference when using this Prisma feature.

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

Client Methods. Reference when using this Prisma feature. npx skills add https://github.com/prisma/cursor-plugin --skill prisma-client-api-client-methods Download ZIPGitHub8

Client Methods

Prisma Client instance methods.

$connect()

Explicitly connect to the database:

Copy & paste — that's it
const prisma = new PrismaClient({ adapter })

// Explicit connection
await prisma.$connect()

When to use

Usually not needed - Prisma connects automatically on first query. Use for:

  • Fail fast on startup

  • Health checks

  • Pre-warming connections

Copy & paste — that's it
async function main() {
 try {
 await prisma.$connect()
 console.log('Database connected')
 } catch (e) {
 console.error('Failed to connect:', e)
 process.exit(1)
 }
}

$disconnect()

Close database connection:

Copy & paste — that's it
await prisma.$disconnect()

Graceful shutdown

Copy & paste — that's it
process.on('beforeExit', async () => {
 await prisma.$disconnect()
})

// Or with SIGTERM
process.on('SIGTERM', async () => {
 await prisma.$disconnect()
 process.exit(0)
})

In tests

Copy & paste — that's it
afterAll(async () => {
 await prisma.$disconnect()
})

$on()

Subscribe to events:

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, 'ms')
})

Log events

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

prisma.$on('info', (e) => console.log(e.message))
prisma.$on('warn', (e) => console.warn(e.message))
prisma.$on('error', (e) => console.error(e.message))

$extends()

Add extensions for custom behavior:

Add custom methods

Copy & paste — that's it
const prisma = new PrismaClient({ adapter }).$extends({
 client: {
 $log: (message: string) => console.log(message)
 }
})

prisma.$log('Hello!')

Add model methods

Copy & paste — that's it
const prisma = new PrismaClient({ adapter }).$extends({
 model: {
 user: {
 async findByEmail(email: string) {
 return prisma.user.findUnique({ where: { email } })
 }
 }
 }
})

const user = await prisma.user.findByEmail('[email protected]')

Query extensions

Copy & paste — that's it
const prisma = new PrismaClient({ adapter }).$extends({
 query: {
 user: {
 async findMany({ args, query }) {
 // Add default filter
 args.where = { ...args.where, deletedAt: null }
 return query(args)
 }
 }
 }
})

Result extensions

Copy & paste — that's it
const prisma = new PrismaClient({ adapter }).$extends({
 result: {
 user: {
 fullName: {
 needs: { firstName: true, lastName: true },
 compute(user) {
 return `${user.firstName} ${user.lastName}`
 }
 }
 }
 }
})

const user = await prisma.user.findFirst()
console.log(user.fullName) // Computed field

Chain extensions

Copy & paste — that's it
const prisma = new PrismaClient({ adapter })
 .$extends(loggingExtension)
 .$extends(softDeleteExtension)
 .$extends(computedFieldsExtension)

$transaction()

See transactions.md for details.

$queryRaw() / $executeRaw()

See raw-queries.md for details.

Type utilities

Prisma namespace

Copy & paste — that's it
import { Prisma } from '../generated/client'

// Input types
type UserCreateInput = Prisma.UserCreateInput
type UserWhereInput = Prisma.UserWhereInput

// Output types
type User = Prisma.UserGetPayload 
type UserWithPosts = Prisma.UserGetPayload 

Prisma.validator

Type-safe query fragments:

Copy & paste — that's it
import { Prisma } from '../generated/client'

const userSelect = Prisma.validator ()({
 id: true,
 email: true,
 name: true
})

const user = await prisma.user.findUnique({
 where: { id: 1 },
 select: userSelect
})