#orm #sqlite #postgresql #column-name

macro rustorm_codegen

code gen provides macro for generating code on data access objects on table metadata

11 unstable releases (5 breaking)

0.20.0 Mar 9, 2021
0.18.0 Dec 1, 2020
0.5.0 Jan 8, 2020
0.3.1 Oct 28, 2019
0.1.1 Mar 11, 2018

#10 in #column-name

Download history 15/week @ 2023-12-11 34/week @ 2023-12-18 26/week @ 2023-12-25 24/week @ 2024-01-01 30/week @ 2024-01-08 33/week @ 2024-01-15 34/week @ 2024-01-22 37/week @ 2024-01-29 49/week @ 2024-02-05 61/week @ 2024-02-12 80/week @ 2024-02-19 95/week @ 2024-02-26 58/week @ 2024-03-04 65/week @ 2024-03-11 45/week @ 2024-03-18 40/week @ 2024-03-25

217 downloads per month
Used in 5 crates (3 directly)

MIT license

34KB
972 lines

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:p0stgr3s@localhost/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:p0stgr3s@localhost/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]

Dependencies

~7MB
~142K SLoC