Prisma Client Overview
Prisma Client is a type-safe database client that’s auto-generated based on your Prisma schema. It provides a fluent API for querying your database with full TypeScript support and autocomplete.Key Features
Prisma Client offers powerful capabilities for working with your database:- Type Safety: Fully typed queries based on your schema
- Auto-completion: IntelliSense support in your editor
- Relation Loading: Easy to use
includeandselectfor nested data - Transactions: Both interactive and batch transaction support
- Middleware: Hook into the query execution pipeline
- Extensions: Extend client functionality with custom logic
- Raw SQL: Execute raw queries when needed
- Connection Pooling: Efficient connection management
Architecture (Prisma 7)
Prisma Client in version 7 uses a modern architecture:Core Components
Orchestrates query execution using WebAssembly query compiler. Located in
packages/client/src/runtime/core/engines/client/.Executes query plans against the database through driver adapters. Defined in
packages/client-engine-runtime/src/interpreter/query-interpreter.ts.JavaScript-based database drivers that connect to your database. Supports PostgreSQL, MySQL, SQLite, SQL Server via adapters like
@prisma/adapter-pg, @prisma/adapter-neon, etc.Query Execution Flow
When you execute a Prisma Client query:- Request Creation: Your query is converted to an internal request object
- Middleware Pipeline: Request passes through registered middleware
- Query Extensions: Client extensions modify the query if applicable
- Serialization: Query is serialized to JSON protocol format
- Query Compilation: WebAssembly compiler generates a query plan
- Execution: Executor runs the plan through the driver adapter
- Result Extensions: Results are transformed by result extensions
- Response: Typed result is returned to your code
/home/daytona/workspace/source/packages/client/src/runtime/getPrismaClient.ts:892-968
Available Operations
Prisma Client provides operations for each model in your schema:CRUD Operations
findUnique()- Find a single record by unique fieldfindUniqueOrThrow()- Find or throw errorfindFirst()- Find first matching recordfindFirstOrThrow()- Find first or throwfindMany()- Find multiple recordscreate()- Create a new recordcreateMany()- Create multiple recordsupdate()- Update a recordupdateMany()- Update multiple recordsupsert()- Update or createdelete()- Delete a recorddeleteMany()- Delete multiple records
Aggregation Operations
count()- Count recordsaggregate()- Perform aggregations (sum, avg, min, max, count)groupBy()- Group and aggregate
Raw Operations
$queryRaw()- Execute raw SQL SELECT queries$executeRaw()- Execute raw SQL modifications$queryRawUnsafe()- Unsafe variant with string interpolation$executeRawUnsafe()- Unsafe execute variant$runCommandRaw()- MongoDB-specific commands
/home/daytona/workspace/source/packages/client/src/runtime/core/model/applyModel.ts:20-34
Client Methods
Beyond model operations, PrismaClient provides utility methods:Connection Management
Explicitly connect to the database. Usually not needed as Prisma Client connects automatically.Source:
/home/daytona/workspace/source/packages/client/src/runtime/getPrismaClient.ts:494-500Disconnect from the database and release resources.Source:
/home/daytona/workspace/source/packages/client/src/runtime/getPrismaClient.ts:506-519Transactions
Execute operations in a transaction. Supports both batch and interactive transactions.Source:
/home/daytona/workspace/source/packages/client/src/runtime/getPrismaClient.ts:853-885Events
Listen to client events like queries, errors, and lifecycle events.Source:
/home/daytona/workspace/source/packages/client/src/runtime/getPrismaClient.ts:485-492Extensions
Extend the Prisma Client with custom functionality.Source:
/home/daytona/workspace/source/packages/client/src/runtime/core/extensions/$extends.ts:8-20Next Steps
Setup
Learn how to set up Prisma Client with driver adapters
CRUD Operations
Master create, read, update, and delete operations
Queries
Explore query patterns and filtering options
Transactions
Work with transactions for data consistency