今天接續 BookStore in SurrealDB 的部分:
初始化DB及cqrs框架:
let db = Surreal::new::<Mem>(()).await?;
db.use_ns("test").use_db("test").await?;
let repo = SurrealEventRepository { db: db.clone() };
let book_service = BookService {};
let book_store: PersistedEventStore<SurrealEventRepository, Book>
= PersistedEventStore::new_event_store(repo.clone());
let book_cqrs = CqrsFramework::new(book_store, vec![], book_service.clone());
執行書籍建檔及入庫指令:
let _ = book_cqrs.execute("test-book-1", BookCommand::CreateBook {
id: "test-book-1".to_string(),
title: "Rust 語言開發實戰".to_string(),
isbn10: "1234567890".to_string(),
description: "使用rust,併同cqrs框架,實現event sourcing".to_string(),
}).await;
let _ = book_cqrs.execute("test-book-1", BookCommand::IngestBook {
id: "test-book-1".to_string(),
copies: 2,
}).await;
檢查 event sourcing內容:
let events : Vec<SerializedEvent> = repo.db.query("SELECT * FROM events").await?.take(0)?;
println!("======== Events ========\n{:#?}", events);
======== Events ========
[
SerializedEvent {
aggregate_id: "test-book-1",
sequence: 1,
aggregate_type: "Book",
event_type: "BookEvent",
event_version: "0.1.0",
payload: Object {
"BookCreated": Object {
"description": String("使用rust,併同cqrs框架,實現event sourcing"),
"id": String("test-book-1"),
"isbn10": String("1234567890"),
"title": String("Rust 語言開發實戰"),
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-book-1",
sequence: 2,
aggregate_type: "Book",
event_type: "BookEvent",
event_version: "0.1.0",
payload: Object {
"BookIngested": Object {
"copies": Number(2),
"id": String("test-book-1"),
},
},
metadata: Object {},
},
]
序列化的event sourcing是ok的,看一下aggregate內容:
let book_store: PersistedEventStore<SurrealEventRepository, Book> = PersistedEventStore::new_event_store(repo.clone());
let book = book_store.load_aggregate("test-book-1").await.unwrap();
println!("======== Book ========\n{:#?}", book.aggregate);
======== Book ========
Book {
id: "test-book-1",
title: "Rust 語言開發實戰",
isbn10: "1234567890",
description: "使用rust,併同cqrs框架,實現event sourcing",
copies: 2,
lending_records: [],
lent_history: [],
}
繼續添加一些指令試試看變化,取2個借閱指令:
let _ = book_cqrs.execute("test-book-1", BookCommand::LendBook (
LendingRecord{
reader_id: "test-reader-1".to_string(),
lent_date: Utc::now(),
due_date: Utc::now().add(chrono::Duration::days(7)),
}
)).await;
let _ = book_cqrs.execute("test-book-1", BookCommand::LendBook (
LendingRecord{
reader_id: "test-reader-2".to_string(),
lent_date: Utc::now(),
due_date: Utc::now().add(chrono::Duration::days(7)),
}
)).await;
event store內的aggreage看起來正確
======== Book ========
Book {
id: "test-book-1",
title: "Rust 語言開發實戰",
isbn10: "1234567890",
description: "使用rust,併同cqrs框架,實現event sourcing",
copies: 2,
lending_records: [
LendingRecord {
reader_id: "test-reader-1",
lent_date: 2023-10-01T14:06:15.411234091Z,
due_date: 2023-10-08T14:06:15.411237089Z,
},
LendingRecord {
reader_id: "test-reader-2",
lent_date: 2023-10-01T14:06:15.412799337Z,
due_date: 2023-10-08T14:06:15.412800741Z,
},
],
lent_history: [],
}
event sourcing 內容
[
SerializedEvent {
aggregate_id: "test-book-1",
sequence: 3,
aggregate_type: "Book",
event_type: "BookEvent",
event_version: "0.1.0",
payload: Object {
"BookLent": Object {
"due_date": String("2023-10-08T14:06:15.411237089Z"),
"lent_date": String("2023-10-01T14:06:15.411234091Z"),
"reader_id": String("test-reader-1"),
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-book-1",
sequence: 3,
aggregate_type: "Book",
event_type: "BookEvent",
event_version: "0.1.0",
payload: Object {
"BookLent": Object {
"due_date": String("2023-10-08T14:06:15.412800741Z"),
"lent_date": String("2023-10-01T14:06:15.412799337Z"),
"reader_id": String("test-reader-2"),
},
},
metadata: Object {},
},
]
可以看到 sequence出現2筆都是3的部分,這部分可能是persist沒寫好,接下來要再研究一下怎麼處理這段。