Overview
Prisma Engines follows a modular architecture with clear separation of concerns. The system is built around a core/connector pattern where core logic is implemented once and database-specific behavior is handled by connectors.Architecture Principles
Core / Connector Pattern
Both the Schema Engine and Query stack expose the same API across all supported databases. This is achieved through:- Core crates - Define the common API and orchestrate functionality
- Connector crates - Implement database-specific behavior
schema-core that coordinates functionality provided by connectors. Each supported database has its own connector implementing the schema-connector API. Most of the actual migration and introspection logic lives in these connectors.
Currently, all connectors are built-in and live in the prisma-engines repository, but the architecture supports external connectors in the future.
Query Compiler
Prisma Client now executes queries through a modern Query Compiler (QC) architecture that separates planning from execution.Architecture
The query stack consists of three layers:1. Query Compiler (Rust)
Location:query-compiler/
The Rust query compiler:
- Consumes the DataModel (DML) from PSL
- Produces query plans describing SQL and orchestration steps
- Handles query optimization and planning
- Compiles to WebAssembly for JavaScript runtime
query-compiler- Main compilation logicquery-core- Core query processingquery-structure- Query AST and data structuresquery-builder- SQL generationsql-query-builder- Database-specific SQL builders
2. Driver Adapters (TypeScript)
Location:libs/driver-adapters/
Driver adapters wrap database drivers in JavaScript:
- Load and interpret query plans from the compiler
- Execute SQL through Node.js/edge database drivers
- Handle transaction management
- Provide compatibility with existing tooling
@prisma/client-engine-runtime package (in the main Prisma repo) implements the query plan interpreter.
3. Query Plan Execution
Query plans describe:- SQL queries to execute
- Orchestration steps (joins, transformations)
- Transaction boundaries
- Result mapping
- Support for serverless databases (Neon, PlanetScale)
- Edge runtime compatibility (Cloudflare Workers, Vercel Edge)
- Custom database drivers
Development Workflow
When working on the query stack, you typically touch three layers:- Rust planner logic -
query-compiler,query-core,query-structure - Driver adapter executor -
libs/driver-adapters/executor - Integration tests -
cargo test -p query-engine-tests(viamake dev-*-qc)
Schema Engine
Location:schema-engine/
The Schema Engine is responsible for database schema management and migrations.
Core Functionality
The Schema Engine provides three main capabilities:1. Migration Generation (Diffing)
Compares Prisma schema with current database state to generate migrations:The shadow database is the only mechanism by which Migrate can determine what migrations do. Migrate treats migration files as black boxes and doesn’t parse SQL.
2. Migration Execution
Runs migrations and tracks applied migrations in the_prisma_migrations table:
checksum- SHA256 of migration file (detects modifications)finished_at- NULL means migration failed or is in progressrolled_back_at- Set byprisma migrate resolve --rolled-backstarted_at- When migration execution began
3. Introspection
Generates Prisma schema files from existing databases:Migration Workflow
Development
Run
prisma migrate dev to:- Check for drift between migrations and database
- Generate new migration if schema changed
- Apply migration to development database
- Regenerate Prisma Client
Review
- Review generated SQL in
prisma/migrations/ - Edit migration file if needed (add custom SQL)
- Commit migration to version control
Components
Key crates in the Schema Engine:schema-core- Core orchestration and APIschema-connector- Connector trait definitionssql-schema-connector- SQL database connector implementationmongodb-schema-connector- MongoDB connectorsql-schema-describer- Reads SQL database schemasmongodb-schema-describer- Reads MongoDB schemasdatamodel-renderer- Generates Prisma schema syntaxjson-rpc-api- JSON-RPC protocol for CLI communication
Why No Down Migrations?
Prisma Migrate uses a forward-only migration approach:In Development
In Development
Down migrations aren’t needed because
migrate dev:- Detects drift automatically
- Offers to reset development database
- Makes iteration easier than manual rollbacks
In Production
In Production
Down migrations give false sense of security:
- May not work if migration partially failed
- Can’t restore dropped data
- May fail on non-reversible changes
- Untested rollback code is risky under pressure
- Use expand-and-contract pattern
- Roll forward instead of back
- Use
migrate resolvefor manual recovery
Prisma Schema Language (PSL)
Location:psl/
PSL is the foundation that all engines build upon.
Crate Structure
The PSL implementation follows a clean dependency graph:diagnostics- Error and warning typesschema-ast- Abstract Syntax Tree for Prisma schemasparser-database- Semantic analysis and validationpsl-core- Core PSL functionality and connector definitionspsl- Public API used by other engines
Usage Across Engines
- Schema Engine - Validates schemas, reads datasource configuration
- Prisma Format - Formats schemas, provides LSP features
- Query Compiler - Consumes DataModel (DML) for query planning
Prisma Format
Location:prisma-fmt/
Prisma Format provides:
- Schema formatting - Consistent style for
.prismafiles - Language Server Protocol (LSP) - IDE features like autocomplete, go-to-definition, diagnostics
- Validation - Real-time schema validation
- WebAssembly module - Browser and Node.js support via
prisma-schema-wasm
LSP Features
- Code completion for models, fields, attributes
- Real-time error diagnostics
- Go to definition
- Hover information
- Rename refactoring
Driver Adapters
Location:libs/driver-adapters/
Driver adapters enable Prisma to work with various database drivers and runtimes.
Supported Adapters
pg- PostgreSQL (node-postgres)neon- Neon serverless PostgreSQLplanetscale- PlanetScale serverless MySQLlibsql- Turso/libSQL SQLitebetter-sqlite3- SQLited1- Cloudflare D1
Architecture
Driver adapters consist of:- Rust side (
libs/driver-adapters/src/) - Types and utilities - TypeScript side (in main Prisma repo) - Actual adapter implementations
- Executor harness (
libs/driver-adapters/executor/) - Test execution
query-engine/connector-test-kit-rs) exercises the full stack end-to-end by spawning the executor process and driving requests through adapters.
Shared Libraries
Location:libs/
Common functionality used across engines:
quaint- Database abstraction layer and query builderuser-facing-errors- Standardized error messages for usersprisma-value- Value types representing database valuestest-setup- Test database provisioningsql-ddl- SQL DDL generation utilitiestelemetry- Metrics and tracingmongodb-client- MongoDB connection handling
Testing Architecture
Prisma Engines has comprehensive test coverage:Unit Tests
Run unit tests for the entire workspace:tests/ folders throughout the codebase.
Integration Tests
Schema Engine:sql-migration-tests- Migration generation and executionsql-introspection-tests- Database introspection
query-engine-tests- End-to-end query execution testscore-tests- Query compiler unit tests
insta for snapshot testing.
Test Configuration
Tests use either environment variables or a.test_config file:
make dev-postgres15- PostgreSQL 15make dev-pg-qc- PostgreSQL with query compilermake dev-mysql8- MySQL 8
Build System
The project uses Cargo workspaces with:- Workspace dependencies - Centralized in root
Cargo.toml - Feature flags - Control compilation of optional functionality
- Release profiles - Optimized for size with LTO
- WebAssembly targets - Special build for query compiler and schema engine
Key Build Commands
CI/CD Pipeline
The engines integrate with the broader Prisma ecosystem:- Test - Full test suite runs on all supported databases
- Build - Engines compiled for multiple platforms
- Release - Engines published to S3 and R2
- Integration - Triggers
prisma/prismaintegration tests - Publish - NPM packages updated with new engine versions
Branches starting with
integration/ automatically trigger the full release pipeline for testing in the main Prisma repository.