Skip to main content

xmtp_db/
traits.rs

1use crate::ConnectionExt;
2use crate::StorageError;
3use crate::association_state::QueryAssociationStateCache;
4use crate::message_deletion::QueryMessageDeletion;
5use crate::pending_remove::QueryPendingRemove;
6use crate::prelude::*;
7use crate::readd_status::QueryReaddStatus;
8use xmtp_common::{MaybeSend, MaybeSync};
9
10/// Get an MLS Key store in the context of a transaction
11/// this must only be used within transactions.
12#[cfg_attr(any(feature = "test-utils", test), mockall::automock(type Store = crate::sql_key_store::mock::MockSqlKeyStore;))]
13pub trait TransactionalKeyStore {
14    type Store<'a>: XmtpMlsStorageProvider
15    where
16        Self: 'a;
17
18    fn key_store<'a>(&'a mut self) -> Self::Store<'a>;
19}
20
21/// Inserts a model to the underlying data store, erroring if it already exists
22pub trait Store<StorageConnection> {
23    type Output;
24    fn store(&self, into: &StorageConnection) -> Result<Self::Output, StorageError>;
25}
26
27/// Inserts a model to the underlying data store, silent no-op on unique constraint violations
28pub trait StoreOrIgnore<StorageConnection> {
29    type Output;
30    fn store_or_ignore(&self, into: &StorageConnection) -> Result<Self::Output, StorageError>;
31}
32
33/// Fetches a model from the underlying data store, returning None if it does not exist
34pub trait Fetch<Model> {
35    type Key;
36    fn fetch(&self, key: &Self::Key) -> Result<Option<Model>, StorageError>;
37}
38
39/// Fetches all instances of `Model` from the data store.
40/// Returns an empty list if no items are found or an error if the fetch fails.
41pub trait FetchList<Model> {
42    fn fetch_list(&self) -> Result<Vec<Model>, StorageError>;
43}
44
45/// Fetches a filtered list of `Model` instances matching the specified key.
46/// Logs an error and returns an empty list if no items are found or if an error occurs.
47///
48/// # Parameters
49/// - `key`: The key used to filter the items in the data store.
50pub trait FetchListWithKey<Model> {
51    type Key;
52    fn fetch_list_with_key(&self, keys: &[Self::Key]) -> Result<Vec<Model>, StorageError>;
53}
54
55/// Deletes a model from the underlying data store
56pub trait Delete<Model> {
57    type Key;
58    fn delete(&self, key: Self::Key) -> Result<usize, StorageError>;
59}
60
61pub trait IntoConnection {
62    type Connection: ConnectionExt;
63    fn into_connection(self) -> Self::Connection;
64}
65
66pub trait DbQuery:
67    MaybeSend
68    + MaybeSync
69    + ReadOnly
70    + QueryConsentRecord
71    + QueryConversationList
72    + QueryDms
73    + QueryGroup
74    + QueryGroupVersion
75    + QueryGroupIntent
76    + QueryGroupMessage
77    + QueryIdentity
78    + QueryIdentityCache
79    + QueryKeyPackageHistory
80    + QueryKeyStoreEntry
81    + QueryDeviceSyncMessages
82    + QueryRefreshState
83    + QueryIdentityUpdates
84    + QueryIncomingEnvelope
85    + QueryDelivery
86    + QueryPreparedEnvelope
87    + QueryLocalCommitLog
88    + QueryRemoteCommitLog
89    + QueryServerConfiguration
90    + QueryAssociationStateCache
91    + QueryReaddStatus
92    + QueryTasks
93    + QueryNotifications
94    + QueryPendingRemove
95    + QueryMessageDeletion
96    + Pragmas
97    + crate::ConnectionExt
98{
99}
100
101impl<T> DbQuery for T where
102    T: MaybeSend
103        + MaybeSync
104        + ReadOnly
105        + QueryConsentRecord
106        + QueryConversationList
107        + QueryDms
108        + QueryGroup
109        + QueryGroupVersion
110        + QueryGroupIntent
111        + QueryGroupMessage
112        + QueryIdentity
113        + QueryIdentityCache
114        + QueryKeyPackageHistory
115        + QueryKeyStoreEntry
116        + QueryDeviceSyncMessages
117        + QueryRefreshState
118        + QueryIdentityUpdates
119        + QueryIncomingEnvelope
120        + QueryDelivery
121        + QueryPreparedEnvelope
122        + QueryLocalCommitLog
123        + QueryRemoteCommitLog
124        + QueryServerConfiguration
125        + QueryAssociationStateCache
126        + QueryReaddStatus
127        + QueryTasks
128        + QueryNotifications
129        + QueryPendingRemove
130        + QueryMessageDeletion
131        + Pragmas
132        + crate::ConnectionExt
133{
134}
135
136pub use crate::xmtp_openmls_provider::XmtpMlsStorageProvider;