Get the FREE Ultimate OpenClaw Setup Guide →

dart-drift

Scanned
npx machina-cli add skill MADTeacher/mad-agents-skills/dart-drift --openclaw
Files (1)
SKILL.md
7.4 KB

Dart Drift

Comprehensive guide for using drift database library in Dart applications.

Overview

Dart Drift skill provides complete guidance for implementing persistent storage in Dart applications (CLI tools, backend services, non-Flutter desktop apps) using the drift library. Drift is a reactive persistence library for Dart built on SQLite, with optional PostgreSQL support, offering type-safe queries, auto-updating streams, schema migrations, and cross-platform database connections.

Quick Start

SQLite Setup

Add dependencies to pubspec.yaml:

dependencies:
  drift: ^2.30.0
  sqlite3: ^3.1.3

dev_dependencies:
  drift_dev: ^2.30.0
  build_runner: ^2.10.4

Define database:

@DriftDatabase(tables: [TodoItems])
class AppDatabase extends _$AppDatabase {
  AppDatabase(QueryExecutor e) : super(e);

  @override
  int get schemaVersion => 1;
}

Open database:

AppDatabase openConnection() {
  final file = File('db.sqlite');
  return AppDatabase(LazyDatabase(() async {
    final db = sqlite3.open(file.path);
    return NativeDatabase.createInBackground(db);
  }));
}

Run code generator:

dart run build_runner build

PostgreSQL Setup

Add PostgreSQL dependencies:

dependencies:
  drift: ^2.30.0
  postgres: ^3.5.9
  drift_postgres: ^1.3.1

dev_dependencies:
  drift_dev: ^2.30.0
  build_runner: ^2.10.4

Configure for PostgreSQL in build.yaml:

targets:
  $default:
    builders:
      drift_dev:
        options:
          sql:
            dialects:
              - postgres

Open PostgreSQL connection:

import 'package:drift_postgres/drift_postgres.dart';

AppDatabase openPostgresConnection() {
  final endpoint = HostEndpoint(
      host: 'localhost',
      port: 5432,
      database: 'mydb',
      username: 'user',
      password: 'password',
    );

  return AppDatabase(
    PgDatabase(
      endpoint: endpoint,
    ),
  );
}

Reference Files

See detailed documentation for each topic:

Common Patterns

CLI Application with SQLite

void main(List<String> args) async {
  final db = openConnection();

  final todos = await db.select(db.todoItems).get();
  print('Found ${todos.length} todos');

  await db.close();
}

Backend Service with PostgreSQL

class TodoService {
  final AppDatabase db;

  TodoService(this.db);

  Future<List<TodoItem>> getAllTodos() async {
    return await db.select(db.todoItems).get();
  }

  Future<int> createTodo(String title) async {
    return await db.into(db.todoItems).insert(
      TodoItemsCompanion.insert(title: title),
    );
  }
}

void main() async {
  final pool = PgPool(
      PgEndpoint(
        host: 'localhost',
        port: 5432,
        database: 'mydb',
        username: 'user',
        password: 'password',
      ),
      settings: PoolSettings(maxSize: 10),
    );

  final db = AppDatabase(PgDatabase.opened(pool));
  final service = TodoService(db);

  final todoId = await service.createTodo('New task');
  print('Created todo with id: $todoId');

  final todos = await service.getAllTodos();
  print('Total todos: ${todos.length}');
}

Connection Pooling

import 'package:postgres/postgres_pool.dart';

Future<AppDatabase> openPooledConnection() {
  final pool = PgPool(
      PgEndpoint(
        host: 'localhost',
        port: 5432,
        database: 'mydb',
        username: 'user',
        password: 'password',
      ),
      settings: PoolSettings(maxSize: 20),
    );

  return AppDatabase(PgDatabase.opened(pool));
}

PostgreSQL-Specific Types

class Users extends Table {
  late final id = postgresUuid().autoGenerate()();
  late final name = text()();
  late final settings = postgresJson()();
  late final createdAt = dateTime().withDefault(
    FunctionCallExpression.currentTimestamp(),
  );
}

In-Memory Testing

AppDatabase createTestDatabase() {
  return AppDatabase(NativeDatabase.memory());
}

Transaction with Data Consistency

Future<void> transferTodo(int fromId, int toId) async {
  await db.transaction(() async {
    final fromTodo = await (db.select(db.todoItems)
      ..where((t) => t.id.equals(fromId))
      ).getSingle();

    await db.update(db.todoItems).write(
      TodoItemsCompanion(
        id: Value(toId),
        title: Value(fromTodo.title),
      ),
    );

    await db.delete(db.todoItems).go(fromId);
  });
}

Platform-Specific Setup

CLI/Desktop (macOS/Windows/Linux)

Uses sqlite3 package with file-based storage.

Server/Backend (PostgreSQL)

Uses postgres package with connection pooling.

Testing

Uses in-memory database for fast unit tests.

Testing

Unit Tests

void main() {
  test('Insert and retrieve todo', () async {
    final db = createTestDatabase();
    final id = await db.into(db.todoItems).insert(
      TodoItemsCompanion.insert(title: 'Test todo'),
    );

    final todos = await db.select(db.todoItems).get();
    expect(todos.length, 1);
    expect(todos.first.title, 'Test todo');

    await db.close();
  });
}

Integration Tests

void main() {
  test('PostgreSQL connection works', () async {
    final pool = PgPool(endpoint, settings: PoolSettings(maxSize: 5));
    final db = AppDatabase(PgDatabase.opened(pool));

    final id = await db.into(db.todoItems).insert(
      TodoItemsCompanion.insert(title: 'Test'),
    );

    expect(id, greaterThan(0));

    await db.close();
  });
}

Best Practices

  1. Connection pooling for PostgreSQL in production
  2. In-memory databases for fast unit tests
  3. Transactions for data consistency
  4. Connection timeouts for robust server apps
  5. Schema migrations with proper versioning
  6. Indexes on frequently queried columns
  7. Prepared statements (automatic in drift)
  8. Close connections properly on shutdown
  9. Pool management for backend services
  10. Error handling for connection failures

Troubleshooting

Build Fails

dart run build_runner clean
dart run build_runner build --delete-conflicting-outputs

Migration Errors

dart run drift_dev schema validate
dart run drift_dev make-migrations

Connection Pool Exhausted

Increase pool size or reduce connection lifetime:

PoolSettings(
    maxSize: 20,
    maxLifetime: Duration(minutes: 5),
  )

PostgreSQL Type Errors

Verify dialect is configured in build.yaml.

Source

git clone https://github.com/MADTeacher/mad-agents-skills/blob/main/dart-drift/SKILL.mdView on GitHub

Overview

Dart Drift is a reactive persistence library for non-Flutter Dart apps (CLI, server-side, desktop) that provides local SQLite storage or PostgreSQL connections with type-safe queries, live streams, migrations, and efficient CRUD. It supports setup with sqlite3 and drift_postgres, plus optional connection pooling for server workloads.

How This Skill Works

Drift generates a typed database layer from your tables and queries. For SQLite, it uses a LazyDatabase with sqlite3-backed NativeDatabase; for PostgreSQL, it leverages drift_postgres with PgDatabase and optional PgPool. It offers auto-updating streams for reactive queries and migrations through a schemaVersion and migration tooling.

When to Use It

  • Building a CLI tool that needs local SQLite storage with reliable CRUD operations
  • Creating a backend service that connects to PostgreSQL using type-safe queries and connection pooling
  • Developing a non-Flutter desktop app that requires cross-platform database access
  • Implementing reactive data flows via streaming queries for live updates
  • Managing schema migrations alongside robust CRUD in server-side patterns

Quick Start

  1. Step 1: Add dependencies (drift, sqlite3) and dev_dependencies (drift_dev, build_runner) in pubspec.yaml
  2. Step 2: Define AppDatabase extends DriftDatabase and declare schemaVersion; create table definitions
  3. Step 3: Open a connection (SQLite via LazyDatabase/NativeDatabase or PostgreSQL via drift_postgres) and run code generation with 'dart run build_runner build'

Best Practices

  • Define a stable schemaVersion and manage migrations with drift_dev tooling
  • Use PgPool with PoolSettings(maxSize: ...) for concurrent PostgreSQL access
  • Keep your database access code in a dedicated layer and leverage drift’s generated code
  • Prefer reactive streaming queries for long-running processes or UI updates
  • Document and version-control your setup (sqlite3 or drift_postgres) and references

Example Use Cases

  • CLI Application with SQLite: openConnection(), run selects, and print results
  • Backend Service with PostgreSQL: TodoService using getAllTodos() and createTodo()
  • PostgreSQL connection pooling: PgPool with HostEndpoint and PoolSettings in production
  • Server-side patterns: AppDatabase(PgDatabase.opened(pool)) to share a single DB instance
  • Migration-focused workflow: setup and references to references/migrations.md for schema updates

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers