Expand description
ORM: an async object-relational mapper dedicated to the Turso database.
The public API follows the entity, active model and query builder shape
familiar from the Rust ORM ecosystem, but this is a from-scratch,
single-engine stack: there is no backend abstraction and no SQL dialect
switch. Targeting one engine lets the crate lean on SQLite semantics
directly — INTEGER PRIMARY KEY row ids,
RETURNING *, pragma_table_info — instead of papering over differences.
The crate sits on two siblings it re-exports rather than wraps:
turso_orm_driverowns connections, pooling, transactions and row decoding; it is re-exported asDatabase,ConnectionTrait,Transactionand friends.turso_sqlowns the SQL builders and theValuetype; it is re-exported assqltogether with the pieces entity code needs (Expr,Condition,Func,Order,Statement).
What this crate adds is the entity layer (entity) — the traits a
Model struct, its Column and PrimaryKey enums and its ActiveModel
implement — and the typed query builders (query) that turn them into
statements. The derive macros that generate those impls live in
turso_orm_macros and are re-exported behind the macros feature.
§Design decisions
- Generated
Columnenums deliberately do not derivePartialEq, so thatColumn::X.eq(v)resolves to theentity::ColumnTraitcondition builder instead of thePartialEqmethod. - Every
entity::ColumnTraitbuilder emits a qualifiedtable.columnreference, so conditions written against one entity never clash with a joined table that has a column of the same name. entity::ModelTrait::setandentity::ActiveModelTrait::setreturn aResultinstead of panicking on a value of the wrong type.- A relation is plain data (
entity::RelationDef): the same value renders the join, drives the loaders and emits the foreign key. A many-to-many relation is two of them (entity::Related::via), aentity::Linkedchain any number, and a table joined twice is aliased byquery::Selectrather than by the caller. Rust allows oneRelated<Target>impl per entity, so a second relation to the same table is used through its definition. - The
__privatemodule exists for generated code only; nothing in it is part of the public API.
§Example
use turso_orm::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[turso(table_name = "user")]
pub struct Model {
#[turso(primary_key)]
pub id: i32,
#[turso(unique)]
pub email: String,
pub name: Option<String>,
}
#[derive(Copy, Clone, Debug, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
let db = Database::connect(ConnectOptions::in_memory()).await?;
db.execute(Schema::new().create_table_from_entity(Entity).to_statement()).await?;
let user = ActiveModel { email: Set("a@b.c".into()), ..Default::default() }.insert(&db).await?;
let found = Entity::find_by_id(user.id).one(&db).await?;Re-exports§
Modules§
- entity
- The entity layer: entities, models, active models, columns, primary keys and relations.
- prelude
- Everything an entity module needs, meant to be glob-imported.
- query
- The typed query builders: select, insert, update, delete and pagination.
- types
- The mapping between Rust field types and column types, modeled by
TursoType.
Structs§
- Condition
- A composable
WHERE/HAVINGcondition. - Connect
Options - The options for opening a Turso database.
- Database
- A Turso database with a pool of connections.
- Exec
Result - The result of a statement that does not return rows.
- Func
- A function call.
- Row
- A result row.
- Statement
- A rendered statement: SQL with
?placeholders and the values to bind. - Transaction
- A transaction on one Turso connection.
Enums§
- DbErr
- The error returned by every fallible operation of the ORM.
- Expr
- A SQL expression.
- Join
Type - The join kinds.
- Order
- A sort direction.
- Transaction
Mode - How a top-level transaction is started.
- Value
- A bound parameter or literal.
Traits§
- Build
- Anything that renders to a
Statement. - Connection
Trait - Executes statements and fetches rows.
- Into
Condition - Anything usable as a condition: an
Expror aCondition. - Stream
Trait - Streams rows lazily.
- Transaction
Trait - Starts transactions.
Type Aliases§
- Database
Connection - Alias of
Databasefor code that prefers the longer name. - Result
- The result alias used throughout the crate, defaulting to
DbErr.
Derive Macros§
- Derive
Active Enum macros - Derives
ActiveEnumand the column-type impls for a fieldless enum. - Derive
Entity Model macros - Derives an entity from a
Modelstruct. - Derive
Iden macros - Derives
IdenStaticand the SQL identifier traits for a unit struct or a fieldless enum. - Derive
Into Active Model macros - Derives
IntoActiveModelfor a plain struct whose fields are a subset of an entity’s columns. - Derive
Partial Model macros - Derives
PartialModelTraitandFromQueryResultfor a projection struct. - Derive
Relation macros - Derives
RelationTraitandRelated<R>impls from aRelationenum. - From
Query Result macros - Derives
FromQueryResultfor a plain struct.