bloom_db library
First-party database and ORM layer for the Bloom framework.
Provides Django-inspired declarative models, strongly typed querysets with lazy query compilation, expression filters (Q, QF, F), and unified database execution across PostgreSQL and SQLite.
Core Subsystems
- Declarative Models & Schema Metadata: Define entity classes annotated with
@BloomModeland@BloomField, carrying runtime schema descriptors via ModelMeta, FieldMeta, RelationMeta, and IndexMeta. - Lazy Query Compilation (QuerySet): Construct queries immutably with
.filter(),.exclude(),.orderBy(),.limit(), and.offset(). Compile queries to dialect-specific SQL only upon terminal execution (.all(),.get(),.first(),.count(),.exists(),.update(),.delete(),.getOrCreate(),.values(),.valuesList()). - Expressive Filtering & Database Arithmetic (Q, QF, F): Compose boolean logic
using bitwise operators (
&,|,~), lookup suffixes (__gte,__icontains,__in,__isnull), field-to-field comparisons (QF), and in-database column arithmetic updates (F). - Unified Execution Engine (DbExecutor): Run queries seamlessly against SqliteDbExecutor
(in-memory or on-disk via
package:sqlite3) or PostgresDbExecutor (viapackage:postgres), with automatic parameter binding and result set mapping to DbRow or model instances. - Multi-Dialect SQL Generators (Dialect): Abstract SQL syntax variances such as
placeholders (
$1vs?), case-insensitive matching (ILIKEvsLIKE), auto-incrementing primary keys (BIGSERIALvsINTEGER PRIMARY KEY AUTOINCREMENT), and type casting.
Example: Defining Models and Querying
import 'package:bloom_db/bloom_db.dart';
// 1. Instantiate an in-memory SQLite executor (or PostgresDbExecutor.connect)
final db = SqliteDbExecutor.inMemory();
// 2. Execute DDL migrations or raw SQL
await db.execute('''
CREATE TABLE "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"age" INTEGER NOT NULL DEFAULT 0,
"is_active" INTEGER NOT NULL DEFAULT 1
);
''');
// 3. Build queries with Q expressions and execute
final adults = await QuerySet<User>(
meta: User.meta,
fromRow: User.fromRow,
)
.filter(Q('age__gte', 18) & Q('is_active', true))
.orderBy('-age')
.limit(10)
.all(db);
Classes
- BloomAndExpr
-
Resolved conjunction of multiple expressions combined with
AND. - BloomBoolValue
- Boolean value expression wrapper.
- BloomCompareExpr
- Resolved comparison expression between a database column field and a literal value.
- BloomCompareFieldExpr
- Resolved column-to-column comparison on the same database row.
- BloomDateTimeValue
- UTC timestamp value expression wrapper.
- BloomExpr
-
Resolved boolean expression tree for query
WHEREclauses. - BloomF64Value
- 64-bit floating point value expression wrapper.
- BloomField
- Field-level annotation defining database column properties and constraints.
- BloomI64Value
- 64-bit integer value expression wrapper.
- BloomListValue
-
List of expression values wrapper for
IN (...)queries. - BloomModel
- Class-level annotation marking a Dart class as a Bloom ORM Model entity.
- BloomNotExpr
-
Resolved negation of an expression (
NOT). - BloomNullValue
-
Database
NULLvalue expression wrapper. - BloomOrExpr
-
Resolved disjunction of multiple expressions combined with
OR. - BloomTextValue
- Text string value expression wrapper.
- BloomValue
- A dynamic, strongly-typed SQL value container used in ORM expressions, query filters, and parameters.
- DbExecutor
- Unified database execution interface across SQL backends.
- DbRow
- Database row abstraction representing a single row returned by a query.
- DecimalFieldKind
- Fixed-precision decimal field kind with precision total digits and scale decimal places.
- DefaultValue
- Represents the default schema value of a model field when omitted during insertion.
- Dialect
- Abstract SQL dialect handler encapsulating syntax and type differences across database engines.
- F
-
Django's
F()expression — a reference to a field's existing value in the database. - FieldKind
- Represents database column types supported by the Bloom ORM.
- FieldMeta
- Runtime schema metadata for a single model field or database column.
- FieldOpSetExpr
-
In-database field arithmetic update operation (e.g.
col = col + 1). - IndexMeta
- Metadata describing an explicit database table index.
- LiteralSetExpr
-
Literal value assignment in an
UPDATEquery (col = $1). - MapDbRow
- A generic map-backed DbRow implementation.
- Model
- Base abstract class for all Bloom ORM entity models.
- ModelMeta
- Runtime schema and mapping metadata for a database model entity.
- PostgresDbExecutor
-
PostgreSQL database executor implementation wrapping
package:postgres. - PostgresDialect
- PostgreSQL database dialect implementation.
-
QuerySet<
T extends Model> -
Immutable, lazily evaluated database query builder for model entity
T. - RelationMeta
- Metadata describing a relationship between two model entities.
- SetExpr
-
Expression specifying a value update in an
UPDATEquery. - SqliteDbExecutor
-
SQLite database executor implementation wrapping
package:sqlite3. - SqliteDialect
- SQLite database dialect implementation.
- UnresolvedAll
-
Unresolved conjunction of sub-expressions combined with
AND. - UnresolvedAny
-
Unresolved disjunction of sub-expressions combined with
OR. - UnresolvedCompare
-
Unresolved comparison node representing
field = valueorfield__lookup = value. - UnresolvedExpr
- Unresolved filter expression AST before model metadata validation and column resolution.
- UnresolvedFieldCompare
- Unresolved column-to-column comparison node on the same row.
- UnresolvedNegate
-
Unresolved negation of a sub-expression (
NOT).
Enums
- ArithOp
- Arithmetic operators for in-database UPDATE set expressions.
- CompareOp
- Comparison operators for query filtering expressions.
- DialectType
- Enumeration of supported SQL database dialects.
- OnDelete
- Action to take on related rows when a referenced primary key object is deleted.
- RelationKind
- The cardinality and kind of relation between models.
Constants
- idField → const BloomField
- Convenience constant for auto-incrementing 64-bit integer primary keys.
- kMaxQueryLogEntries → const int
- Maximum retained SQL statements per executor. Query logging is useful for diagnostics but must not become an unbounded memory sink in long-lived servers.
Functions
-
Q(
String field, dynamic value) → UnresolvedExpr -
Helper function to construct an UnresolvedExpr filter node — Django's
Q()object. -
QF(
String leftField, String rightField) → UnresolvedExpr -
Helper function to construct column-to-column comparisons on the same database row — Django's
q_f!. -
splitFieldLookup(
String s) → (String, String) -
Splits a field lookup string (e.g.
"age__gte") into field name"age"and suffix"gte". -
suffixToOp(
String suffix) → CompareOp -
Converts a lookup suffix string (e.g.
'gte','icontains','in') into the corresponding CompareOp.
Typedefs
-
ModelFromRow<
T> = T Function(DbRow row) -
Type signature for a factory function instantiating a strongly typed model
Tfrom a DbRow.
Exceptions / Errors
- BloomOrmException
- Base exception class for all errors and exceptions raised by Bloom ORM operations.
- BloomOrmFieldNotFoundError
- Exception thrown when referencing a field or column name that does not exist in model metadata.
- BloomOrmInvalidQueryError
- Exception thrown when a queryset is configured in an invalid state that cannot generate valid SQL.
- BloomOrmMultipleObjectsReturnedError
-
Exception thrown when a single-object query (
QuerySet.get()) matches more than one record. - BloomOrmNotFoundError
-
Exception thrown when a single-object query (such as
QuerySet.get()orDbExecutor.fetchOne()) finds zero matching database records. - BloomOrmQueryException
- Exception thrown when underlying SQL query execution fails at the driver or database level.
- BloomOrmSelectForUpdateOutsideTransactionError
-
Exception thrown when
select_for_updaterow locking is attempted outside of an active database transaction. - BloomOrmUnsupportedOnDialectError
- Exception thrown when attempting an operation not supported by the active database dialect.