Prisma Schema Language (PSL)
The Prisma Schema Language (PSL) is the core parser, validator, and formatter for Prisma schemas. It provides the foundation for all schema-related operations across the Prisma ecosystem.What is PSL?
PSL is the implementation of the Prisma Schema Language - a declarative language for defining database models, relations, and configuration. The PSL crates are responsible for:- Parsing schema files into an Abstract Syntax Tree (AST)
- Validating schemas against connector-specific rules and constraints
- Analyzing schema structure and relationships
- Formatting and reformatting schema code
- Providing APIs for other Prisma engines to work with schemas
PSL is connector-agnostic at its core. Database-specific validations and features are handled through the connector trait system.
Architecture Overview
The PSL implementation is organized into several focused crates with a clear dependency hierarchy:Crate Dependency Graph
The dependency structure is intentionally simple and linear:The dml crate is a separate data structure that can optionally be produced (“lifted”) from a validated schema. It’s not part of the main dependency chain.
Core Crates
diagnostics
Provides error and warning reporting with source span information:DatamodelError- Error types with source locationsDatamodelWarning- Warning typesDiagnostics- Collection of errors and warningsSpanandFileId- Source location tracking- Pretty-printing utilities for user-facing error messages
schema-ast
The Abstract Syntax Tree representation:- Faithfully represents Prisma Schema syntax with source spans
- Built using the Pest parser generator
- Supports single and multi-file schemas
- Provides AST nodes for models, fields, attributes, enums, etc.
parse_schema()- Parse string to ASTreformat()- Format schema codeSourceFile- File content wrapper
parser-database
Connector-agnostic semantic analysis:- Resolves names to IDs for all schema items
- Resolves types for fields and type aliases
- Validates attributes on models and fields
- Performs global validations (e.g., index name collisions)
- Provides walker APIs for traversing validated schemas
- Name resolution
- Type resolution
- Attribute validation
- Global validations
ParserDatabase never fails - it accumulates as much information as possible and returns diagnostics alongside incomplete but usable results.
psl-core
The core implementation entry point:- Defines the
Connectortrait for database-specific behavior - Implements validation pipeline
- Provides configuration parsing
- Handles datasource and generator blocks
- Coordinates connector-specific validations
Connector- Database connector interfaceExtensionTypes- Custom type system extensions
builtin-connectors
Implements connectors for supported databases:- PostgreSQL - Full featured connector with extensions support
- MySQL - Including PlanetScale compatibility
- SQLite - Embedded database support
- SQL Server - Microsoft SQL Server connector
- CockroachDB - Distributed SQL database
- MongoDB - Document database (limited QC support)
- Native type mappings
- Capability flags
- Database-specific validations
- Referential action support
psl
The public API crate - the main entry point used by other engines:parse_schema()- Full parsing and validationparse_configuration()- Parse only datasource/generator blocksvalidate()- Validation with error tolerancereformat()- Code formatting
Usage Example
Parsing and validating a schema:Integration Points
PSL is used throughout the Prisma ecosystem:- Query Compiler - Processes schemas to produce client API and DMMF JSON
- Schema Engine - Powers migrations and introspection
- prisma-fmt - Language server and formatter
- Prisma CLI - Schema validation and configuration
Testing
Run the test suite:PSL uses declarative testing where
.prisma files in tests/validation/ are automatically validated and their errors are compared against comment expectations.Key Design Principles
- Connector Agnostic Core - Database-specific logic lives in connectors
- Error Tolerance - Validation accumulates diagnostics without failing
- Source Fidelity - AST preserves exact source locations for errors
- Simple Dependencies - Linear dependency graph prevents cycles
- Walker Pattern - Type-safe traversal of validated schemas
Next Steps
Parser Implementation
Deep dive into the parser and schema-ast
Validation
Learn about validation and parser-database
Connectors
Explore database connectors and capabilities
Contributing
Contributing guide for PSL development