Mool
Mool, pronounced mool, means root or source.
Mool is a source-first typed SQL data mapper for Rust.
It gives SQLx projects typed model metadata, row mapping, query handles, relations, filters, schema metadata, migrations, test mocks, and enum mapping without turning records into active-record objects. SQL remains visible at the call site; Mool just makes the shape typed.
Why Mool?
Use Mool when plain SQLx starts repeating the same database plumbing:
- derive table metadata once, then reuse typed columns everywhere
- compose selects, writes, filters, joins, subqueries, and CTEs with checked column/value types
- scan rows into models, projections, joined records, and write payloads
- keep SQL rendering explicit enough to inspect, test, and reason about
- share one session abstraction across pools, transactions, raw SQL, and mocks
- keep schema/migration metadata next to Rust models when that is useful
Mool is best described as a typed SQL data mapper. It is ORM-like, but not an active-record ORM: records do not save themselves, no runtime identity map owns your data, and queries are still written as explicit operations.
Why Not Plain SQL?
Plain SQL is still the right tool for one-off queries, highly tuned hand-written SQL, or database-specific statements that should stay exact.
Mool earns its keep when the same tables appear across many query paths. It removes stringly-typed column names, repeated bind ordering, manual row scanning, ad hoc filter builders, and test-only database setup while keeping escape hatches:
query
.bind
.
.await?;
Install
Enable exactly one backend feature for real use:
= { = "0.1", = ["postgres"] }
# or
= { = "0.1", = ["sqlite"] }
# or
= { = "0.1", = ["mysql"] }
Optional features:
= { = "0.1", = ["postgres", "migrations"] }
= { = "0.1", = ["sqlite", "migrations"] }
= { = "0.1", = ["sqlite", "mock"] }
Backend features are mutually exclusive. Do not verify with --all-features.
Migrations are supported for Postgres and SQLite3, not MySQL. Mock support is
available in Mool debug/test builds and behind mock for downstream release
builds.
Quick Example
use mool as db;
async
Core Pieces
| Area | What it gives you |
|---|---|
Model |
Table-backed rows, typed table handles, columns, primary keys, schema metadata. |
Record |
Projections, patches, joined records, raw write payloads, and scan metadata. |
SqlEnum |
Rust enum to SQL label/code/native type mapping. |
Filterable |
Request/search structs converted into typed predicates. |
| Queries | select, writes, subqueries, CTEs, variables, functions, aggregates, windows. |
| Relations | Joined records, explicit references, backrefs, many-to-many predicates, prefetch. |
| Sessions | One execution shape for pools, transactions, raw SQL, and mocks. |
| Migrations | Gaman schema/migration re-exports plus Mool registries. |
Crates:
mool: runtime crate for applicationsmool-macros: derive macros re-exported bymoolmool-macros-impl: internal macro implementation
Models And Records
Use Model for table-backed rows:
Use Record for projections, patches, joined output, and write-only rows:
Queries
Queries start from a source and finish with a terminal:
let posts = table;
let author_id = .named;
let rows = from
.filter
.filter
.bind
.
.exec
.await?;
Terminals:
- reads:
all,first,one,slice,count,exists,scalar - writes:
insert,batch_insert,update,delete,upsert,batch_upsert,returning - derived sources:
subquery,cte
Writes
let posts = table;
let id = .named;
from
.insert
.exec
.await?;
from
.filter
.bind
.update
.exec
.await?;
SQL Enums
SqlEnum maps fieldless Rust enums to database values.
Storage modes:
| Storage | Backends | Notes |
|---|---|---|
text |
Postgres, SQLite3, MySQL | Default. Stores labels and emits check metadata. |
int |
Postgres, SQLite3, MySQL | Requires explicit codes and repr = "i16", "i32", or "i64". |
native_postgres |
Postgres | Registers native enum schema metadata. |
native_mysql |
MySQL | Emits ENUM(...) column metadata. MySQL migrations are not managed. |
Generated helpers:
SQL_NAME;
SQL_VALUES;
SQL_STORAGE;
Published.as_sql_str;
try_from_sql_str?;
Filters
Filterable turns API/search structs into typed predicates. Empty Option,
empty Vec, and absent optional lists are skipped.
let rows = from
.filter_with
.
.exec
.await?;
Relations
Joined records describe references in the output type:
let rows = from
.
.exec
.await?;
Backrefs and many-to-many helpers render correlated predicates and aggregates.
Use prefetch when child rows should be loaded in a second query.
Subqueries And CTEs
let comments = table;
let posts = table;
let visible_post_ids = from
.filter
.
.set
.subquery?;
let rows = from
.filter
.
.exec
.await?;
Use cte() plus .with(&cte) when the derived source should be declared in a
WITH clause and reused by the parent query.
Functions
Legend: yes = implemented, no = unsupported.
| Row function | MySQL | SQLite3 | Postgres |
|---|---|---|---|
now, coalesce, case |
yes | yes | yes |
ranking windows: row_number, rank, dense_rank |
yes | yes | yes |
distribution windows: percent_rank, cume_dist, ntile |
yes | yes | yes |
value windows: lag, lead, first_value, last_value, nth_value |
yes | yes | yes |
| JSON path helpers | yes | yes | yes |
json::postgres::contains |
no | no | yes |
| SQL array helpers | no | no | yes |
postgres::unaccent |
no | no | yes |
custom functions: funcs::func, funcs::custom |
yes | yes | yes |
| Aggregate | MySQL | SQLite3 | Postgres |
|---|---|---|---|
count, count_all |
yes | yes | yes |
sum, avg, min, max |
yes | yes | yes |
Aggregates work with group_by, having, scalar terminals, output
assignments, and over(window()) where the backend supports windows.
Migrations
With migrations, Mool re-exports Gaman
schema/migration tools and adds registries for root and crate-owned migration
sources.
let mut registry = new;
registry.register_schema?;
Use db::schema(...) instead of raw SchemaBuilder when models include native
enum fields.
Testing
MockDBSession records statements and returns planned responses.
use ;
let mut session = new;
session.plan;
let rows = from
.
.exec
.await?;
Verification
Boundary
Mool owns database concerns: pools, sessions, records, models, typed queries, filters, relations, raw SQL, schema metadata, migrations, enum mappings, and test mocks.
Framework concerns belong outside Mool: routing, commands, templates, assets, uploads, task queues, notifications, and UI.