query-compiler crate is the core of Prisma’s query compilation architecture. It transforms high-level query graphs into executable expression trees that can be interpreted by driver adapters in Prisma Client.
Overview
The query compiler takes aQueryGraph and connection information as input, then produces an Expression tree representing the query execution plan. This expression tree is sent to the TypeScript interpreter in Prisma Client, which executes it using driver adapters.
Installation
Core Functions
compile
Compiles a query operation into an executable expression tree.The query schema containing model and field definitions
The query operation to compile (read or write)
Database connection information including SQL family
The compiled expression tree ready for execution
Compilation error if the query cannot be compiled
Types
Expression
The core expression type representing query execution steps.Expression Variants
A plain value literal
Sequence of expressions evaluated in order. Returns the result of the last expression.
A database query that returns data
A database query that returns the number of affected rows
Application-level join operation for loading relations
Wraps an expression to run inside a database transaction
CompileError
Errors that can occur during compilation.Binding
Represents a let-binding in the expression tree.FieldOperation
Operations that can be performed on record fields.InMemoryOps
Operations performed in-memory on query results.Methods
Expression::simplify
Simplifies an expression tree by removing redundant operations.- Removing single-element sequences
- Inlining single bindings
- Flattening nested operations
Expression::pretty_print
Formats the expression tree for human-readable display.Whether to include ANSI color codes in output
Maximum line width for formatting
Feature Flags
The crate supports database-specific features:Dependencies
Key dependencies:psl- Prisma Schema Language parserquery-structure- Query structure typesquery-builder- SQL query builderquery-core- Core query graph typessql-query-builder- SQL-specific query buildingquaint- Database abstraction layer
Examples
Basic Query Compilation
Related
- query-structure - Type definitions for queries
- Query Compiler Overview - Architecture and design
- Driver Adapters - Execution layer