use batpak::prelude::*;
use batpak::store::{BatchAppendItem, CausationRef};
#[derive(serde::Serialize, serde::Deserialize, EventPayload)]
#[batpak(category = 1, type_id = 1)]
struct ChatSent {
text: String,
}
#[derive(serde::Serialize, serde::Deserialize, EventPayload)]
#[batpak(category = 2, type_id = 1)]
struct AuditLogged {
action: String,
participants: u32,
}
#[allow(clippy::print_stdout)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let dir = tempfile::tempdir()?;
let config = StoreConfig::new(dir.path())
.with_sync_every_n_events(25)
.with_sync_mode(SyncMode::SyncData)
.with_batch_max_bytes(1024 * 1024); let store = Store::open(config)?;
let items = vec![
BatchAppendItem::typed(
Coordinate::new("user:alice", "chat:general")?,
&ChatSent {
text: "Hello everyone!".into(),
},
AppendOptions::default(),
CausationRef::None,
)?,
BatchAppendItem::typed(
Coordinate::new("user:bob", "chat:general")?,
&ChatSent {
text: "Hi Alice!".into(),
},
AppendOptions::default(),
CausationRef::PriorItem(0),
)?,
BatchAppendItem::typed(
Coordinate::new("system:audit", "chat:general")?,
&AuditLogged {
action: "message_exchange".into(),
participants: 2,
},
AppendOptions::default(),
CausationRef::PriorItem(1),
)?,
];
let receipts = store.append_batch(items)?;
println!("batch committed: {} events", receipts.len());
for (i, receipt) in receipts.iter().enumerate() {
let fetched = store.get(receipt.event_id)?;
println!(
" [{}] event_id={} seq={} entity={}",
i,
receipt.event_id,
receipt.sequence,
fetched.coordinate.entity(),
);
}
let alice_events = store.query(&Region::entity("user:alice"));
println!("\nalice has {} event(s)", alice_events.len());
Ok(())
}