# simolus3/drift architecture

> Architecture diagram of the GitHub repository simolus3/drift, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/simolus3/drift
- Repository: https://github.com/simolus3/drift

## Overview

1. This is a Dart/Flutter monorepo organized with Melos. The top-level packages form a layered stack: `sqlparser` at the bottom, `drift_dev` as the compiler/analyzer, and `drift` as the runtime API. Around those are platform adapters, docs, examples, and experimental/future packages.

2. The core architecture is split between compile time and runtime. At compile time, app code and `.drift` files are analyzed by `drift_dev`, which uses `sqlparser` plus the Dart analyzer to understand tables, queries, views, migrations, and type mappings. It then generates Dart sources such as `.g.dart` files, schema snapshots, DAO/accessor classes, and migration helpers.

3. `drift` is the runtime layer consumed by applications. It exposes the query builder, table/DAO abstractions, transactions, streams, managers, and executor/connection APIs. Generated code from `drift_dev` plugs directly into this runtime, so the main data flow is: schema/query definitions -> generated code -> runtime execution against a database backend.

4. The runtime is backend-agnostic but has concrete execution paths for different environments. There are native, isolate, remote, and web-specific modules in `drift/lib/src`, plus platform entrypoints in `drift/lib/native.dart`, `drift/lib/web.dart`, and `drift/lib/remote.dart`. This boundary is important: the query API stays stable while database connectivity changes underneath.

5. A major subsystem is reactive change propagation. The executor layer tracks writes and query subscriptions, and stream-query components turn SQL queries into auto-updating streams. That means the read path is not just “execute query” but “execute query, subscribe to update rules, and emit changes when underlying tables change.”

6. Code generation is a first-class architectural boundary. `drift_dev` contains writers for tables, queries, result sets, schema versions, and managers, as well as build-system integrations. The generator output is meant to be checked in or produced during build, and many examples include both handwritten sources and generated artifacts side by side.

7. SQL understanding is centralized in `sqlparser`. It provides the parser, AST, semantic analysis, type inference, schema inspection, autocomplete engine, and SQLite feature/module support. `drift_dev` depends on this package for validation and generation, while `sqlparser` itself can also be used standalone.

8. The repository supports multiple database deployment modes. The main `drift` package targets SQLite, but there are additional integration packages and examples for Flutter/native, web/WASM, remote worker setups, encryption, and postgreSQL-like extensions in `extras`. This makes platform selection an architectural boundary rather than an application detail.

9. `drift_flutter` and `drift_sqflite` are adapter packages that bridge the core runtime to Flutter-specific storage backends. They sit between application code and the low-level database engine, letting apps swap implementations without changing the query layer.

10. The docs site is its own application under `docs/`. It contains a generated/static site with reusable code snippets, search indexing, platform-specific guides, and rendered API docs. Its architecture mirrors the library stack: it imports examples/snippets, builds documentation pages, and serves search/navigation assets.

11. The examples directory is not just sample code; it demonstrates the intended integration patterns. There are separate app topologies for single-package Flutter apps, web worker setups, migrations, modular multi-package layouts, and a server/client split. These examples are useful reference diagrams for how the packages are expected to be composed.

12. `extras/` contains supporting products and experiments that extend the ecosystem without being part of the core runtime. Notable ones are the DevTools extension for inspecting live databases, the PostgreSQL/MariaDB experiments, encryption samples, and integration test harnesses. They usually depend on the core packages rather than the other way around.

13. Testing is distributed by package and by concern. The core runtime has unit, integration, and platform tests; `drift_dev` heavily tests analysis, resolution, generation, and CLI behavior; `sqlparser` tests parsing, analysis, and autocomplete. This reflects the architectural separation between language analysis, code generation, and execution.

14. For a diagram, the key end-to-end flow is: source schema/query definitions in app or package code -> `drift_dev`/`sqlparser` analysis -> generated Dart artifacts -> `drift` runtime API -> backend adapter / SQLite engine / worker / remote connection -> reactive results back to the app. The main boundaries are compile time vs runtime, analyzer vs generator, and backend-agnostic API vs platform-specific storage.

Main entry points: [`drift/lib/drift.dart`](https://github.com/simolus3/drift/blob/develop/drift/lib/drift.dart) (drift), [`drift_dev/lib/src/analysis/driver/driver.dart`](https://github.com/simolus3/drift/blob/develop/drift_dev/lib/src/analysis/driver/driver.dart) (drift_dev), [`drift/lib/src/runtime/executor/executor.dart`](https://github.com/simolus3/drift/blob/develop/drift/lib/src/runtime/executor/executor.dart) (Executor), [`examples/app/lib/main.dart`](https://github.com/simolus3/drift/blob/develop/examples/app/lib/main.dart) (Examples app)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core packages"]
  node_sqlparser[("sqlparser<br/>SQL parser/analyzer<br/>[sqlparser.dart]")]
  node_drift_dev["drift_dev<br/>Generator/analyzer<br/>[driver.dart]"]
  node_drift_dev_writers["Writers<br/>codegen emitters<br/>[writer.dart]"]
  node_drift_dev_cli["CLI/tools<br/>build + cli<br/>[cli.dart]"]
  node_drift_dev_schema["Schema svc<br/>schema services"]
  node_drift[("drift<br/>runtime API<br/>[drift.dart]")]
  node_query_builder["Query builder<br/>dsl + SQL compiler<br/>[query_builder.dart]"]
  node_runtime_api["Runtime API<br/>db API<br/>[runtime_api.dart]"]
  node_executor["Executor<br/>execution engine<br/>[executor.dart]"]
  node_manager["Manager<br/>relational API<br/>[manager.dart]"]
end

subgraph group_platform["Platform adapters"]
  node_remote["Remote<br/>client/server bridge<br/>[protocol.dart]"]
  node_web_runtime["Web runtime<br/>web backend<br/>[shared.dart]"]
  node_native_runtime["Native runtime<br/>native backend<br/>[native.dart]"]
  node_drift_flutter["drift_flutter<br/>flutter adapter<br/>[drift_flutter.dart]"]
  node_drift_sqflite["drift_sqflite<br/>sqflite adapter<br/>[drift_sqflite.dart]"]
end

subgraph group_apps["Apps and docs"]
  node_docs_site["Docs site<br/>documentation app<br/>[main.server.dart]"]
  node_examples_app["Examples app<br/>flutter example<br/>[main.dart]"]
  node_examples_web_worker["Web worker ex<br/>web example<br/>[main.dart]"]
  node_examples_modular["Modular ex<br/>[database.dart]"]
end

subgraph group_extras["Extras"]
  node_extras_devtools["DevTools ext<br/>inspector app<br/>[main.dart]"]
  node_extras_postgres["drift_postgres<br/>postgres adapter"]
end

node_sqlparser -->|"analyzes SQL"| node_drift_dev
node_sqlparser -->|"powers docs"| node_docs_site
node_drift_dev -->|"emits code"| node_drift_dev_writers
node_drift_dev -->|"exposes tooling"| node_drift_dev_cli
node_drift_dev -->|"verifies schema"| node_drift_dev_schema
node_drift_dev_writers -->|"generates for"| node_drift
node_drift_dev_schema -->|"migration artifacts"| node_drift
node_drift -->|"builds SQL"| node_query_builder
node_drift -->|"exports API"| node_runtime_api
node_drift -->|"executes through"| node_executor
node_drift -->|"offers relations"| node_manager
node_executor -->|"remote transport"| node_remote
node_executor -->|"web backend"| node_web_runtime
node_executor -->|"native backend"| node_native_runtime
node_drift_flutter -->|"connects to"| node_native_runtime
node_drift_flutter -->|"connects to"| node_web_runtime
node_drift_sqflite -->|"adapts runtime"| node_drift
node_docs_site -->|"documents runtime"| node_drift
node_examples_app -->|"uses generation"| node_drift_dev
node_examples_app -->|"uses runtime"| node_drift
node_examples_app -->|"platform setup"| node_drift_flutter
node_examples_web_worker -->|"uses wasm"| node_web_runtime
node_examples_modular -->|"modular generation"| node_drift_dev
node_extras_devtools -->|"inspects live db"| node_drift
node_extras_postgres -->|"extends runtime"| node_drift
node_extras_postgres -->|"reuses SQL model"| node_sqlparser

click node_sqlparser "https://github.com/simolus3/drift/blob/develop/sqlparser/lib/sqlparser.dart"
click node_drift_dev "https://github.com/simolus3/drift/blob/develop/drift_dev/lib/src/analysis/driver/driver.dart"
click node_drift_dev_writers "https://github.com/simolus3/drift/blob/develop/drift_dev/lib/src/writer/writer.dart"
click node_drift_dev_cli "https://github.com/simolus3/drift/blob/develop/drift_dev/lib/src/cli/cli.dart"
click node_drift_dev_schema "https://github.com/simolus3/drift/blob/develop/drift_dev/lib/src/services/schema/find_differences.dart"
click node_drift "https://github.com/simolus3/drift/blob/develop/drift/lib/drift.dart"
click node_query_builder "https://github.com/simolus3/drift/blob/develop/drift/lib/src/runtime/query_builder/query_builder.dart"
click node_runtime_api "https://github.com/simolus3/drift/blob/develop/drift/lib/src/runtime/api/runtime_api.dart"
click node_executor "https://github.com/simolus3/drift/blob/develop/drift/lib/src/runtime/executor/executor.dart"
click node_manager "https://github.com/simolus3/drift/blob/develop/drift/lib/src/runtime/manager/manager.dart"
click node_remote "https://github.com/simolus3/drift/blob/develop/drift/lib/src/remote/protocol.dart"
click node_web_runtime "https://github.com/simolus3/drift/blob/develop/drift/lib/src/web/wasm_setup/shared.dart"
click node_native_runtime "https://github.com/simolus3/drift/blob/develop/drift/lib/native.dart"
click node_drift_flutter "https://github.com/simolus3/drift/blob/develop/drift_flutter/lib/drift_flutter.dart"
click node_drift_sqflite "https://github.com/simolus3/drift/blob/develop/drift_sqflite/lib/drift_sqflite.dart"
click node_docs_site "https://github.com/simolus3/drift/blob/develop/docs/lib/main.server.dart"
click node_examples_app "https://github.com/simolus3/drift/blob/develop/examples/app/lib/main.dart"
click node_examples_web_worker "https://github.com/simolus3/drift/blob/develop/examples/web_worker_example/web/main.dart"
click node_examples_modular "https://github.com/simolus3/drift/blob/develop/examples/modular/lib/database.dart"
click node_extras_devtools "https://github.com/simolus3/drift/blob/develop/extras/drift_devtools_extension/lib/main.dart"
click node_extras_postgres "https://github.com/simolus3/drift/blob/develop/extras/drift_postgres/lib/drift_postgres.dart"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sqlparser,node_drift_dev,node_drift_dev_writers,node_drift_dev_cli,node_drift_dev_schema,node_drift,node_query_builder,node_runtime_api,node_executor,node_manager toneBlue
class node_remote,node_web_runtime,node_native_runtime,node_drift_flutter,node_drift_sqflite toneAmber
class node_docs_site,node_examples_app,node_examples_web_worker,node_examples_modular toneMint
class node_extras_devtools,node_extras_postgres toneRose
```

## Components

### Core packages

Primary layered stack from SQL parsing to code generation to runtime execution.

- [sqlparser](https://github.com/simolus3/drift/blob/develop/sqlparser/lib/sqlparser.dart): SQL parser/analyzer · Parses SQL and drift files, builds ASTs, performs semantic analysis, type inference, schema inspection, and autocomplete support. · `sqlparser/lib/sqlparser.dart`
- [drift_dev](https://github.com/simolus3/drift/blob/develop/drift_dev/lib/src/analysis/driver/driver.dart): Generator/analyzer · Compile-time analysis and code generation for tables, queries, views, migrations, and schema artifacts. · `drift_dev/lib/src/analysis/driver/driver.dart`
- [Writers](https://github.com/simolus3/drift/blob/develop/drift_dev/lib/src/writer/writer.dart): codegen emitters · Produces generated Dart sources for databases, tables, queries, result sets, managers, and schema versions. · `drift_dev/lib/src/writer/writer.dart`
- [CLI/tools](https://github.com/simolus3/drift/blob/develop/drift_dev/lib/src/cli/cli.dart): build + cli · Command-line and build-system entrypoints for analysis, schema export, and migration workflows. · `drift_dev/lib/src/cli/cli.dart`
- [Schema svc](https://github.com/simolus3/drift/blob/develop/drift_dev/lib/src/services/schema/find_differences.dart): schema services · Schema diffing, verification, SQLite-to-drift conversion, and stream update rule discovery. · `drift_dev/lib/src/services/schema/find_differences.dart`
- [drift](https://github.com/simolus3/drift/blob/develop/drift/lib/drift.dart): runtime API · Backend-agnostic runtime library exposing the query builder, DAOs, transactions, streams, managers, and executor APIs. · `drift/lib/drift.dart`
- [Query builder](https://github.com/simolus3/drift/blob/develop/drift/lib/src/runtime/query_builder/query_builder.dart): dsl + SQL compiler · Runtime SQL construction layer with expressions, statements, schema entities, and query generation. · `drift/lib/src/runtime/query_builder/query_builder.dart`
- [Runtime API](https://github.com/simolus3/drift/blob/develop/drift/lib/src/runtime/api/runtime_api.dart): db API · Public database, connection, DAO, batch, and stream-update abstractions used by generated code and applications. · `drift/lib/src/runtime/api/runtime_api.dart`
- [Executor](https://github.com/simolus3/drift/blob/develop/drift/lib/src/runtime/executor/executor.dart): execution engine · Executes queries, manages transactions and connection pooling, and coordinates reactive stream updates. · `drift/lib/src/runtime/executor/executor.dart`
- [Manager](https://github.com/simolus3/drift/blob/develop/drift/lib/src/runtime/manager/manager.dart): relational API · High-level relational access layer for joins, filters, ordering, and computed fields. · `drift/lib/src/runtime/manager/manager.dart`

### Platform adapters

Backend and environment-specific bridges around the runtime API.

- [Remote](https://github.com/simolus3/drift/blob/develop/drift/lib/src/remote/protocol.dart): client/server bridge · Protocol and client/server implementation for remote database connections and web transport. · `drift/lib/src/remote/protocol.dart`
- [Web runtime](https://github.com/simolus3/drift/blob/develop/drift/lib/src/web/wasm_setup/shared.dart): web backend · Web-specific database setup, wasm integration, storage, and worker/channel implementations. · `drift/lib/src/web/wasm_setup/shared.dart`
- [Native runtime](https://github.com/simolus3/drift/blob/develop/drift/lib/native.dart): native backend · Native SQLite-backed execution paths, isolate support, and platform entrypoints. · `drift/lib/native.dart`
- [drift_flutter](https://github.com/simolus3/drift/blob/develop/drift_flutter/lib/drift_flutter.dart): flutter adapter · Flutter-facing adapter package that connects the runtime to platform-specific database setup. · `drift_flutter/lib/drift_flutter.dart`
- [drift_sqflite](https://github.com/simolus3/drift/blob/develop/drift_sqflite/lib/drift_sqflite.dart): sqflite adapter · Adapter package bridging Drift to the sqflite storage backend for Flutter apps. · `drift_sqflite/lib/drift_sqflite.dart`

### Apps and docs

Example applications and the documentation site built on top of the core packages.

- [Docs site](https://github.com/simolus3/drift/blob/develop/docs/lib/main.server.dart): documentation app · Generated/static documentation application with pages, navigation, search, snippets, and API guides. · `docs/lib/main.server.dart`
- [Examples app](https://github.com/simolus3/drift/blob/develop/examples/app/lib/main.dart): flutter example · Main Flutter sample app demonstrating database setup, generated code, screens, and platform-specific entrypoints. · `examples/app/lib/main.dart`
- [Web worker ex](https://github.com/simolus3/drift/blob/develop/examples/web_worker_example/web/main.dart): web example · Example of web/WASM execution using a worker and generated database code. · `examples/web_worker_example/web/main.dart`
- [Modular ex](https://github.com/simolus3/drift/blob/develop/examples/modular/lib/database.dart): modular example · Split-package example showing generated drift files and reusable schema/query modules. · `examples/modular/lib/database.dart`

### Extras

Supporting packages, experiments, and integration harnesses.

- [DevTools ext](https://github.com/simolus3/drift/blob/develop/extras/drift_devtools_extension/lib/main.dart): inspector app · DevTools extension for inspecting live databases, schema state, and remote database details. · `extras/drift_devtools_extension/lib/main.dart`
- [drift_postgres](https://github.com/simolus3/drift/blob/develop/extras/drift_postgres/lib/drift_postgres.dart): postgres adapter · PostgreSQL-oriented extension package with dialect and type support built around the drift runtime concepts. · `extras/drift_postgres/lib/drift_postgres.dart`

## Connections

- sqlparser → drift_dev: analyzes SQL · drift_dev relies on sqlparser for parsing, semantic checks, and schema understanding.
- sqlparser → Docs site: powers docs · Documentation snippets and guides rely on the language and SQL model defined by sqlparser.
- drift_dev → Writers: emits code · The analyzer feeds generation of Dart databases, tables, queries, and schema helpers.
- drift_dev → CLI/tools: exposes tooling · Command-line and build integrations are frontends around the analysis engine.
- drift_dev → Schema svc: verifies schema · Schema comparison, conversion, and migration verification are separate services inside drift_dev.
- Writers → drift: generates for · Generated sources are designed to compile against the runtime API.
- Schema svc → drift: migration artifacts · Schema snapshots and migration helpers connect generated models back to runtime execution.
- drift → Query builder: builds SQL · The public runtime API is implemented on top of the query builder and DSL layer.
- drift → Runtime API: exports API · Top-level package exposes runtime abstractions for apps and generated code.
- drift → Executor: executes through · Database operations flow into the executor, which handles connections and queries.
- drift → Manager: offers relations · Manager APIs provide higher-level relational querying on top of the runtime.
- Executor → Remote: remote transport · Execution can be routed through the remote protocol and client/server implementations.
- Executor → Web runtime: web backend · On web, the executor is backed by wasm, worker, and storage setup.
- Executor → Native runtime: native backend · On native platforms, the executor uses sqlite-backed and isolate-capable paths.
- drift_flutter → Native runtime: connects to · Flutter adapter packages connect app code to the native runtime backend.
- drift_flutter → Web runtime: connects to · Flutter adapter packages also route to the web runtime when appropriate.
- drift_sqflite → drift: adapts runtime · Sqflite adapter provides a concrete storage backend under the shared runtime API.
- Docs site → drift: documents runtime · Docs pages and snippets are built around the drift public API and usage patterns.
- Examples app → drift_dev: uses generation · The main example contains handwritten sources plus generated drift artifacts.
- Examples app → drift: uses runtime · Sample app code runs queries and transactions through the core runtime package.
- Examples app → drift_flutter: platform setup · Flutter example demonstrates environment-specific database setup through the adapter.
- Web worker ex → Web runtime: uses wasm · Web worker example demonstrates the web/WASM execution path.
- Modular ex → drift_dev: modular generation · Modular example shows split schema files and generated artifacts across packages.
- DevTools ext → drift: inspects live db · DevTools extension connects to the running drift database runtime.
- drift_postgres → drift: extends runtime · Postgres package mirrors the drift model while targeting a different backend.
- drift_postgres → sqlparser: reuses SQL model · Postgres support depends on the shared SQL understanding and expression model.
