oxidite-db
Database ORM with relationships and migrations for Oxidite.
Overview
oxidite-db provides a powerful and intuitive Object-Relational Mapping (ORM) system for Rust. It includes model derivation, relationship management, migrations, and query building capabilities to make database interactions simple and type-safe.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
= { = "1", = ["derive"] }
Features
- Model derive macro - Automatic CRUD operations with the
#[derive(Model)]macro - Relationships - Support for HasOne, HasMany, and BelongsTo relationships
- Migrations - Database schema evolution with rollback support
- Soft deletes - Logical deletion with automatic filtering
- Timestamps - Automatic created_at and updated_at tracking
- Field validation - Built-in validation for model fields
- Transaction support - ACID-compliant transaction handling
- Multi-database support - Works with PostgreSQL, MySQL, and SQLite
Usage
Defining Models
Define your database models using the #[derive(Model)] macro:
use ;
use ;
use ;
Database Connection
Establish a connection to your database:
use DbPool;
// Connect to PostgreSQL
let pool = connect.await?;
// Connect to SQLite
let pool = connect.await?;
// Connect to MySQL
let pool = connect.await?;
CRUD Operations
Perform Create, Read, Update, and Delete operations:
// Create a new record
let mut user = User ;
user.create.await?; // User now has an assigned ID
// Read records
let all_users = all.await?;
let specific_user = find.await?;
// Update a record
user.name = "Jane Doe".to_string;
user.update.await?;
// Delete a record
user.delete.await?;
Query Building
Build complex queries with the fluent interface:
// Find with conditions
let users = where_eq
.order_by
.limit
.get
.await?;
// Count records
let count = count.await?;
// Find first matching record
let user = where_eq
.first
.await?;
Relationships
Define and use relationships between models:
// Get user's posts
let user_posts = user..await?;
// Get post's author
let author = post..await?;
Migrations
Manage your database schema with migrations:
use ;
;
// Run migrations
CreateUsersTable.run_up.await?;
Transactions
Use transactions for atomic database operations:
use Transaction;
let tx = pool.begin_transaction.await?;
// Perform operations within transaction
let mut user = User ;
user.create.await?;
let mut post = Post ;
post.create.await?;
// Commit the transaction
tx.commit.await?;
// Or rollback if something goes wrong
// tx.rollback().await?;
License
MIT