Skip to main content

Crate turso_orm

Crate turso_orm 

Source
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:

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 Column enums deliberately do not derive PartialEq, so that Column::X.eq(v) resolves to the entity::ColumnTrait condition builder instead of the PartialEq method.
  • Every entity::ColumnTrait builder emits a qualified table.column reference, so conditions written against one entity never clash with a joined table that has a column of the same name.
  • entity::ModelTrait::set and entity::ActiveModelTrait::set return a Result instead 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), a entity::Linked chain any number, and a table joined twice is aliased by query::Select rather than by the caller. Rust allows one Related<Target> impl per entity, so a second relation to the same table is used through its definition.
  • The __private module 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§

pub use entity::Schema;
pub use self as sql;

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 / HAVING condition.
ConnectOptions
The options for opening a Turso database.
Database
A Turso database with a pool of connections.
ExecResult
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.
JoinType
The join kinds.
Order
A sort direction.
TransactionMode
How a top-level transaction is started.
Value
A bound parameter or literal.

Traits§

Build
Anything that renders to a Statement.
ConnectionTrait
Executes statements and fetches rows.
IntoCondition
Anything usable as a condition: an Expr or a Condition.
StreamTrait
Streams rows lazily.
TransactionTrait
Starts transactions.

Type Aliases§

DatabaseConnection
Alias of Database for code that prefers the longer name.
Result
The result alias used throughout the crate, defaulting to DbErr.

Derive Macros§

DeriveActiveEnummacros
Derives ActiveEnum and the column-type impls for a fieldless enum.
DeriveEntityModelmacros
Derives an entity from a Model struct.
DeriveIdenmacros
Derives IdenStatic and the SQL identifier traits for a unit struct or a fieldless enum.
DeriveIntoActiveModelmacros
Derives IntoActiveModel for a plain struct whose fields are a subset of an entity’s columns.
DerivePartialModelmacros
Derives PartialModelTrait and FromQueryResult for a projection struct.
DeriveRelationmacros
Derives RelationTrait and Related<R> impls from a Relation enum.
FromQueryResultmacros
Derives FromQueryResult for a plain struct.