MCPcopy Create free account
hub / github.com/NyxCode/ormx / main

Function main

example-postgres/src/main.rs:12–79  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10
11#[tokio::main]
12async fn main() -> anyhow::Result<()> {
13 dotenv::dotenv().ok();
14 env_logger::builder()
15 .filter_level(LevelFilter::Debug)
16 .init();
17
18 let db = PgPool::connect(&dotenv::var("DATABASE_URL")?).await?;
19
20 log::info!("insert a few new rows into the database");
21 let mut new = InsertUser {
22 user_id: 1,
23 first_name: "Moritz".to_owned(),
24 last_name: "Bischof".to_owned(),
25 email: "moritz.bischof1@gmail.com".to_owned(),
26 disabled: None,
27 role: Role::User,
28 ty: Some(AccountType::Normal),
29 }
30 .insert(&db)
31 .await?;
32 InsertUser {
33 user_id: 2,
34 first_name: "Dylan".to_owned(),
35 last_name: "Thomas".to_owned(),
36 email: "dylan.thomas@gmail.com".to_owned(),
37 disabled: Some("email not verified".to_owned()),
38 role: Role::Admin,
39 ty: None,
40 }
41 .insert(&db)
42 .await?;
43
44 log::info!("update a single field");
45 new.set_last_login(&db, Some(Utc::now().naive_utc()))
46 .await?;
47
48 log::info!("update all fields at once");
49 new.email = "asdf".to_owned();
50 new.update(&db).await?;
51
52 log::info!("apply a patch to the user");
53 new.patch(
54 &db,
55 UpdateUser {
56 first_name: "NewFirstName".to_owned(),
57 last_name: "NewLastName".to_owned(),
58 disabled: Some("Reason".to_owned()),
59 role: Role::Admin,
60 },
61 )
62 .await?;
63
64 log::info!("reload the user, in case it has been modified");
65 new.reload(&db).await?;
66
67 log::info!("use the improved query macro for searching users");
68 let search_result = query2::query_users(&db, Some("NewFirstName"), None).await?;
69 log::info!("search result: {:?}", search_result);

Callers

nothing calls this directly

Calls 5

insertMethod · 0.80
patchMethod · 0.80
reloadMethod · 0.80
deleteMethod · 0.80
query_usersFunction · 0.70

Tested by

no test coverage detected