Skip to content

A full example

The following snippet illustrates how to read the previously mentioned config file to connect to the database, query all users and update, insert and delete some. Note that this is just the most basic functionality, but rorm will provide a lot more functionality in the future:

use std::fs::read_to_string;

use rorm::{delete, insert, update, query, Database};
use rorm::config::DatabaseConfig;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct ConfigFile {
    pub database: DatabaseConfig,
    // more configuration parameters depending on your application ...

async fn main() {
    // Read the config from a TOML file
    let path = "config.toml";
    let db_conf_file = toml::from_str::<ConfigFile>(
            .expect("File read error")
    .expect("Couldn't deserialize configuration file");

    // Connect to the database to get the database handle using the TOML configuration
    let db = Database::connect(DatabaseConfiguration {
        driver: db_conf_file.database.driver,
        min_connections: 1,
        max_connections: 1,
    .expect("error connecting to the database");

    // Query all users from the database
    for user in query!(&db, User)
        .expect("querying failed")
            "User {} '{}' is {} years old",
  , user.username, user.age

    // Add three new users to the database
    insert!(&db, UserNew)
            UserNew {
                username: "foo".to_string(),
                age: 42,
            UserNew {
                username: "bar".to_string(),
                age: 0,
            UserNew {
                username: "baz".to_string(),
                age: 1337,

    // Update the second user by increasing its age
    let all_users = query!(&db, User).all().await.expect("error");
    update!(&db, User)
        .set(User::FIELDS.age, all_users[2].age + 1)

    // Delete some user with age 69 or older than 100 years
    let zero_aged_user = query!(&db, User)
    delete!(&db, User).single(&zero_aged_user).await;