basalt library Getting Started
Basalt — a type-safe query builder and ORM for Dart, inspired by basalt.
Stage 1 surface: typed schema (TableColumn/TableRef), expressions, the
select*/insertInto/update/deleteFrom builders, SQL serialization, and
the dialect-agnostic Connection interface. Concrete backends live in
separate packages (e.g. basalt_sqlite).
Classes
- Agg Annotations & Codegen
-
Binds an aggregate field to a private static select tear-off that returns
the SQL projection (e.g.
@Agg(CategoryRevenueRow._revenue)). -
Aggregate<
T> Schema & Columns -
An aggregate over a column (or
COUNT(*)), usable inselect(...)and readable from a row via its readKey. Build with TableColumn.count, countAll, or the numeric aggregates (IntColumnAggregates). - AsChangeset Annotations & Codegen
-
Marks a data class for UPDATE generation against table
(e.g.
@AsChangeset(Users.table)). The generator emits atoUpdate()extension method returning anUpdateStatementwhoseSETclause is built from each writable field; the caller appends the.where(...)(typically on the primary key).@Column(readOnly: true)fields are skipped. - BlobSqlType Types & Codecs
- BooleanSqlType Types & Codecs
-
Encodes a canonical Dart
bool; each dialect'sencodeParammaps it to the driver form (SQLite:bool->int). decode is lenient so it reads back either representation. - Column Annotations & Codegen
- Maps a constructor field to a schema column and/or tunes its read/write direction. Without it, the field name selects the column by matching the generated schema's camelCase accessor.
-
ColumnValue<
Tbl> Schema & Columns -
A column-scoped assignment (
column = value) for INSERT/UPDATE. The value is already encoded;Tblkeeps it bound to its table. - Connection Connection & Backends
-
Database-agnostic execution surface. The query builder produces statements;
a
Connectionimplementation serializes and runs them against a driver. - DateTimeSqlType Types & Codecs
- Stored as epoch milliseconds (sortable and timezone-free).
-
DeleteStatement<
Tbl> Insert, Update & Delete -
DELETE FROM table WHERE .... - DoubleSqlType Types & Codecs
-
Expression<
T, Tbl> Predicates & Expressions - A typed SQL expression.
-
FoldMappedQuery<
R> Query Builder - A JOIN Query whose SQL rows are folded into fewer parents via folder.
- ForeignKey Connection & Backends
- A foreign-key target discovered during introspection.
- HasMany Annotations & Codegen
-
Marks a one-to-many relation: column is the child's foreign key pointing at
this row's primary key (e.g.
@HasMany(Addresses.customerId)on a customer view). The field must beList<ChildRow>whereChildRowis@Queryable. - Insertable Annotations & Codegen
-
Marks a data class for INSERT generation against table
(e.g.
@Insertable(Users.table)). The generator emits twotoInsert()extension methods returning anInsertStatement, mapping each writable field (everything except@Column(readOnly: true)) throughTableColumn.set: one on the class itself (single-row insert) and one onIterableof it, which batches every element into a single multi-rowINSERT ... VALUES (...), (...). -
InsertStatement<
Tbl> Insert, Update & Delete -
INSERT INTO table (...) VALUES (...). - IntrospectedColumn Connection & Backends
- IntrospectedTable Connection & Backends
- IntSqlType Types & Codecs
-
MappedQuery<
R> Query Builder - A Query finished with a decoder — the executable SelectQuery.
-
NullableSqlType<
T extends Object> Types & Codecs -
Nullable variant of any SqlType:
nullpasses through in both directions, non-null values delegate to inner. -
OnConflict<
Tbl> Insert, Update & Delete -
Fluent builder for an
ON CONFLICTclause (from InsertStatement.onConflict). - Ordering Query Builder
-
One
ORDER BYterm. -
PrimaryKey<
T, Tbl> Schema & Columns - A primary-key column.
- Projection
-
One item in a SELECT projection: an expression plus an optional
ASalias. Built from aSelectionso the serializer stays purely AST-level. -
Query<
Scope> Query Builder - Immutable SELECT builder.
- Queryable Annotations & Codegen
-
Marks a data class for query generation against table
(e.g.
@Queryable(Posts.table)). The generator emits aThisClassQuerycompanion that is the canonical query, carrying astatic fromRowreader (oneRowReader.getper mapped field) and itsRowMapper. - QueryBuilder SQL Serialization
-
Walks an untyped AST and emits
(sql, params)for a given SqlDialect. -
QuerySource<
Tbl> -
Something a query can read FROM or JOIN: a real table (TableRef) or an
aliased one (TableAlias).
columnsare bound to the source's effective name (alias ?? table), so reads/predicates address the right instance. -
RawSelection<
T> Schema & Columns -
A raw, typed SQL selection (escape hatch): emitted verbatim in the projection
and read back by its readKey (the raw
asalias). Uses?placeholders. -
Ref<
T, Tbl, Target> Schema & Columns -
A foreign-key column on
Tblthat references the PrimaryKey ofTarget. Referencing the PK column object (a leaf) keeps it const-cycle free even for mutual foreign keys, and the sharedTenforces matching key types. - Relation Annotations & Codegen
-
Marks a relation field filled by joining the table referenced by FK column
and nesting the related object via its own generated reader, e.g.
@Relation(Posts.authorId) final User? author;. The field MUST be a nullable, optional (named) parameter whose type is another@Queryableclass. - Returning Insert, Update & Delete
- Intermediate builder from WriteReturning.returning; call map / mapWith to attach a row decoder and produce an executable ReturningQuery.
-
ReturningQuery<
R> Insert, Update & Delete -
A write statement finished with a
RETURNINGprojection and a row decoder — the executable analog ofMappedQueryfor INSERT/UPDATE/DELETE. -
RowMapper<
R> Query Builder -
A reusable, codegen-friendly row decoder for a data class.
@Queryable(Users)emits one of these (a singlereadbuilt fromRowReader.getcalls). They compose freely — aCommentreader can call aPostreader on the same RowReader to nest objects, with no arity-specific machinery. - RowReader Query Builder
-
Reads typed values out of one result row, addressed by selection (a
column or an aggregate), not by position. This is what makes a single
mapdecoder work for any number of columns/tables:r.get(Users.name)returns aStringregardless of wherenamesits in the projection, joins stay unambiguous (each column is keyed bytable.name), and aggregates are keyed by their alias. -
Selection<
T> -
Something selectable in a query's projection — a TableColumn or an
Aggregate. Carries the SQL selectExpression to emit, an optional
ASselectAlias, the readKey aRowReaderuses to find the value, and the SqlType used to decode it. -
SelectQuery<
R> -
The shape the serializer and
Connectionconsume. Implemented by the terminal MappedQuery and FoldMappedQuery; Query is the builder that produces them. - SerialLock Connection & Backends
- A minimal FIFO async mutex: serializes the run callbacks handed to it so that at most one is in flight at a time, in submission order.
- SqlDialect SQL Serialization
-
SQL-dialect differences the serializer must account for. Keeping this behind
an interface is the seam that lets each backend (SQLite, Postgres, ...) plug
in its own quoting and placeholder style without touching the query builder.
Concrete implementations live in the backend packages (e.g.
basalt_sqlite). -
SqlType<
T> Types & Codecs - StringSqlType Types & Codecs
-
TableAlias<
Tbl> Schema & Columns -
An aliased table for self-joins (the same table joined more than once).
Columns are rebound to the alias, so
sender.col(Users.id)serializes as"sender"."id"and is distinct fromrecipient.col(Users.id). -
TableColumn<
T, Tbl> Schema & Columns -
A typed column belonging to table
Tbl. -
TableRef<
Tbl> Schema & Columns -
Table descriptor: its name and full column list (the default projection for
from/joins). Cycle-safe even with foreign keys because Ref points at a PrimaryKey leaf, not back at aTableRef. -
UpdateAllStatement<
Tbl> Insert, Update & Delete -
Batch
UPDATE: one statement that updates many rows with per-row values, joining the target table against aVALUEStable on the key column(s): -
UpdateStatement<
Tbl> Insert, Update & Delete -
UPDATE table SET ... WHERE .... -
ValueColumn<
T, Tbl> Schema & Columns - An ordinary value column.
- WriteStatement Insert, Update & Delete
- Statements that mutate rows and return an affected-row count rather than a result set. Sealed so the serializer can exhaustively switch over them.
Enums
- ColumnType
-
Canonical column type — the backend normalizes its native type into one of
these, and codegen maps it to a Dart type +
SqlType.
Extensions
-
BoolExpression
on Expression<
bool, Tbl> -
Boolean combinators are only meaningful on predicates, so they live on an
extension over
Expression<bool, Tbl>rather than the general class. -
DoubleColumnAggregates
on TableColumn<
double, Tbl> - Numeric aggregates for double columns.
-
FoldMappedQueryExecute
on FoldMappedQuery<
R> Query Builder - Terminal helpers for FoldMappedQuery — one SQL round-trip, fold in Dart.
-
IntColumnAggregates
on TableColumn<
int, Tbl> - Numeric aggregates for integer columns. SQLite returns NULL over an empty set, so these decode to nullable Dart types.
-
MappedQueryExecute
on MappedQuery<
R> Query Builder -
basalt-style terminal helpers so a finished query reads like
query.load(db)/query.first(db)/query.optional(db). -
NumColumnArithmetic
on TableColumn<
num, Tbl> - Typed arithmetic on numeric columns and expressions.
-
NumExpressionArithmetic
on Expression<
num, Tbl> - Typed arithmetic that chains on the result of a previous numeric expression.
-
QueryMapFold
on Query<
Object?> -
Attach a row folder after JOINs — use the
FoldMappedQueryExecute.loadextension to run. -
TextColumn
on TableColumn<
String, Tbl> -
LIKEonly makes sense for text columns. - WriteReturning on WriteStatement
-
Attaches a
RETURNINGclause to any write statement.
Functions
-
avg(
Object operand, {String? as}) → Aggregate< double?> -
AVGover a numeric column or expression — always decodes toAggregate<double?>. Alias defaults toavg_$columnfor columns. -
countAll(
) → Aggregate< int> -
COUNT(*)— total row count. -
countDistinct(
TableColumn< Object?, Object?> column, {String? as}) → Aggregate<int> -
COUNT(DISTINCT col). Alias defaults tocount_$column. -
deleteFrom<
Tbl> (TableRef< Tbl> table) → DeleteStatement<Tbl> -
from<
Tbl> (QuerySource< Tbl> source) → Query<Tbl> -
Start a query from
source(a table or an alias). Single-table scope keepswherestrictly typed. -
insertInto<
Tbl> (TableRef< Tbl> table) → InsertStatement<Tbl> -
loadGroupedByFk<
Tbl, K, C> (Connection db, QuerySource< Query BuilderTbl> childSource, TableColumn<K, Tbl> fk, List<K> parentKeys, C readChild(RowReader reader)) → Future<Map< K, List< >C> > -
Loads the children whose foreign key
fkis one ofparentKeys, grouped by that key — thebelonging_to(parents).grouped_by(parents)pattern in a single query (avoids N+1). Every key inparentKeysis present in the result, mapping to an empty list when it has no children. -
max<
T extends num> (Object operand, {String? as}) → Aggregate< T?> -
MAXover a numeric column or expression — typed like sum (integer columns decode toAggregate<int?>). -
min<
T extends num> (Object operand, {String? as}) → Aggregate< T?> -
MINover a numeric column or expression — typed like sum (integer columns decode toAggregate<int?>). -
raw<
T> (String sql, SqlType< T> type, {required String as, List<Object?> params = const []}) → Selection<T> -
A raw typed SQL selection for
select([...]), read back viar.get(...). Write valid SQL yourself (qualify columns); bind values with?+params. -
rawCondition(
String sql, {List< Object?> params = const []}) → Expression<bool, Object?> -
A raw boolean SQL fragment for
having(and joinedwhere/filter) — uses the relaxedObject?scope;?placeholders bindparamsin order. -
sum<
T extends num> (Object operand, {String? as}) → Aggregate< T?> -
SUMover a numeric column or expression. -
update<
Tbl> (TableRef< Tbl> table) → UpdateStatement<Tbl> -
updateAll<
Tbl> (TableRef< Insert, Update & DeleteTbl> table) → UpdateAllStatement<Tbl> -
Starts a batch
UPDATEoftable— see UpdateAllStatement.