saropa_drift_advisor library
Debug-only HTTP server that exposes SQLite/Drift table data as JSON and a minimal web viewer.
Architecture: The package is
dependency-injection friendly: it does not depend
on drift. You supply a DriftDebugQuery that
runs SQL; the server uses it for table listing,
table data, schema, and optional read-only SQL
runner. One server per process;
DriftDebugServer.stop clears state so
DriftDebugServer.start can be called again
(e.g. in tests).
Use from any Flutter or Dart app that has a SQLite (or Drift) database. Add the package (from pub.dev or a path dependency), then start the server with DriftDebugServer.start, passing a DriftDebugQuery callback that runs SQL and returns rows as maps.
Public API
- DriftDebugServer — Static API: DriftDebugServer.start to run the server, DriftDebugServer.stop to shut it down, DriftDebugServer.port to read the bound port.
- DriftDebugQuery — Callback that executes
a SQL string and returns rows as
List<Map<String, dynamic>>. Pass as thequeryargument to DriftDebugServer.start. - DriftDebugOnLog — Optional callback for
log messages (e.g. startup banner). Pass as
onLogto DriftDebugServer.start. - DriftDebugOnError — Optional callback for
errors and stack traces. Pass as
onErrorto DriftDebugServer.start. - DriftDebugOnClassifiedError — Like
DriftDebugOnError but includes a
DriftDebugErrorKind so the host can route
user-input errors differently from server bugs.
Pass as
onClassifiedError; when set,onErroris not called. - DriftDebugGetDatabaseBytes — Optional
callback that returns the raw SQLite file bytes.
Pass as
getDatabaseBytesto enable "Download database" in the UI. - DriftDebugErrorLogger — Helpers for DriftDebugOnLog and DriftDebugOnError: DriftDebugErrorLogger.logCallback, DriftDebugErrorLogger.errorCallback, and DriftDebugErrorLogger.callbacks for a single prefix.
See the package README for HTTP endpoints, UI features, and optional auth for dev tunnels.
Classes
- DeclaredColumn
- One column in a host-declared Drift schema (the schema as written in code, independent of the live SQLite file). Served by GET /api/schema/declared so the web viewer can show code-declared tables and (stretch) flag divergence from the runtime database.
- DeclaredRelationship
-
One declared relationship edge:
fromTable.fromColumnreferencestoTable.toColumn. Descriptive only — the advisor reads it to know the link exists; it never enforces it (noPRAGMA foreign_keys, no constraint, no migration). This is how a host that links by convention (a shared UUID/_idcolumn) instead of SQLite foreign keys makes its relationships visible to tooling, turning the web wizard's column-name heuristic into a fact. - DeclaredTable
-
One table in a host-declared Drift schema, with its columns and any declared
index names.
indexesmay be empty when the host cannot supply them. - DriftDebugErrorLogger
- Best-practice error and message logger for the Drift debug server.
Enums
- DriftDebugErrorKind
- Classifies errors the server reports via the error callback so host apps can route user-input mistakes differently from genuine server failures.
Mixins
- DriftDebugServer
- Debug-only HTTP server (stub when dart:io unavailable).
Extensions
- StartDriftViewerExtension on Object
-
Convenience API for Drift apps:
await myDb.startDriftViewer(...).
Typedefs
-
DeclaredRelationships
= List<
DeclaredRelationship> - The full host-declared relationship manifest: an ordered list of edges.
- DeclaredRelationshipsCallback = DeclaredRelationships Function()
-
Host-supplied callback returning the relationship manifest. When null the
GET /api/schema/relationships endpoint reports
available: falseand the metadata fold falls back toPRAGMA foreign_key_list(same opt-in posture as DeclaredSchemaCallback). -
DeclaredSchema
= List<
DeclaredTable> - The full code-declared schema: an ordered list of tables.
- DeclaredSchemaCallback = DeclaredSchema Function()
-
Host-supplied callback returning the code-declared Drift schema. When null
the GET /api/schema/declared endpoint reports the feature as unavailable
(same opt-in posture as
declaredTableNamesfor the orphan check). -
DriftDebugGetDatabaseBytes
= Future<
List< Function()int> > - Optional callback that returns the raw SQLite database file bytes.
- DriftDebugLoggerCallbacks = ({_ErrorCallback error, _LogCallback log})
- Type for the pair of log and error callbacks returned by DriftDebugErrorLogger.callbacks.
- DriftDebugOnClassifiedError = void Function(Object error, StackTrace stack, DriftDebugErrorKind kind)
- Like DriftDebugOnError but includes an error classification so the host can route user-input mistakes (warning log) differently from server bugs (halt / crash report). When both DriftDebugOnError and this callback are set, only this one fires.
- DriftDebugOnError = void Function(Object error, StackTrace stack)
- Optional callback for errors (and optional stack trace).
- DriftDebugOnLog = void Function(String message)
- Optional callback for log messages.
-
DriftDebugQuery
= Future<
List< Function(String sql)Map< >String, dynamic> > - Callback that runs a single SQL query and returns rows as list of maps.
-
DriftDebugQueryWithBindings
= Future<
List< Function(String sql, {Map<Map< >String, dynamic> >String, Object?> ? namedArgs, List<Object?> ? positionalArgs}) - Optional read callback that receives declared positional/named bindings.
- DriftDebugReportActivity = void Function(String message)
-
Shape of the host-statement reporting hook for the Heartbeat screen's
live capture (Feature 80, phase 2). The host wires drift's
logStatementsoutput or aQueryInterceptortoDriftDebugServer.reportActivity(which matches this signature). Not wiring it is fine — the heartbeat screen then degrades gracefully to phase 1 signals (advisor traffic + detected row-count changes). -
DriftDebugWriteQuery
= Future<
void> Function(String sql) - Optional callback for write queries (INSERT/UPDATE/DELETE). Debug-only: used exclusively by the import endpoint.
-
DriftDebugWriteQueryWithBindings
= Future<
void> Function(String sql, {Map<String, Object?> ? namedArgs, List<Object?> ? positionalArgs}) - Optional write callback that receives declared positional/named bindings.