xmtp_db/
traits.rs

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