-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbank.rs
83 lines (69 loc) · 2.13 KB
/
bank.rs
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
75
76
77
78
79
80
81
82
83
use ate::prelude::*;
use names::Generator;
use rust_decimal::prelude::*;
#[allow(unused_imports)]
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Serialize, Deserialize, Clone)]
struct Person {
first_name: String,
last_name: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
struct Transaction {
from: PrimaryKey,
to: DaoWeak<Person>,
description: String,
amount: Decimal,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
struct Account {
name: String,
transactions: DaoVec<Transaction>,
balance: Decimal,
}
async fn make_account<'a>(
chain: &Arc<Chain>,
generator: &mut Generator<'a>,
) -> Result<(), AteError> {
let session = AteSessionUser::new();
let dio = chain.dio_mut(&session).await;
let person = Person {
first_name: generator.next().unwrap(),
last_name: generator.next().unwrap(),
};
let _person = dio.store(person).unwrap();
let acc = Account {
name: "Current Account".to_string(),
transactions: DaoVec::new(),
balance: Decimal::default(),
};
let mut acc = dio.store(acc).unwrap();
for _ in 0..10 {
let trans = Transaction {
to: DaoWeak::from_key(&dio, acc.key().clone()),
from: PrimaryKey::generate(),
description: generator.next().unwrap(),
amount: Decimal::from_i64(10).unwrap(),
};
acc.as_mut().transactions.push(trans).unwrap();
}
dio.commit().await?;
Ok(())
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), AteError> {
ate::log_init(0, true);
// The default configuration will store the redo log locally in the temporary folder
let conf = ConfAte::default();
let builder = ChainBuilder::new(&conf).await.build();
// We create a chain with a specific key (this is used for the file name it creates)
let chain = builder.open(&ChainKey::from("bank")).await?;
// Make a thousand bank accounts
let mut generator = Generator::default();
for _ in 0..200 {
make_account(&chain, &mut generator).await?;
}
chain.flush().await.unwrap();
Ok(())
}