forked from
microcosm.blue/microcosm-rs
Constellation, Spacedust, Slingshot, UFOs: atproto crates and services for microcosm
1use rusqlite::{Connection, OptionalExtension, Result};
2use std::path::Path;
3
4pub struct Storage {
5 con: Connection,
6}
7
8impl Storage {
9 pub fn connect(path: impl AsRef<Path>) -> Result<Self> {
10 let con = Connection::open(path)?;
11 con.pragma_update(None, "journal_mode", "WAL")?;
12 con.pragma_update(None, "synchronous", "NORMAL")?;
13 con.pragma_update(None, "busy_timeout", "100")?;
14 con.pragma_update(None, "foreign_keys", "ON")?;
15 Ok(Self { con })
16 }
17 pub fn init(path: impl AsRef<Path>) -> Result<Self> {
18 let me = Self::connect(path)?;
19 me.con.execute(
20 r#"
21 create table prefs (
22 actor text not null,
23 aud text not null,
24 pref text not null,
25 primary key (actor, aud)
26 ) strict"#,
27 (),
28 )?;
29 Ok(me)
30 }
31 pub fn put(&self, actor: &str, aud: &str, pref: &str) -> Result<()> {
32 self.con.execute(
33 r#"insert into prefs (actor, aud, pref)
34 values (?1, ?2, ?3)
35 on conflict do update set pref = excluded.pref"#,
36 [actor, aud, pref],
37 )?;
38 Ok(())
39 }
40 pub fn get(&self, actor: &str, aud: &str) -> Result<Option<String>> {
41 self.con
42 .query_one(
43 r#"select pref from prefs
44 where actor = ?1 and aud = ?2"#,
45 [actor, aud],
46 |row| row.get(0),
47 )
48 .optional()
49 }
50}