-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.rs
More file actions
74 lines (66 loc) · 1.99 KB
/
main.rs
File metadata and controls
74 lines (66 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use sqlx::{
migrate::Migrator,
types::chrono::{DateTime, Utc},
FromRow, SqlitePool,
};
use tiny_orm::{SetOption, Table};
#[derive(Debug, FromRow, Table, Clone)]
struct Todo {
id: i32,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
description: String,
done: bool,
}
#[derive(Debug, FromRow, Table, Clone)]
struct NewTodo {
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
description: String,
done: bool,
}
#[derive(Debug, Default, FromRow, Table, Clone)]
struct UpdateTodo {
id: i32,
updated_at: SetOption<DateTime<Utc>>,
description: SetOption<String>,
done: SetOption<bool>,
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let m = Migrator::new(std::path::Path::new("examples/sqlite/migrations"))
.await
.unwrap();
let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
let _ = m.run(&pool).await.unwrap();
let new_todo = NewTodo {
created_at: Utc::now(),
updated_at: Utc::now(),
description: "My first item".to_string(),
done: false,
};
let todo = new_todo
.create(&pool)
.await
.expect("Todo item should be created");
println!("My first todo created {:?}", todo.id);
let first_todo = Todo::get_by_id(&pool, &todo.id).await.unwrap();
match first_todo {
Some(ref item) => println!("First todo item is {:?}", item),
None => println!("Todo item does not exist for the id {}", todo.id),
}
let update_todo = UpdateTodo {
id: todo.id,
description: "My first item updated".to_string().into(),
..Default::default()
};
update_todo
.update(&pool)
.await
.expect("Item should be updated");
let check_updated_item = Todo::get_by_id(&pool, &todo.id).await.unwrap();
match check_updated_item {
Some(ref item) => println!("Updated item is {:?}", item),
None => println!("Todo item does not exist for the id {}", todo.id),
}
}