use std::time::Duration;
use prax_orm::{Model, PraxClient, client};
use prax_postgres::{PgEngine, PgPool, PgPoolBuilder};
#[derive(Model, Debug)]
#[prax(table = "example_users")]
struct User {
#[prax(id, auto)]
id: i32,
#[prax(unique)]
email: String,
name: Option<String>,
}
client!(User);
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url = std::env::var("PRAX_POSTGRES_URL")
.unwrap_or_else(|_| "postgres://prax:prax_test_password@localhost:5432/prax_test".into());
let pool: PgPool = PgPoolBuilder::new()
.url(url)
.max_connections(4)
.connection_timeout(Duration::from_secs(10))
.build()
.await?;
let conn = pool.get().await?;
conn.batch_execute(
"DROP TABLE IF EXISTS example_users; \
CREATE TABLE example_users ( \
id SERIAL PRIMARY KEY, \
email TEXT UNIQUE NOT NULL, \
name TEXT \
)",
)
.await?;
drop(conn);
let client = PraxClient::new(PgEngine::new(pool));
let alice = client
.user()
.create()
.set("email", "alice@example.com")
.set("name", "Alice")
.exec()
.await?;
println!("Created: {alice:?}");
let all = client.user().find_many().exec().await?;
println!("All: {all:?}");
let updated = client
.user()
.update()
.r#where(user::id::equals(alice.id))
.set("name", "Alicia")
.exec()
.await?;
println!("Updated: {updated:?}");
let count_before = client.user().count().exec().await?;
println!("Count: {count_before}");
let deleted = client
.user()
.delete_many()
.r#where(user::email::contains("example.com"))
.exec()
.await?;
println!("Deleted: {deleted} rows");
Ok(())
}