測試一下snapshot是否work
在new 先前的 cqrs event store 改為aggregate:
let reader_store: PersistedEventStore<SurrealEventRepository, Reader>
= PersistedEventStore::new_aggregate_store(repo.clone());
// = PersistedEventStore::new_event_store(repo.clone());
let book_store: PersistedEventStore<SurrealEventRepository, Book>
= PersistedEventStore::new_aggregate_store(repo.clone());
// = PersistedEventStore::new_event_store(repo.clone());
執行先前的
建立讀者1
建立讀者2
建立書籍1
書籍1入庫2本
讀者1借閱書籍1
讀者2借閱書籍1
讀者1歸還書籍1
完畢後 ES如下:
let reader_snaps : Vec<SerializedEvent> = repo.db
.query("SELECT * FROM events ORDER BY aggregate_id, sequence").await?.take(0)?;
println!("======== [Source of Truth] Events ========\n{:#?}", reader_snaps);
[
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 {},
},
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-13T14:52:21.629658384Z"),
"lent_date": String("2023-10-06T14:52:21.629657377Z"),
"reader_id": String("test-reader-1"),
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-book-1",
sequence: 4,
aggregate_type: "Book",
event_type: "BookEvent",
event_version: "0.1.0",
payload: Object {
"BookLent": Object {
"due_date": String("2023-10-13T14:52:21.635709830Z"),
"lent_date": String("2023-10-06T14:52:21.635709093Z"),
"reader_id": String("test-reader-2"),
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-book-1",
sequence: 5,
aggregate_type: "Book",
event_type: "BookEvent",
event_version: "0.1.0",
payload: Object {
"BookReturned": Object {
"due_date": String("2023-10-13T14:52:21.641074341Z"),
"lent_date": String("2023-10-06T14:52:21.641073294Z"),
"reader_id": String("test-reader-1"),
"returned_date": String("2023-10-08T14:52:21.641076051Z"),
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-reader-1",
sequence: 1,
aggregate_type: "Reader",
event_type: "ReaderEvent",
event_version: "0.1.0",
payload: Object {
"ReaderCreated": Object {
"id": String("e5cc5ca2-1d86-41e7-be3b-3354ff4c02f2"),
"name": String("John Smith"),
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-reader-1",
sequence: 2,
aggregate_type: "Reader",
event_type: "ReaderEvent",
event_version: "0.1.0",
payload: Object {
"BookBorrowed": Object {
"book_id": String("test-book-1"),
"borrowed_date": String("2023-10-06T14:52:21.625621265Z"),
"due_date": String("2023-10-13T14:52:21.625630153Z"),
"return_date": Null,
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-reader-1",
sequence: 3,
aggregate_type: "Reader",
event_type: "ReaderEvent",
event_version: "0.1.0",
payload: Object {
"BookReturned": Object {
"book_id": String("test-book-1"),
"borrowed_date": String("2023-10-06T14:52:21.625621265Z"),
"due_date": String("2023-10-13T14:52:21.625630153Z"),
"return_date": String("2023-10-08T14:52:21.638410679Z"),
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-reader-2",
sequence: 1,
aggregate_type: "Reader",
event_type: "ReaderEvent",
event_version: "0.1.0",
payload: Object {
"ReaderCreated": Object {
"id": String("8b513dfb-25dc-4ac4-8455-41dfe18fd2f9"),
"name": String("Mary Ann"),
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-reader-2",
sequence: 2,
aggregate_type: "Reader",
event_type: "ReaderEvent",
event_version: "0.1.0",
payload: Object {
"BookBorrowed": Object {
"book_id": String("test-book-1"),
"borrowed_date": String("2023-10-06T14:52:21.632674289Z"),
"due_date": String("2023-10-13T14:52:21.632675278Z"),
"return_date": Null,
},
},
metadata: Object {},
},
]
查詢snapshot:
let reader_snaps : Vec<SerializedSnapshot> = repo.db
.query("SELECT * FROM snapshots ORDER BY aggregate_id, sequence")
.await?
.take(0)?;
println!("======== [Source of Truth] Snapshots ========\n{:#?}", reader_snaps);
======== [Source of Truth] Snapshots ========
[
SerializedSnapshot {
aggregate_id: "test-book-1",
aggregate: Object {
"copies": Number(2),
"description": String("使用rust,併同cqrs框架,實現event sourcing"),
"id": String("test-book-1"),
"isbn10": String("1234567890"),
"lending_records": Array [
Object {
"due_date": String("2023-10-13T14:54:33.543438667Z"),
"lent_date": String("2023-10-06T14:54:33.543437952Z"),
"reader_id": String("test-reader-2"),
},
],
"lent_history": Array [
Object {
"due_date": String("2023-10-13T14:54:33.549005226Z"),
"lent_date": String("2023-10-06T14:54:33.549004182Z"),
"reader_id": String("test-reader-1"),
"returned_date": String("2023-10-08T14:54:33.549007094Z"),
},
],
"title": String("Rust 語言開發實戰"),
},
current_sequence: 5,
current_snapshot: 5,
},
SerializedSnapshot {
aggregate_id: "test-reader-1",
aggregate: Object {
"borrowed_books": Array [],
"borrowed_history": Array [
Object {
"book_id": String("test-book-1"),
"borrowed_date": String("2023-10-06T14:54:33.533163671Z"),
"due_date": String("2023-10-13T14:54:33.533167117Z"),
"return_date": String("2023-10-06T14:54:33.546738258Z"),
},
],
"id": String("291bdbde-b30a-4fc1-b08b-76878bb53b2b"),
"name": String("John Smith"),
},
current_sequence: 3,
current_snapshot: 3,
},
SerializedSnapshot {
aggregate_id: "test-reader-2",
aggregate: Object {
"borrowed_books": Array [
Object {
"book_id": String("test-book-1"),
"borrowed_date": String("2023-10-06T14:54:33.540181878Z"),
"due_date": String("2023-10-13T14:54:33.540182676Z"),
"return_date": Null,
},
],
"borrowed_history": Array [],
"id": String("88d576ad-516c-4fea-bece-e90d9a5dd41b"),
"name": String("Mary Ann"),
},
current_sequence: 2,
current_snapshot: 2,
},
]
讀取 Query: reader
let reader_dtos: Vec<ReaderDto> = repo.db
.query("SELECT * FROM readers ORDER BY id").await?.take(0)?;
println!("======== [Query] Readers ========\n{:#?}", reader_dtos);
======== [Query] Readers ========
[
ReaderDto {
reader_id: "test-reader-1",
name: "John Smith",
books_borrowed: 0,
books_borrows: [],
next_due_date: None,
},
ReaderDto {
reader_id: "test-reader-2",
name: "Mary Ann",
books_borrowed: 1,
books_borrows: [
ReaderBorrows {
book_id: "test-book-1",
book_name: "Rust 語言開發實戰",
borrowed_date: 2023-10-06T14:56:17.316562653Z,
due_date: 2023-10-13T14:56:17.316563388Z,
},
],
next_due_date: Some(
2023-10-13T14:56:17.316563388Z,
),
},
]
讀取 Query: book
let book_dtos: Vec<BookDto> = repo.db
.query("SELECT * FROM books ORDER BY id").await?.take(0)?;
println!("======== [Query] Book ========\n{:#?}", book_dtos);
======== [Query] Book ========
[
BookDto {
book_id: "test-book-1",
title: "Rust 語言開發實戰",
isbn10: "1234567890",
description: "使用rust,併同cqrs框架,實現event sourcing",
copies: 2,
available_copies: 1,
},
]