Crate parvati

source ·
Expand description

§Parvati SDK

Indeed, an ORM library, not a framework, written in Rust

§Features

The main idea that I put into my ORM library is a minimum of stupid code and easy use of the library. I wanted users not to have to write long chains of function calls to construct a simple SQL query.

  • SQLite support
  • MySQL support

§Usage

Cargo.toml

[dependencies]
parvati = {version = "1.0.0", features = ["sqlite"]} # or "mysql"
parvati_derive = "1.0.0"
#[tokio::test]
async fn test() -> Result<(), ORMError> {

    let file = std::path::Path::new("file1.db");
    if file.exists() {
        std::fs::remove_file(file)?;
    }

    let _ = env_logger::Builder::from_env(env_logger::Env::new().default_filter_or("debug")).try_init();

    let conn = ORM::connect("file1.db".to_string())?;
    let init_script = "create_table_sqlite.sql";
    conn.init(init_script).await?;

    #[derive(TableDeserialize, TableSerialize, Serialize, Deserialize, Debug, Clone)]
    #[table(name = "user")]
    pub struct User {
        pub id: i32,
        pub name: Option<String>,
        pub age: i32,
    }

    let mut user = User {
        id: 0,
        name: Some("John".to_string()),
        age: 30,
    };

    let mut user_from_db: User = conn.add(user.clone()).apply().await?;

    user.name = Some("Mary".to_string());
    let  _: User = conn.add(user.clone()).apply().await?;

    let user_opt: Option<User> = conn.find_one(user_from_db.id as u64).run().await?;
    log::debug!("User = {:?}", user_opt);

    let user_all: Vec<User> = conn.find_all().run().await?;
    log::debug!("Users = {:?}", user_all);

    user_from_db.name = Some("Mike".to_string());
    let _updated_rows: usize = conn.modify(user_from_db.clone()).run().await?;


    let user_many: Vec<User> = conn.find_many("id > 0").limit(2).run().await?;
    log::debug!("Users = {:?}", user_many);

    let query = format!("select * from user where name like {}", conn.protect("M%"));
    let result_set: Vec<Row> = conn.query(query.as_str()).exec().await?;
    for row in result_set {
        let id: i32 = row.get(0).unwrap();
        let name: Option<String> = row.get(1);
        log::debug!("User = id: {}, name: {:?}", id, name);
    }

    let updated_rows = conn.query_update("update user set age = 100").exec().await?;
    log::debug!("updated_rows: {}", updated_rows);
    let updated_rows: usize = conn.remove(user_from_db.clone()).run().await?;
    log::debug!("updated_rows: {}", updated_rows);
    conn.close().await?;

    Ok(())
}

Modules§

  • mysql is a module that contains the ORM struct that represents an Object-Relational Mapping (ORM) for a MySQL database.
  • sqlite is a module that contains the ORM struct that represents an Object-Relational Mapping (ORM) for a SQLite database.

Structs§

  • QueryBuilder is a struct that represents a SQL query builder. It is used to construct SQL queries in a safe and convenient manner. The QueryBuilder struct is generic over the lifetime 'a, the result type R, the entity type E, and the ORM type O. The ORM type O must implement the ORMTrait.
  • Row is a struct that represents a row in a database table. It contains a HashMap where the keys are column indices and the values are the column values.

Enums§

  • ORMError is an enumeration of possible errors that can occur in the ORM library. Each variant represents a different kind of error.

Traits§

  • ORMTrait is a trait that provides methods for interacting with a database. This trait is used to perform operations such as adding data, finding data, modifying data, and removing data. It also provides methods for executing arbitrary queries and escaping strings.
  • TableDeserialize is a trait that provides methods for deserializing table data. This trait is used to convert data from a stored or transmitted format into table data.
  • TableSerialize is a trait that provides methods for serializing table data. This trait is used to convert table data into a format that can be stored or transmitted.