and the #[ergol] macro will generate most of the code you will need. You'll
then be able to run code like the following:
externcrate tokio;
externcrate ergol;
use ergol::prelude::*;
#[ergol]pubstructUser {
#[id]pub id: i32,
#[unique]pub username: String,
pub password: String,
pub age: Option<i32>,
}
#[tokio::main]asyncfnmain() -> Result<(), ergol::tokio_postgres::Error> {
let (db, connection) = ergol::connect(
"host=localhost user=ergol password=ergol dbname=ergol",
ergol::tokio_postgres::NoTls,
)
.await?;
tokio::spawn(asyncmove {
ifletErr(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
// Drop the user table if it exists
User::drop_table().execute(&db).await.ok();
// Create the user table
User::create_table().execute(&db).await?;
// Create a user and save it into the databaseletmut user: User = User::create("thomas", "pa$$w0rd", Some(28)).save(&db).await?;
// Change some of its fields
*user.age.as_mut().unwrap() += 1;
// Update the user in the database
user.save(&db).await?;
// Fetch a user by its username thanks to the unique attributelet user: Option<User> = User::get_by_username("thomas", &db).await?;
// Select all userslet users: Vec<User> = User::select().execute(&db).await?;
Ok(())
}