Skip to main content

xmtp_db/encrypted_store/
store.rs

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