This crate provides the #[ergol]
macro. It allows to persist the data in a
database. For example, you just have to write
use ergol::prelude::*;
#[ergol]
pub struct User {
#[id] pub id: i32,
#[unique] pub username: String,
pub password: String,
pub age: Option<i32>,
}
and the #[ergol]
macro will generate most of the code you will need. You'll
then be able to run code like the following:
# use ergol::prelude::*;
# #[ergol]
# pub struct User {
# #[id] pub id: i32,
# #[unique] pub username: String,
# pub password: String,
# pub age: Option<i32>,
# }
# use ergol::tokio;
# #[tokio::main]
# async fn main() -> Result<(), ergol::tokio_postgres::Error> {
# let (client, connection) = ergol::connect(
# "host=localhost user=ergol password=ergol dbname=ergol",
# ergol::tokio_postgres::NoTls,
# )
# .await?;
# tokio::spawn(async move {
# if let Err(e) = connection.await {
# eprintln!("connection error: {}", e);
# }
# });
User::drop_table().execute(&client).await.ok();
User::create_table().execute(&client).await?;
let mut user: User = User::create("thomas", "pa$$w0rd", Some(28)).save(&client).await?;
*user.age.as_mut().unwrap() += 1;
user.save(&client).await?;
let user: Option<User> = User::get_by_username("thomas", &client).await?;
let users: Vec<User> = User::select().execute(&client).await?;
# Ok(())
# }
See the book for more information.