Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T04:30:43.340Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

persistence.rs

60 lines · 2.0 KB · rust
1use anyhow::Result;
2use db::{
3    query,
4    sqlez::{domain::Domain, statement::Statement, thread_safe_connection::ThreadSafeConnection},
5    sqlez_macros::sql,
6};
7use workspace::{ItemId, WorkspaceDb, WorkspaceId};
8
9pub struct ComponentPreviewDb(ThreadSafeConnection);
10
11impl Domain for ComponentPreviewDb {
12    const NAME: &str = stringify!(ComponentPreviewDb);
13
14    const MIGRATIONS: &[&str] = &[sql!(
15            CREATE TABLE component_previews (
16                workspace_id INTEGER,
17                item_id INTEGER UNIQUE,
18                active_page_id TEXT,
19                PRIMARY KEY(workspace_id, item_id),
20                FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
21                ON DELETE CASCADE
22            ) STRICT;
23    )];
24}
25
26db::static_connection!(ComponentPreviewDb, [WorkspaceDb]);
27
28impl ComponentPreviewDb {
29    pub async fn save_active_page(
30        &self,
31        item_id: ItemId,
32        workspace_id: WorkspaceId,
33        active_page_id: String,
34    ) -> Result<()> {
35        log::debug!(
36            "Saving active page: item_id={item_id:?}, workspace_id={workspace_id:?}, active_page_id={active_page_id}"
37        );
38        let query = "INSERT INTO component_previews(item_id, workspace_id, active_page_id)
39            VALUES (?1, ?2, ?3)
40            ON CONFLICT DO UPDATE SET
41                active_page_id = ?3";
42        self.write(move |conn| {
43            let mut statement = Statement::prepare(conn, query)?;
44            let mut next_index = statement.bind(&item_id, 1)?;
45            next_index = statement.bind(&workspace_id, next_index)?;
46            statement.bind(&active_page_id, next_index)?;
47            statement.exec()
48        })
49        .await
50    }
51
52    query! {
53        pub fn get_active_page(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<String>> {
54            SELECT active_page_id
55            FROM component_previews
56            WHERE item_id = ? AND workspace_id = ?
57        }
58    }
59}
60
Served at tenant.openagents/omega Member data and write actions are omitted.