How simolus3/drift works
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.dart drift · driver.dart drift_dev · executor.dart Executor · main.dart Examples app
Components 21
Core packages
Primary layered stack from SQL parsing to code generation to runtime execution.
- sqlparser · Parses SQL and drift files, builds ASTs, performs semantic analysis, type inference, schema inspection, and autocomplete support.
sqlparser/lib/sqlparser.dart - drift_dev · Compile-time analysis and code generation for tables, queries, views, migrations, and schema artifacts.
drift_dev/lib/src/analysis/driver/driver.dart - Writers · Produces generated Dart sources for databases, tables, queries, result sets, managers, and schema versions.
drift_dev/lib/src/writer/writer.dart - CLI/tools · Command-line and build-system entrypoints for analysis, schema export, and migration workflows.
drift_dev/lib/src/cli/cli.dart - Schema svc · Schema diffing, verification, SQLite-to-drift conversion, and stream update rule discovery.
drift_dev/lib/src/services/schema/find_differences.dart - drift · Backend-agnostic runtime library exposing the query builder, DAOs, transactions, streams, managers, and executor APIs.
drift/lib/drift.dart - Query builder · Runtime SQL construction layer with expressions, statements, schema entities, and query generation.
drift/lib/src/runtime/query_builder/query_builder.dart - Runtime 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 · Executes queries, manages transactions and connection pooling, and coordinates reactive stream updates.
drift/lib/src/runtime/executor/executor.dart - Manager · 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 · Protocol and client/server implementation for remote database connections and web transport.
drift/lib/src/remote/protocol.dart - Web runtime · Web-specific database setup, wasm integration, storage, and worker/channel implementations.
drift/lib/src/web/wasm_setup/shared.dart - Native runtime · Native SQLite-backed execution paths, isolate support, and platform entrypoints.
drift/lib/native.dart - drift_flutter · Flutter-facing adapter package that connects the runtime to platform-specific database setup.
drift_flutter/lib/drift_flutter.dart - drift_sqflite · 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 · Generated/static documentation application with pages, navigation, search, snippets, and API guides.
docs/lib/main.server.dart - Examples app · Main Flutter sample app demonstrating database setup, generated code, screens, and platform-specific entrypoints.
examples/app/lib/main.dart - Web worker ex · Example of web/WASM execution using a worker and generated database code.
examples/web_worker_example/web/main.dart - Modular ex · 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 · DevTools extension for inspecting live databases, schema state, and remote database details.
extras/drift_devtools_extension/lib/main.dart - drift_postgres · PostgreSQL-oriented extension package with dialect and type support built around the drift runtime concepts.
extras/drift_postgres/lib/drift_postgres.dart
Connections 26
- 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.