OXYGENHUB
# 5d695d869fbf spectra / crates / spectra-core / src / projections.rs
835 B · Rust · last changed Identities instead of tokens: init, owned agents as delegates, verified replay of identities, request authentication, one writer (#4) 2 days ago source blame history raw
1
//! Projections (spec §9.3): tables kept from the ledger in the same
2
//! transaction as each append, and rebuilt from it by `spectra admin
3
//! rebuild`. Every table here must be a function of the events alone; the
4
//! test harness and `spectra admin check` hold them to it.
5
6
use crate::event::Event;
7
use sqlx::SqliteConnection;
8
9
/// The projection tables, in the order they are cleared and compared.
10
pub const TABLES: &[&str] = &["event_counts", "identities", "agents"];
11
12
/// Apply one event to every projection.
13
pub async fn apply(conn: &mut SqliteConnection, e: &Event) -> sqlx::Result<()> {
14
    sqlx::query("INSERT INTO event_counts (kind, n) VALUES (?, 1) ON CONFLICT(kind) DO UPDATE SET n = n + 1")
15
        .bind(&e.kind)
16
        .execute(&mut *conn)
17
        .await?;
18
    crate::principals::project(conn, e).await?;
19
    Ok(())
20
}