Bottle ORM
Bottle ORM is a lightweight, async ORM for Rust built on top of sqlx. It is designed to be simple, efficient, and easy to use, providing a fluent Query Builder and automatic schema migrations.
Features
- Async & Non-blocking: Built on
tokioandsqlx. - Multi-Driver Support: Compatible with PostgreSQL, MySQL, and SQLite (via
sqlx::Any). - Macro-based Models: Define your schema using standard Rust structs with
#[derive(Model)]. - Fluent Query Builder: Chainable methods for filtering, selecting, pagination, and sorting.
- Auto-Migration: Automatically creates tables and foreign key constraints based on your structs.
Installation
Add bottle-orm to your Cargo.toml. You will also need sqlx, tokio, and serde.
[]
= "0.1.0"
= { = "0.8", = ["runtime-tokio", "tls-native-tls", "any", "postgres", "sqlite", "mysql", "chrono"] }
= { = "1", = ["full"] }
= { = "1", = ["derive"] }
= { = "0.4", = ["serde"] }
Quick Start
1. Define your Models
Use the #[derive(Model)] macro to define your database tables. You can use the #[orm(...)] attribute to configure columns.
use Model;
use ;
use ;
use FromRow;
2. Connect and Migrate
Initialize the database connection and run migrations to create tables automatically.
use Database;
use env;
async
3. Insert Data
let new_user = User ;
db..insert.await?;
4. Query Data
Use the fluent query builder to filter and retrieve data.
// Fetch a single user by ID
let user: User = db.
.filter
.first
.await?;
println!;
// Fetch multiple records with conditions, order, and pagination
let adults: = db.
.filter
.order
.limit
.scan
.await?;
for u in adults
Supported Attributes (#[orm(...)])
primary_key: Marks the column as the Primary Key.unique: Adds a UNIQUE constraint.create_time: Sets default value to current timestamp on creation.foreign_key = "Table::Column": Creates a Foreign Key relationship.size = N: (Optional) Hints the size for text columns (mostly for docs/optimization).index: Creates an index for this column.
Database Support
Bottle ORM uses sqlx::Any to support multiple databases. The driver is detected automatically from the connection string scheme:
postgres://...-> PostgreSQLmysql://...-> MySQLsqlite://...-> SQLite
License
This project is licensed under the MIT License - see the LICENSE file for details.