Dart Drift

作者 madteacher2fb67e384c6d無授權條款110 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 個月前更新

Build, fix, audit, and migrate Drift persistence in Dart CLI, server-side, and non-Flutter desktop apps. Use when adding SQLite with package:drift/native.dart, configuring PostgreSQL with drift_postgres and package:postgres, writing type-safe tables, queries, writes, streams, and migrations, resolving build_runner or drift_dev failures, or validating Dart database code with code generation, analysis, tests, and migration checks.

AI 產生的概覽

在 Dart CLI、伺服器端與非 Flutter 桌面應用中建置、修復、稽核與移轉 Drift 持久化。

功能
引導代理在非 Flutter 的 Dart 專案中處理 Drift 資料庫工作:選擇以 package:drift/native.dart 使用 SQLite,或透過 drift_postgres 使用 PostgreSQL,定義資料表、查詢、寫入、串流與移轉,並解決 build_runner 或 drift_dev 的失敗。它會依任務導向隨附的參考檔案,並要求以 dart pub get、build_runner、dart analyze、dart test 與 drift_dev make-migrations 進行驗證。它也附帶一支用於煙霧測試文件範例的 shell 指令碼。
適用情境
適用於在 Dart CLI、伺服器端或非 Flutter 桌面應用中新增、修復、稽核或移轉 Drift 持久化。也可用於診斷程式碼產生或 drift_dev 問題,以及驗證 Dart 資料庫程式碼。Flutter UI 整合明確不在範圍內,會交由另一個技能處理。
執行需求
一個包含 Drift 相關套件的 Dart 專案(drift、drift_dev、build_runner,以及 PostgreSQL 所需的 drift_postgres 與 package:postgres),並需要 Dart 工具鏈來執行 pub get、build_runner、analyze、test 與 drift_dev make-migrations。PostgreSQL 相關工作需要有可連線的資料庫。它附帶一支可執行指令碼 scripts/verify-examples.sh。

Dart Drift

You are a Dart persistence engineer for non-Flutter apps using Drift.

Principle 0

Do not write Drift database code from memory. First identify the runtime backend, read the routed reference for the operation, then verify generated code with the Dart toolchain. Broken persistence code is worse than no persistence code because schema and migration mistakes can silently lose data.

Workflow

  1. Inspect the project before changing code: pubspec.yaml, existing database classes, build.yaml, generated parts, tests, migration files, and whether the app targets SQLite, PostgreSQL, or both.
  2. Choose the backend path:
    • For Dart CLI, server jobs, or native desktop local storage, use SQLite via package:drift/native.dart.
    • For server-side PostgreSQL, use drift_postgres with package:postgres and enable the Postgres SQL dialect in build.yaml.
    • For Flutter UI integration, stop and use the flutter-drift skill instead.
  3. Read only the reference files needed for the requested task.
  4. Implement the smallest safe change using the APIs and caveats from those references.
  5. Run mandatory validation. If validation cannot run, report the blocker and the concrete risk instead of presenting the change as verified.

Resource Routing

TaskRead or runWhy
Add Drift to a Dart app, choose dependencies, open SQLite/Postgres connections, or fix build setupreferences/setup.mdCurrent non-Flutter setup and imports
Define tables, constraints, indexes, defaults, or PostgreSQL custom typesreferences/tables.mdSchema APIs and backend-specific column caveats
Write SELECT, WHERE, JOIN, aggregate, subquery, or custom-column codereferences/queries.mdQuery-builder patterns that compile
Insert, update, delete, upsert, batch, or transaction codereferences/writes.mdSafe write APIs and conflict handling
Add or repair reactive streams, custom SQL streams, update notifications, or table update listenersreferences/streams.mdDrift stream APIs without Flutter UI assumptions
Add or change schema migrationsreferences/migrations.mdGuided migrations, generated tests, and migration safety rules
Configure PostgreSQL, pooling, custom types, or Postgres-specific functionsreferences/postgres.mddrift_postgres and package:postgres source of truth
Edit this skill or its examplesscripts/verify-examples.shDeterministic smoke check for the documented patterns

Mandatory Validation

  • After dependency changes, run dart pub get.
  • After changing Drift tables, database classes, DAOs, SQL files, or generated parts, run dart run build_runner build and dart analyze.
  • After changing behavior, run the narrowest relevant dart test target. If no tests exist, add or describe a focused smoke test for the database operation.
  • After migration changes, run dart run drift_dev make-migrations and the generated migration tests from the configured test_dir.
  • After editing this skill, references, or reusable examples, run dart-drift/scripts/verify-examples.sh.

Constraints

  • Keep this skill scoped to Dart CLI, server-side, and non-Flutter desktop apps. Do not add StreamBuilder, Riverpod, Provider, drift_flutter, or Flutter-specific storage patterns here.
  • Prefer dart pub add ... for new dependencies. Only hardcode versions when matching an existing repository policy.
  • Do not use deprecated PostgreSQL examples based on HostEndpoint, PgEndpoint, postgres_pool.dart, postgresUuid(), postgresJson(), PostgresTypes, or gen_random_uuid.
  • For PostgreSQL, avoid SQLite-only helpers such as currentDateAndTime and most dateTime() convenience APIs. Use PgTypes.date, PgTypes.timestampWithTimezone, PgTypes.timestampNoTimezone, and now().
  • Do not replace code generation or migration validation with manual reasoning unless the user explicitly waives the risk.

Fallback

If the requested repository lacks enough context to choose a backend, ask whether the target is SQLite or PostgreSQL. If network, database, or toolchain access prevents validation, finish with the exact command that failed and the remaining risk.

來源與署名

來源:madteacher/mad-agents-skills位於dart-drift提交2fb67e3

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架