rusticx

Crates.iorusticx
lib.rsrusticx
version0.1.3
created_at2025-05-12 06:41:16.193893+00
updated_at2025-05-14 08:54:34.653185+00
descriptionA lightweight, intuitive ORM library for Rust
homepage
repositoryhttps://github.com/TarunVishwakarma1/rustix-orm
max_upload_size
id1670080
size146,177
Tarun Vishwakarma (TarunVishwakarma1)

documentation

README

rusticx

rusticx License: MIT

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. (Tested for Postgres)
  • Asynchronous Operations: Built on top of tokio, allowing for non-blocking database interactions. (in development)
  • 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.3"
features = ["postgres"] 

Getting Started

Basic Usage

  1. Creating a Connection:
use rusticx::{Connection, DatabaseType};

let conn = Connection::new("postgresql://user:password@localhost/dbname")?;
  1. 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...
}

Usage with rustic_derive

use rusticx::model::SQLModel;
use rusticx_derive::Model;

#[derive(Debug, Serialize, Deserialize, Model)]
#[model(table = "users")]
pub struct User {
    /// The unique identifier for the user, which is the primary key.
    #[model(primary_key, auto_increment)]
    pub id: Option<i32>,

    /// The full name of the user.
    #[model(column = "full_name")] 
    #[serde(rename = "full_name")]
    pub name: String,

    /// The email address of the user.
    pub email: String,

    /// The timestamp when the user was created.
    pub created_at: NaiveDateTime,

    /// The password of the user.
    #[model(sql_type = "VARCHAR(100)")]
    pub password_hash: String,
}
  1. Inserting a Record:
let mut user = User { id: None, name: "Alice".to_string(), email: "alice@example.com".to_string() };
user.insert(&conn)?;
  1. Querying Records:
let users: Vec<User> = User::find_all(&conn)?;
  1. Updating a Record:
user.name = "Alice Smith".to_string();
user.update(&conn)?;
  1. 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:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Make your changes and commit them (git commit -m 'Add new feature').
  4. Push to the branch (git push origin feature-branch).
  5. 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

  • Rust - The programming language used.
  • Tokio - The asynchronous runtime for Rust.
  • Serde - The serialization framework for Rust.
Commit count: 33

cargo fmt