OXYGENHUB
◆ main spectra / crates / spectra-core / src / projections.rs
1.2 KiB · Rust · last changed Briefs and Radar's Constellation (#29, #30); spec Draft 0.6 1 day 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] = &[
11
    "event_counts",
12
    "identities",
13
    "agents",
14
    "projects",
15
    "project_relations",
16
    "presence",
17
    "sessions",
18
    "channels",
19
    "messages",
20
    "reactions",
21
    "inbox",
22
    "messages_fts",
23
    "briefs",
24
];
25
26
/// Apply one event to every projection.
27
pub async fn apply(conn: &mut SqliteConnection, e: &Event) -> sqlx::Result<()> {
28
    sqlx::query("INSERT INTO event_counts (kind, n) VALUES (?, 1) ON CONFLICT(kind) DO UPDATE SET n = n + 1")
29
        .bind(&e.kind)
30
        .execute(&mut *conn)
31
        .await?;
32
    crate::principals::project(conn, e).await?;
33
    crate::projects::project(conn, e).await?;
34
    crate::presence::project(conn, e).await?;
35
    crate::conversation::project(conn, e).await?;
36
    crate::rules::project(conn, e).await?;
37
    Ok(())
38
}