Skip to main content
Driver adapters bridge the gap between Rust-compiled query plans and database-specific TypeScript drivers. They enable Prisma to run in any JavaScript environment with appropriate database connectivity.

Overview

The driver adapter system allows the Query Compiler to target different databases and environments:
  • PostgreSQL: pg, @neondatabase/serverless, @prisma/adapter-pg
  • MySQL: mysql2, @planetscale/database, @prisma/adapter-mysql
  • SQLite: better-sqlite3, @libsql/client, Cloudflare D1
  • SQL Server: tedious, @prisma/adapter-mssql

Architecture

The Rust side provides WASM bindings and connection info abstraction:
use quaint::connector::ConnectionInfo;
use query_compiler::compile;

pub fn compile(
    query_schema: &QuerySchema,
    query: Operation,
    connection_info: &ConnectionInfo,
) -> Result<Expression, CompileError>
Connection info tells the compiler which SQL dialect to generate without requiring actual database access.

Driver Adapter Manager

The DriverAdaptersManager interface standardizes driver setup:
export interface DriverAdaptersManager {
  /**
   * Access the Driver Adapter factory
   */
  factory: () => SqlDriverAdapterFactory

  /**
   * Creates a queryable instance from the Driver Adapter factory,
   * attempting a connection to the database.
   */
  connect: () => Promise<SqlDriverAdapter>

  /**
   * Closes the connection to the database and cleans up any used resources.
   */
  teardown: () => Promise<void>

  /**
   * Returns the connector used by the Manager.
   */
  connector: () => Env['CONNECTOR']
}
From libs/driver-adapters/executor/src/driver-adapters-manager/index.ts

Example: PostgreSQL Adapter

Here’s how the pg adapter is implemented:
import { PrismaPg } from '@prisma/adapter-pg'
import type {
  SqlMigrationAwareDriverAdapterFactory,
  SqlDriverAdapter,
} from '@prisma/driver-adapter-utils'
import { postgresSchemaName, postgresOptions } from '../utils.js'
import type { DriverAdaptersManager } from './index.js'

export class PgManager implements DriverAdaptersManager {
  #factory: SqlMigrationAwareDriverAdapterFactory
  #adapter?: SqlDriverAdapter

  private constructor(
    private env: EnvForAdapter<'pg'>,
    { url }: SetupDriverAdaptersInput,
  ) {
    const schemaName = postgresSchemaName(url)
    this.#factory = new PrismaPg(postgresOptions(url), {
      schema: schemaName,
    })
  }

  static async setup(env: EnvForAdapter<'pg'>, input: SetupDriverAdaptersInput) {
    return new PgManager(env, input)
  }

  factory() {
    return this.#factory
  }

  async connect() {
    return (this.#adapter ??= await this.#factory.connect())
  }

  async teardown() {
    await this.#adapter?.dispose()
  }

  connector(): Env['CONNECTOR'] {
    // could be 'postgresql' or 'cockroachdb'
    return this.env.CONNECTOR
  }
}
From libs/driver-adapters/executor/src/driver-adapters-manager/pg.ts

Supported Adapters

PostgreSQL

  • @prisma/adapter-pg (node-postgres)
  • @prisma/adapter-neon (Neon serverless)
  • @prisma/adapter-pg-worker (Cloudflare Workers)

MySQL

  • @prisma/adapter-mysql (mysql2)
  • @prisma/adapter-planetscale (PlanetScale serverless)
  • @prisma/adapter-mariadb

SQLite

  • @prisma/adapter-better-sqlite3
  • @prisma/adapter-libsql (Turso)
  • @prisma/adapter-d1 (Cloudflare D1)

SQL Server

  • @prisma/adapter-mssql (tedious)

Building the Driver Adapters Kit

The driver adapters are built separately from the query compiler:
1

Ensure Prisma repository is available

The build process requires the main Prisma repository for adapter packages:
make ensure-prisma-present
This clones prisma/prisma to ../prisma if not already present.
2

Install dependencies

make install-driver-adapters-kit-deps
Runs pnpm i in the driver adapters directory.
3

Build for Query Compiler

make build-driver-adapters-kit-qc
This runs pnpm build:qc which builds only the packages needed for query compiler tests.
From the Makefile:
install-driver-adapters-kit-deps: build-driver-adapters
	cd libs/driver-adapters && pnpm i

build-driver-adapters-kit-qc: install-driver-adapters-kit-deps
	cd libs/driver-adapters && pnpm build:qc

build-driver-adapters: ensure-prisma-present
	@echo "Building driver adapters..."
	@cd ../prisma && pnpm i
	@echo "Driver adapters build completed.";

Testing with Driver Adapters

The connector test kit can run against any driver adapter:
make dev-pg-qc
cargo test -p query-engine-tests
This:
  1. Starts PostgreSQL via Docker
  2. Builds query-compiler-wasm
  3. Builds driver adapters kit
  4. Sets up test config for pg adapter

Connection Info Types

The WASM compiler needs to know the provider without having a real connection:
export type AdapterProvider =
  | 'postgresql'
  | 'mysql'
  | 'sqlite'
  | 'sqlserver'
  | 'cockroachdb'

export interface JsConnectionInfo {
  supports_relation_joins: boolean
  // Additional provider-specific options
}
In the WASM constructor:
pub struct QueryCompilerParams {
    datamodel: String,
    provider: AdapterProvider,
    connection_info: JsConnectionInfo,
}

#[wasm_bindgen(constructor)]
pub fn new(params: QueryCompilerParams) -> Result<QueryCompiler, JsError> {
    let schema = Arc::new(psl::parse_without_validation(
        datamodel.into(),
        CONNECTOR_REGISTRY,
        &NoExtensionTypes,
    ));
    
    Ok(Self {
        schema,
        connection_info: ConnectionInfo::External(
            connection_info.into_external_connection_info(provider)
        ),
        protocol: EngineProtocol::Json,
    })
}
From query-compiler-wasm/src/compiler.rs

Expression Execution Flow

When the TypeScript interpreter executes a plan:

Relation Join Strategies

Driver adapters support different relation loading strategies:
Load relations in separate queries (application-level joins):
PRISMA_RELATION_LOAD_STRATEGY=query make dev-pg-qc
Generates a Join expression in the plan.

Migration Script Support

Some adapters (like Cloudflare D1) support migration scripts:
export type SetupDriverAdaptersInput = {
  url: string
  
  /**
   * The `prisma migrate diff --script` output to apply migrations.
   * This is a temporary workaround only used by Cloudflare D1.
   */
  migrationScript?: string
}

Benchmarking

Benchmark driver adapter performance:
make bench-pg-js
This:
  1. Sets up a PostgreSQL instance via Docker
  2. Runs JavaScript benchmarks comparing adapter performance

Next Steps

Query Planning

Learn how queries are planned and optimized

WASM Build

Build the WebAssembly module

Build docs developers (and LLMs) love