2 releases
new 0.1.1 | May 12, 2025 |
---|---|
0.1.0 | May 12, 2025 |
#359 in Database interfaces
86KB
1.5K
SLoC
rusticx
Overview
rusticx
is a lightweight, intuitive ORM (Object-Relational Mapping) library for Rust, designed to simplify database interactions across various SQL databases, including PostgreSQL, MySQL, and SQLite. It provides a unified interface for managing database connections, executing queries, and handling transactions.
Features
- Multi-Database Support: Seamlessly switch between PostgreSQL, MySQL, and SQLite.
- Asynchronous Operations: Built on top of
tokio
, allowing for non-blocking database interactions. - Error Handling: Comprehensive error management with custom error types.
- Model Definition: Define your database models with ease using traits.
- Transactions: Support for executing transactions with rollback capabilities.
- Serialization: Automatic serialization and deserialization of data using
serde
.
Installation
To use rusticx
, add it to your Cargo.toml
:
cargo add rusticx
Optional Features
You can enable specific database support by adding features in your Cargo.toml
:
[dependencies.rusticx]
version = "0.1.0"
features = ["postgres", "mysql", "rusqlite"]
Getting Started
Basic Usage
- Creating a Connection:
use rusticx::{Connection, DatabaseType};
let conn = Connection::new("postgres://user:password@localhost/dbname")?;
- Defining a Model:
use rusticx::model::SQLModel;
#[derive(Debug, Serialize, Deserialize)]
struct User {
id: Option<i32>,
name: String,
email: String,
}
impl SQLModel for User {
fn table_name() -> String {
"users".to_string()
}
fn primary_key_field() -> String {
"id".to_string()
}
fn primary_key_value(&self) -> Option<i32> {
self.id
}
fn set_primary_key(&mut self, id: i32) {
self.id = Some(id);
}
// Implement other required methods...
}
- Inserting a Record:
let mut user = User { id: None, name: "Alice".to_string(), email: "alice@example.com".to_string() };
user.insert(&conn)?;
- Querying Records:
let users: Vec<User> = User::find_all(&conn)?;
- Updating a Record:
user.name = "Alice Smith".to_string();
user.update(&conn)?;
- Deleting a Record:
user.delete(&conn)?;
Error Handling
rusticx
provides a custom error type, RusticxError
, which encapsulates various error scenarios, including connection errors, query errors, and serialization errors. You can handle these errors using Rust's standard error handling mechanisms.
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes and commit them (
git commit -m 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Create a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contact
For any inquiries or issues, please reach out to Tarun Vishwakarma.
Acknowledgments
Dependencies
~4–19MB
~256K SLoC