DB class

Database manager for Flint Dart

  • Automatically connects on first use (lazy init).
  • Reads connection info from .env (DB_CONNECTION, DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD, DB_SECURE).
  • Supports MySQL & PostgreSQL with query normalization.
  • Provides retry logic if the database is not reachable.

Example:

final users = await DB.query("SELECT * FROM users WHERE id = ?", positionalParams: [1]);
final lastId = await DB.getLastInsertId("users", "id");

Constructors

DB.new()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

driver DBDriver
Current driver in use.
no setter
instance → DBWrapper
Returns the active connection (MySQL or PostgreSQL).
no setter
isConnected bool
Returns whether the database is currently connected.
no setter
mysql → MySqlConnectionWrapper
no setter
pg → PgConnectionWrapper
no setter

Static Methods

autoConnect() Future<void>
Connect to the database using environment variables.
buildLimitClause(int limit, [int? offset]) String
Build a LIMIT/OFFSET clause.
close() Future<void>
Close active connection.
execute(String sql, {List? positionalParams, Map<String, dynamic>? namedParams}) Future<void>
Execute a command (INSERT, UPDATE, DELETE).
getLastInsertId(String tableName, String primaryKey) Future
Get the last inserted ID (DB-specific).
normalizeQuery(String sql, {List? positionalParams, Map<String, dynamic>? namedParams}) → (String, List)
Normalize SQL and parameters for the active driver.
query(String sql, {List? positionalParams, Map<String, dynamic>? namedParams}) Future<List<Map>>
Execute a query with parameters.
tableExists(String tableName) Future<bool>
Check if a table exists.
tryAutoConnect({int retries = 5, int delaySeconds = 3}) Future<void>
Attempt auto-connect with retries.