All skills
prisma avatar

/prisma-client-api

@808913c official
by prismaprisma/skills66 stars
7

Prisma Client API reference covering model queries, filters, operators, and client methods. Use when writing database queries, using CRUD operations, filtering data, or configuring Prisma Client. Triggers on "prisma query", "findMany", "create", "update", "delete", "$transaction".

Use this Skill: https://skilld.dev/gh/prisma/skills/prisma-client-api

This session only. Nothing lands on disk.

referencesclient-methods.md

≈1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Client Methods

Prisma Client instance methods.

$connect()

Explicitly connect to the database:

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
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:

await prisma.$disconnect()

Graceful shutdown

process.on('beforeExit', async () => {
  await prisma.$disconnect()
})

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

In tests

afterAll(async () => {
  await prisma.$disconnect()
})

$on()

Subscribe to events:

Query events

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

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

const prisma = new PrismaClient({ adapter }).$extends({
  client: {
    $log: (message: string) => console.log(message)
  }
})

prisma.$log('Hello!')

Add model methods

const prisma = new PrismaClient({ adapter }).$extends({
  model: {
    user: {
      async findByEmail(email: string) {
        return prisma.user.findUnique({ where: { email } })
      }
    }
  }
})

const user = await prisma.user.findByEmail('alice@prisma.io')

Query extensions

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

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

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

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<{
  include: { posts: true }
}>

Type-safe query fragments with satisfies

Type-safe query fragments:

import { Prisma } from '../generated/client'

const userSelect = {
  id: true,
  email: true,
  name: true
} satisfies Prisma.UserSelect

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

With the prisma-client generator, use TypeScript satisfies for typed query fragments. You may still see older examples that use Prisma.validator() with prisma-client-js.

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides a comprehensive API reference for Prisma Client, covering CRUD operations, filtering, relations, and transactions. It includes documentation for raw SQL execution with explicit security warnings and best practices for preventing SQL injection. All external resources and packages are official components of the Prisma ecosystem.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    9 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 808913c. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 2 months ago
metadata
{
  "author": "prisma",
  "version": "7.9.1"
}

README badge

README badge for prisma/skills/prisma-client-api

Provides API reference for Prisma Client CRUD operations, filtering, relations, transactions, and client configuration. Use when writing database queries, performing create/read/update/delete operations, or configuring PrismaClient instantiation.

Generated from the current SKILL.md.

Does this skill cover Prisma's generated client types and TypeScript support?
The skill focuses on Prisma Client API reference and query patterns. TypeScript support is implicit in the examples, but the skill does not provide dedicated guidance on type safety, generated types, or satisfies patterns beyond mentioning them in the client-methods category.
What versions of Prisma does this skill support?
The skill is versioned 7.6.0 and targets current Prisma projects. It assumes you are using a recent Prisma Client release with adapters and extensions support.
Does this cover schema definition or migrations?
No. This skill covers only Prisma Client runtime queries and operations. Schema definition, migrations, and introspection are outside its scope.
Can I use this skill for raw SQL queries?
Yes. The skill includes a raw-queries category covering $queryRaw and $executeRaw, with guidance on safety patterns for executing raw SQL directly.
Does this skill explain how to set up Prisma Client with different databases?
The skill covers client construction and adapter wiring (e.g. PrismaPg), but database-specific setup and configuration details are deferred to the constructor reference file.

Generated from the current SKILL.md. These answers refresh after source changes.