Skip to main content

toasty/
lib.rs

1#![warn(missing_docs)]
2//! Toasty is an async ORM for Rust supporting both SQL (SQLite, PostgreSQL,
3//! MySQL) and NoSQL (DynamoDB) databases.
4//!
5//! This crate is the user-facing API. It contains the database handle,
6//! query execution traits, and the types that generated code builds on. For
7//! a tutorial-style introduction, see the [Toasty guide].
8//!
9#![doc = include_str!(concat!(env!("OUT_DIR"), "/guide_link.md"))]
10//!
11//! # Modules
12//!
13//! ## [`db`] — database handle and connection pool
14//!
15//! The [`Db`] type is the entry point for all database interaction. It owns
16//! a connection pool and provides [`Db::builder`] for configuration. The
17//! module also contains [`Builder`](db::Builder) and the pool internals.
18//!
19//! ## [`schema`] — model, relation, and schema inspection
20//!
21//! The [`Model`](schema::Model) trait represents a root model that maps to a
22//! database table. It is implemented by `#[derive(Model)]` — users do not
23//! implement it manually. The module also contains [`Field`](schema::Field),
24//! which provides schema registration and runtime helpers for field types, and
25//! [`Auto`](schema::Auto), a wrapper for auto-generated values such as
26//! database-assigned IDs.
27//!
28//! Relation fields can use [`Deferred`](schema::Deferred) for lazy loading or
29//! direct relation values for eager loading. Lazy relations are populated
30//! through `.include(...)` or generated relation accessors. Eager relations are
31//! loaded whenever the model is loaded.
32//!
33//! The module also re-exports from `toasty-core` for inspecting the
34//! app-level and db-level schema representations at runtime.
35//!
36//! ## [`stmt`] — typed statement and expression types
37//!
38//! Contains the typed wrappers around the statement AST:
39//! [`Query`](stmt::Query), [`Insert`](stmt::Insert),
40//! [`Update`](stmt::Update), [`Delete`](stmt::Delete), and
41//! [`Statement`]. Also includes expression helpers like [`Expr`](stmt::Expr),
42//! [`Path`](stmt::Path), and the [`in_list`](stmt::in_list) function.
43//! Generated query builders (e.g. `find_by_*`, `filter_by_*`) produce these
44//! types.
45//!
46//! ## [`sql`] — raw SQL execution helpers
47//!
48//! Contains [`sql::statement`] and [`sql::query`] for running backend SQL
49//! through [`Db`], [`Connection`], or [`Transaction`] handles.
50//!
51//! # Key traits
52//!
53//! - [`Model`](schema::Model) — a root model backed by a database table.
54//!   Implemented by `#[derive(Model)]`.
55//! - [`Embed`](schema::Embed) — an embedded type whose fields are flattened
56//!   into the parent model's table. Implemented by `#[derive(Embed)]`.
57//! - [`Executor`] — the dyn-compatible interface for running statements.
58//!   [`Db`] and [`Transaction`] both implement it. The generic
59//!   [`exec`](Executor::exec) method on `dyn Executor` accepts any typed
60//!   [`Statement<T>`].
61//! - [`Load`](schema::Load) — deserializes a model instance from the database
62//!   value representation.
63//!
64//! # Other key types
65//!
66//! - [`Transaction`] / [`TransactionBuilder`] — transactions with
67//!   auto-rollback on drop and nested savepoint support.
68//! - [`Page`](stmt::Page) — a page of results from a paginated query, with cursor-based
69//!   navigation.
70//! - [`Batch`](stmt::Batch) — groups multiple queries into a single round-trip.
71//! - [`Capability`] / [`SqlPlaceholder`] — driver metadata, including SQL
72//!   placeholder syntax for raw SQL.
73//! - [`Error`] / [`Result`] — re-exported from `toasty-core`.
74//!
75//! # Derive macros
76//!
77//! - [`#[derive(Model)]`](derive@Model) — generates the
78//!   [`Model`](schema::Model) impl, query builders, create/update builders,
79//!   relation accessors, and schema registration for a struct.
80//! - [`#[derive(Embed)]`](derive@Embed) — generates the
81//!   [`Embed`](schema::Embed) impl for a type whose fields are stored inline
82//!   in a parent model's table.
83//!
84//! # Feature flags
85//!
86//! Each database driver is behind an optional feature flag:
87//!
88//! | Feature        | Driver crate                 |
89//! |----------------|------------------------------|
90//! | `sqlite`       | `toasty-driver-sqlite`       |
91//! | `postgresql`   | `toasty-driver-postgresql`   |
92//! | `mysql`        | `toasty-driver-mysql`        |
93//! | `dynamodb`     | `toasty-driver-dynamodb`     |
94//! | `turso`        | `toasty-driver-turso`        |
95//!
96//! Additional feature flags: `rust_decimal`, `bigdecimal`, `jiff` (date/time
97//! via the `jiff` crate), and `serde` (JSON serialization support).
98//!
99//! # Other crates in the workspace
100//!
101//! `toasty` depends on several internal crates that most users do not need
102//! to use directly:
103//!
104//! - **`toasty-core`** — shared types used across the workspace: the schema
105//!   representations (app-level and db-level), the statement AST, the
106//!   [`Driver`](driver::Driver) trait, and [`Error`] / [`Result`].
107//! - **`toasty-macros`** — the proc-macro entry points and the code generation
108//!   logic they call.
109//! - **`toasty-sql`** — serializes the statement AST to SQL strings. Used by
110//!   the SQL driver crates.
111//! - **`toasty-driver-*`** — one crate per database backend.
112
113mod update_target;
114pub use update_target::UpdateTarget;
115
116// `Batch`, `batch()`, and `CreateMany` live in `stmt`.
117pub use stmt::{Batch, batch};
118
119/// Database handle, connection pool, executor trait, and transaction support.
120pub mod db;
121pub use db::{
122    Capability, Connection, Db, Executor, SqlPlaceholder, Transaction, TransactionBuilder,
123};
124
125mod engine;
126
127/// Schema migration types: history files, snapshots, and generation helpers.
128#[cfg(feature = "migration")]
129pub mod migration;
130
131/// Model, relation, and schema inspection types.
132pub mod schema;
133pub use schema::Deferred;
134
135// `Page` lives in `stmt`.
136
137/// Typed statement, expression, and query builder types.
138pub mod stmt;
139#[cfg(feature = "serde")]
140pub use stmt::Json;
141pub use stmt::Statement;
142
143/// Raw SQL execution helpers.
144pub mod sql;
145
146pub use toasty_macros::{Embed, Model, create, query, update};
147
148pub use toasty_core::{Error, Result, schema::app::ModelSet};
149
150pub mod codegen_support;