xmtp_db/encrypted_store/
store.rs

1use super::*;
2use derive_builder::Builder;
3
4/// Manages a Sqlite db for persisting messages and other objects.
5#[derive(Clone, Debug, Builder)]
6#[builder(setter(into))]
7pub struct EncryptedMessageStore<Db> {
8    pub(super) db: Db,
9}
10
11impl<Db> EncryptedMessageStore<Db> {
12    pub fn builder() -> EncryptedMessageStoreBuilder<Db>
13    where
14        Db: Clone,
15    {
16        Default::default()
17    }
18}
19
20impl<Db: XmtpDb> EncryptedMessageStore<Db> {
21    pub fn new(db: Db) -> Result<Self, StorageError> {
22        db.init()?;
23        Ok(Self { db })
24    }
25
26    pub fn new_uninit(db: Db) -> Result<Self, StorageError> {
27        Ok(Self { db })
28    }
29
30    pub fn opts(&self) -> &StorageOption {
31        self.db.opts()
32    }
33}
34
35impl<Db> EncryptedMessageStore<Db>
36where
37    Db: XmtpDb,
38{
39    /// Access to the database queries defined on connections
40    pub fn db(&self) -> <Db as XmtpDb>::DbQuery {
41        self.db.db()
42    }
43
44    /// Pulls a new connection from the store
45    pub fn conn(&self) -> Db::Connection {
46        self.db.conn()
47    }
48
49    /// Release connection to the database, closing it
50    pub fn release_connection(&self) -> Result<(), ConnectionError> {
51        self.db.disconnect()
52    }
53
54    /// Reconnect to the database
55    pub fn reconnect(&self) -> Result<(), ConnectionError> {
56        self.db.reconnect()
57    }
58}
59
60impl<Db> XmtpDb for EncryptedMessageStore<Db>
61where
62    Db: XmtpDb,
63{
64    type Connection = Db::Connection;
65    type DbQuery = Db::DbQuery;
66
67    fn conn(&self) -> Self::Connection {
68        self.db.conn()
69    }
70
71    fn db(&self) -> Self::DbQuery {
72        self.db.db()
73    }
74
75    fn validate(&self, conn: &mut SqliteConnection) -> Result<(), ConnectionError> {
76        self.db.validate(conn)
77    }
78
79    fn opts(&self) -> &StorageOption {
80        self.db.opts()
81    }
82
83    fn reconnect(&self) -> Result<(), crate::ConnectionError> {
84        self.db.reconnect()
85    }
86
87    fn disconnect(&self) -> Result<(), crate::ConnectionError> {
88        self.db.disconnect()
89    }
90}