Rust-Rustorm: rustorm — an ORM for Rust

Build Status

rustorm

Rustorm

Financial Contributors on Open Collective Latest Version Build Status MIT licensed

Rustorm is an SQL-centered ORM with focus on ease of use on conversion of database types to their appropriate rust type.

Selecting records

use rustorm::{
    DbError,
    FromDao,
    Pool,
    ToColumnNames,
    ToTableName,
};

#[derive(Debug, FromDao, ToColumnNames, ToTableName)]
struct Actor {
    actor_id: i32,
    first_name: String,
}

#[cfg(any(feature="with-postgres", feature = "with-sqlite"))]
fn main() {
    let mut pool = Pool::new();
    #[cfg(feature = "with-sqlite")]
    let db_url = "sqlite://sakila.db";
    #[cfg(feature = "with-postgres")]
    let db_url = "postgres://postgres:[email protected]/sakila";
    let em = pool.em(db_url).unwrap();
    let sql = "SELECT * FROM actor LIMIT 10";
    let actors: Result<Vec<Actor>, DbError> =
        em.execute_sql_with_return(sql, &[]);
    println!("Actor: {:#?}", actors);
    let actors = actors.unwrap();
    assert_eq!(actors.len(), 10);
    for actor in actors {
        println!("actor: {:?}", actor);
    }
}
#[cfg(feature="with-mysql")]
fn main() {
   println!("see examples for mysql usage, mysql has a little difference in the api");
}

Inserting and displaying the inserted records

use chrono::{
    offset::Utc,
    DateTime,
    NaiveDate,
};
use rustorm::{
    DbError,
    FromDao,
    Pool,
    TableName,
    ToColumnNames,
    ToDao,
    ToTableName,
};


#[cfg(any(feature="with-postgres", feature = "with-sqlite"))]
fn main() {
    mod for_insert {
        use super::*;
        #[derive(Debug, PartialEq, ToDao, ToColumnNames, ToTableName)]
        pub struct Actor {
            pub first_name: String,
            pub last_name: String,
        }
    }

    mod for_retrieve {
        use super::*;
        #[derive(Debug, FromDao, ToColumnNames, ToTableName)]
        pub struct Actor {
            pub actor_id: i32,
            pub first_name: String,
            pub last_name: String,
            pub last_update: DateTime<Utc>,
        }
    }

    let mut pool = Pool::new();
    #[cfg(feature = "with-sqlite")]
    let db_url = "sqlite://sakila.db";
    #[cfg(feature = "with-postgres")]
    let db_url = "postgres://postgres:[email protected]/sakila";
    let em = pool.em(db_url).unwrap();
    let tom_cruise = for_insert::Actor {
        first_name: "TOM".into(),
        last_name: "CRUISE".to_string(),
    };
    let tom_hanks = for_insert::Actor {
        first_name: "TOM".into(),
        last_name: "HANKS".to_string(),
    };

    let actors: Result<Vec<for_retrieve::Actor>, DbError> =
        em.insert(&[&tom_cruise, &tom_hanks]);
    println!("Actor: {:#?}", actors);
    assert!(actors.is_ok());
    let actors = actors.unwrap();
    let today = Utc::now().date();
    assert_eq!(tom_cruise.first_name, actors[0].first_name);
    assert_eq!(tom_cruise.last_name, actors[0].last_name);
    assert_eq!(today, actors[0].last_update.date());

    assert_eq!(tom_hanks.first_name, actors[1].first_name);
    assert_eq!(tom_hanks.last_name, actors[1].last_name);
    assert_eq!(today, actors[1].last_update.date());
}
#[cfg(feature="with-mysql")]
fn main() {
   println!("see examples for mysql usage, mysql has a little difference in the api");
}

Rustorm is wholly used by diwata

License: MIT

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

Comments

  • Add feature to be able to connect to ssl required/enabled database.
    Add feature to be able to connect to ssl required/enabled database.

    Mar 13, 2018

    From https://github.com/ivanceras/diwata/issues/5

    Reply
  • Pool need 'Clone'
    Pool need 'Clone'

    Nov 14, 2019

                                                                                                                                                                                                           
    Reply
  • Remove mut from `em()`
    Remove mut from `em()`

    Mar 31, 2020

    Current em() wants &mut self. It dropped usability for sharing for threads because we have to wrap Pool with Mutex or RwLock. If we do so, we use only single connection over threads.

    This PR is makes BREAKING CHANGES.

    Reply
  • This ORM Does not support transactions ?
    This ORM Does not support transactions ?

    May 31, 2020

    This ORM Does not support transactions ? Or can anyone help me how to start a transaction ? Thank you !

    Reply
  • Unify the DatabaseMut,Database and EntityMut and EntityMut interface
    Unify the DatabaseMut,Database and EntityMut and EntityMut interface

    Jan 7, 2020

                                                                                                                                                                                                           
    Reply
  • implement std Error for error types
    implement std Error for error types

    Oct 25, 2019

    This could help errors from this crate work better with general error handling crates.

    Currently the most suitable solution would be using the thiserror crate. Or, we could manulay implement std::error::Error for all the errors exported from rustorm.

    Which is your fav? I can make a PR based on the choice.

    Reply
  • Some neat picks
    Some neat picks

    Oct 25, 2019

    This PR adds two modifications:

    1. Add NaiveDateTime to Value conversion
    2. Use local dependencies of dao and codegen. This is important because currently rustorm depends on crate.io distributed dao & codegen. This makes depending on the git branch impossible. Thus generally considered as anti-pattern.

    By the way, [replace] is not used when rustorm is introduced as a lib dependency. So it's also removed.

    Reply
  • Problem to adopt it.
    Problem to adopt it.

    Sep 29, 2018

    Hi, rustorm_dao, and rustorm_codegen should update to latest on crates.io, so we can do development using rustorm.

    Reply
  • Can’t compile due to OpenSSL
    Can’t compile due to OpenSSL

    Jun 18, 2019

    I can’t compile Diwata because Rustorm is using an old version of the crate openssl: https://github.com/sfackler/rust-openssl/issues/987

    Would it be possible to bump up the version of openssl in the depencecies?

    Reply
  • MySQL support
    MySQL support

    Sep 29, 2019

    @ivanceras Hi.

    I love this crate more than diesel but not contains mysql support so i added it.

    The big issue

    r2d2-mysql wants connection as mutable for execute query but current signature of Database trait expects immutable. I added Database2, DBPlatform2 and EntityManager2 for keep API for postgres and sqlite. rust-postgres next varsion will be wanted mutable connection maybe (SEE).

    I cloud not implement all of function of Database trait to Database2 trait. But i think it is enough for SELECT, INSERT, UPDATE and DELETE. I hope merge this PR and then implement other function in after.

    Please tell me if there are mistake in my code or thinking.

    Thank you!

    Reply