Back to Prisma

ARCHITECTURE.md

ARCHITECTURE.md

7.10.0-dev.3215.4 KB
Original Source

ARCHITECTURE.md

This file's purpose

This document describes the repository's high-level architecture and provides practical guidance and workflows for contributors. It explains how to generate dependency graphs, how the DMMF (Data Model Meta Format) relates to the Prisma Client, and step-by-step instructions for debugging and upgrading the engines used by the project.

High-Level Overview

Prisma is organized as a pnpm/Turborepo monorepo. The repository is grouped into logical layers that separate concerns and make it easier for contributors to find and modify code.

LayerKey PackagesPurpose
User-facingpackages/cli, packages/client, packages/migrateCLI, runtime client, and schema migration tooling — what end users interact with directly
Code generationpackages/client-generator-js, packages/client-generator-ts, packages/ts-buildersGenerators and helpers that emit the Prisma Client and TypeScript types
Execution & Enginespackages/client-engine-runtime, packages/engines, packages/fetch-engine, packages/get-platform, packages/json-protocolQuery execution, engine binary management, platform detection, and engine protocol glue
Database connectivitypackages/driver-adapter-utils, packages/adapter-*, packages/bundled-js-driversDriver abstractions and adapter implementations for PostgreSQL, MySQL, SQLite, serverless providers, etc.
Configuration & Utilitiespackages/config, packages/internals, packages/schema-files-loader, helpers/Config loaders, shared helpers, and schema/file utilities used throughout the repo
Extensions & integrationspackages/sqlcommenter*, packages/query-plan-executor, packages/instrumentationPlugins, monitoring, telemetry, and optional runtime integrations
Testing & CIpackages/integration-tests, docker/, scripts/, test/End-to-end suites, Docker test environments, and CI automation

Short notes:

  • The repository favors single-purpose packages (many packages/*) so changes are localized and discoverable.
  • Most development flows start in packages/ and use pnpm scripts defined at the repo root; CI and test orchestration live under scripts/ and docker/.
  • Visual diagrams (in graphs/) provide a quick entry-point for new contributors — see the "Architecture Diagrams" section for generation steps.

Repository layout (high level)

This repo is a pnpm/Turborepo monorepo. Most of the product code lives in packages/, with supporting tooling and fixtures at the repo root.

Root folders

FolderPurposeKey Contents
packages/Monorepo packages containing all product codeCLI, client, engines, migrate, adapters, generators, tests, utilities
scripts/Automation and maintenanceci/publish.ts (build orchestration), bump-engines.ts, bench.ts, graph-dependencies.ts
helpers/Shared build/test utilitiescompile/ (build configs), blaze/ (utility functions), test/ (test helpers)
docker/Database test environmentsdocker-compose.yml, PostgreSQL, MongoDB, MariaDB, MSSQL containers
graphs/Generated architecture diagramsGraphViz .dot files and rendered .png outputs (build artifacts)
examples/Example projects and templatesSample apps demonstrating Prisma usage patterns
sandbox/Development and debugging helpersDMMF explorer, basic setups for quick testing
eslint-local-rules/Custom ESLint rules for this repoType safety checks, import conventions

Notable packages

Core User-Facing Packages

PackagePurposeDependencies
packages/cliPrisma CLI entry point (prisma command)Implements all CLI commands, delegates to migrate, client, etc.
packages/clientPrisma Client runtime & generated client integrationOrchestrates query execution via ClientEngine
packages/migratePrisma Migrate & DB commandsSchema migrations, introspection, push, fixtures for testing

Code Generation & Types

PackagePurpose
packages/client-generator-jsLegacy prisma-client-js generator
packages/client-generator-tsNew prisma-client generator (TS-first)
packages/client-generator-registryGenerator plugin registry
packages/ts-buildersFluent API for generating TypeScript code & types
packages/dmmfData Model Meta Format types & utilities

Runtime & Query Execution

PackagePurpose
packages/client-engine-runtimeCore query execution engine (WASM-based)
packages/enginesRust binary engine wrapper & downloader
packages/fetch-engineEngine download utilities
packages/get-platformPlatform detection for engine binaries
packages/json-protocolJSON protocol types for engine communication

Database Connectivity

PackagePurpose
packages/driver-adapter-utilsShared interfaces for driver adapters (SqlDriverAdapter, SqlQueryable)
packages/adapter-pgPostgreSQL driver adapter (node-postgres)
packages/adapter-neonNeon serverless PostgreSQL adapter
packages/adapter-planetscalePlanetScale serverless MySQL adapter
packages/adapter-libsqllibSQL/Turso adapter
packages/adapter-d1Cloudflare D1 adapter
packages/adapter-better-sqlite3Better-sqlite3 adapter
packages/adapter-mariadbMariaDB adapter
packages/adapter-mssqlSQL Server adapter
packages/bundled-js-driversBundled JS database drivers

Configuration & Utilities

PackagePurpose
packages/configPrisma config loader & types (prisma.config.ts)
packages/internalsShared CLI/internal utilities (schema parsing, etc.)
packages/schema-files-loaderSchema file loading utilities
packages/client-commonCommon client utilities
packages/client-runtime-utilsClient runtime helper functions

Extensions & Integrations

PackagePurpose
packages/sqlcommenterBase SQL commenter plugin types & interfaces
packages/sqlcommenter-query-tagsAdd custom query tags to SQL comments
packages/sqlcommenter-trace-contextW3C Trace Context integration
packages/sqlcommenter-query-insightsQuery shape insights for monitoring
packages/query-plan-executorStandalone executor for Prisma Accelerate
packages/instrumentationTelemetry & tracing instrumentation
packages/instrumentation-contractInstrumentation interfaces

Testing & Development

PackagePurpose
packages/integration-testsEnd-to-end test suites across providers
packages/type-benchmark-testsTypeScript performance benchmarks
packages/bundle-sizeBundle size tracking & tests

Tooling

PackagePurpose
packages/generatorBase generator types
packages/generator-helperGenerator development utilities
packages/debugDebug utilities for Prisma
packages/credentials-storeSecure credential storage
packages/nextjs-monorepo-workaround-pluginNext.js monorepo compatibility plugin

Architecture Diagrams

Generating Graphs

To generate or update architecture diagrams, first install GraphViz:

  • Windows: choco install graphviz or download from graphviz.org
  • macOS: brew install graphviz
  • Linux: apt-get install graphviz or yum install graphviz

Package Dependency Graphs

Generate dependency graphs showing relationships between packages:

bash
pnpm ts-node scripts/graph-dependencies.ts

This creates:

  • ./graphs/dependencies.png - Runtime dependencies
  • ./graphs/devDependencies.png - Development dependencies
  • ./graphs/peerDependencies.png - Peer dependencies

Repository Layout Diagram

Generate high-level repository structure visualization:

bash
cd graphs
dot -Tpng repo-layout.dot -o repo-layout.png

Or from repo root:

bash
dot -Tpng ./graphs/repo-layout.dot -o ./graphs/repo-layout.png

Visual Architecture

Dependencies

Dev Dependencies

Peer Dependencies

Generators

See Prisma Generators

The DMMF, or Data Model Meta Format

What the ... is DMMF? It's the Datamodel Meta Format. It is an AST (abstract syntax tree) of the datamodel in the form of JSON.
The whole Prisma Client is just generated based on the DMMF, which comes from the Rust engines. Note: the datamodel is contained in the Prisma schema file, along the datasource and generators blocks.

⚠️ Note: The DMMF is a Prisma ORM internal API with no guarantees for stability to outside users. We might - and do - change the DMMF in potentially breaking ways between minor versions. 🐲

Upgrading and debugging

<!-- TODO -->

Oftentimes, the Rust team did a change in DMMF, which you now need to integrate. How to do that?
The first step is to identify, which new @prisma/engines version you want to use.
Either have a look in the Versions tab in https://www.npmjs.com/package/@prisma/engines or check out npm info @prisma/engines in your terminal.
Let's say you determined, that you want to upgrade to 2.20.0-14.f461292a2242db52d9f4c87995f0237aacd300d2. To upgrade your local workspace, run this command to upgrade both @prisma/engines and @prisma/engines-version:

bash
pnpm update -r @prisma/[email protected] @prisma/engines-version@2.20.0-14.f461292a2242db52d9f4c87995f0237aacd300d2

In the ./packages/client dir, now open sandbox/dmmf.ts in your VSCode editor. Either run ndb in your terminal to debug the file: ndb -r ts-node/register ./sandbox/dmmf.ts Or

  1. Open .vscode/launch.json.default and save it as .vscode/launch.json.
  2. Then click on the debug icon in VSCode:
  3. Then select in the dropdown of possible runner options Client - Current TS File
  4. Then just press the green play button
  5. You should now be able to go through the DMMF and have a look at the json structure

You can always check out the test of our "not-so-exhaustive-schema", where we test the fully generated client, which depends on the dmmf:

bash
pnpm run test exhaustive

Usually, dmmf changes are also visible in the tests of the @prisma/internals package:

bash
cd ./packages/internal
pnpm run test

If there is a change in the snapshots, only accept them if you're 100% certain, that these changes are expected.
If not, please always ping the Rust team, if this is an intended change.