laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
//! Database connections, raw SQL, and transactions.
//!
//! Most handlers read or write a few rows. This module runs that SQL on the connections your
//! database configuration defines. [`query!`] binds the Rust values it captures as query parameters,
//! [`table`] starts a [query builder](Builder), and [`transaction`] runs writes that must succeed or
//! fail together. Each connection opens its pool on the first query, and a connection with read
//! replicas sends reads to them. To learn how to configure connections and write common queries,
//! check out the [database guide](https://github.com/laravel/rust-framework/blob/0.x/docs/database.md).
//!
//! ```no_run
//! # async fn example() -> laravel::Result<()> {
//! use laravel::db;
//!
//! let active = true;
//! let users = db::query!("SELECT id, name FROM users WHERE active = {active}")
//!     .select()
//!     .await?;
//!
//! for user in users {
//!     let name: String = user.try_get("name")?;
//! }
//!
//! db::transaction(async |tx| {
//!     db::query!(tx, "UPDATE accounts SET balance = balance - 25 WHERE id = 1").update().await?;
//!     db::query!(tx, "UPDATE accounts SET balance = balance + 25 WHERE id = 2").update().await?;
//!
//!     Ok(())
//! })
//! .await?;
//! # Ok(()) }
//! ```
//!
//! [`default_connection`] and [`connection`] return an [`AnyConnection`], which works with every
//! enabled driver. [`connection_for`] returns a [`Connection`] for one `SQLx` backend, for code that
//! needs the native driver.

mod commands;
mod provider;
mod seed;

pub(crate) use laravel_database::db::*;
#[doc(hidden)]
pub use laravel_database::db::{
    __DeserializeOwned, __Migration, __Seeder, __Serialize, __Step, __StepFuture, __decode_enum,
    __query_default_connection, __seed, Seeders, values,
};
#[doc(inline)]
pub use laravel_database::db::{
    AfterCommitError, AnyConnection, AnyQuery, AnyQueryResult, AnyResultSet, AnyRow, AnyTransaction, Backend, Binding,
    Builder, ColumnIndex, Condition, Connection, DatabaseManager, Decode, Direction, Driver, Embed, EncodedJson, Enum,
    Expr, FromRow, InOrderOfColumn, IntoColumn, IntoCondition, IntoExpr, IntoList, IntoRows, IntoSelections,
    IntoUpdates, Json, JsonElement, Literal, Migrations, MultipleRecordsFound, Newtype, Nullable, Order, OrderColumn,
    Query, QueryError, QueryInfo, Raw, RecordNotFound, ResultSet, SchemaDumps, Search, Selection, Source, Sql,
    Transaction, Value, Values, Vector, after_commit, all, any, begin, col, connection, connection_for,
    default_connection, default_connection_for, excluded, exists, expr, is_check_violation, is_foreign_key_violation,
    is_lost_connection, is_unique_violation, listen, manager, query, random, raw, schema, search, seeder, sqlx, sub,
    table, transaction, transaction_with_attempts, union, union_all, using_connection, vector,
};
/// A struct whose fields are columns of a model's table, re-exported here beside its derive.
#[doc(inline)]
pub use laravel_database::model::Embed;

/// Compile a directory of migrations into the binary, returning [`Migrations`].
///
/// The path is relative to the crate's `Cargo.toml`. Each `.rs` file in the directory, but not in
/// its subdirectories, is a migration named after the file, such as
/// `2026_10_27_143000_create_posts_table`. Migrations run in name order.
///
/// A migration has one public `migrate` function. Migrations only run forward, so a file that
/// defines `up` or `down` is a compile error. To change or undo a migration that has run, add a new
/// one. `migrate` takes one parameter, which decides how it runs:
///
/// - `&mut AnyTransaction<'_>` runs it in a transaction, which also records it in the
///   `migrations` table. If it fails, neither its changes nor the record are kept. On MySQL, a DDL
///   statement commits everything before it and ends the transaction, so changes up to the failure
///   can stay.
/// - `&AnyConnection` runs it without a transaction, for statements that can't run in one, such as
///   PostgreSQL's `CREATE INDEX CONCURRENTLY`. The record is written once it succeeds.
///
/// ```ignore
/// // database/migrations/2026_10_27_143000_create_posts_table.rs
/// use laravel::db::AnyTransaction;
///
/// pub async fn migrate(tx: &mut AnyTransaction<'_>) -> laravel::Result<()> {
///     tx.query("CREATE TABLE posts (id BIGSERIAL PRIMARY KEY, title TEXT NOT NULL)").statement().await?;
///
///     Ok(())
/// }
/// ```
///
/// A migration may instead define `migrate` with a macro, such as one that creates the tables a
/// framework driver uses. Its `migrate` runs in a transaction:
///
/// ```ignore
/// // database/migrations/0001_01_01_000001_create_cache_table.rs
/// laravel::cache::tables!();
/// ```
///
/// Editing a migration rebuilds the crate. Adding or removing one doesn't, because Cargo can't
/// watch a directory for a macro. Have the crate's `build.rs` print
/// `cargo::rerun-if-changed=database/migrations`, as the
/// [migrations guide](https://github.com/laravel/rust-framework/blob/0.x/docs/migrations.md#rebuilding-after-adding-a-migration)
/// shows.
pub use laravel_rust_database_macros::migrations;

/// Compile a directory's schema dumps into the binary, returning [`SchemaDumps`].
///
/// The path is relative to the crate's `Cargo.toml`. The macro embeds each
/// `<connection>-schema.sql` and `<connection>-schema.dump` file in the directory, which
/// `schema:dump` writes, under its connection's name. When a connection has both, the `.dump`
/// file wins. A missing or empty directory embeds no dumps. Register the dumps with
/// [`ApplicationBuilder::with_schema`](crate::foundation::ApplicationBuilder::with_schema):
///
/// ```ignore
/// laravel::Application::configure()
///     .with_migrations(laravel::db::migrations!("database/migrations"))
///     .with_schema(laravel::db::schema!("database/schema"));
/// ```
///
/// Changing a dump rebuilds the crate. Adding the first one doesn't, so have the crate's
/// `build.rs` print `cargo::rerun-if-changed=database/schema`, as the
/// [migrations guide](https://github.com/laravel/rust-framework/blob/0.x/docs/migrations.md#squashing-migrations)
/// shows.
pub use laravel_rust_database_macros::schema;

pub(crate) use self::commands::register as register_commands;
pub(crate) use self::provider::DatabaseServiceProvider;
pub(crate) use self::seed::register as register_seed_command;