scufflecloud_core_traits/
dataloader.rs

1use std::collections::HashMap;
2
3use core_db_types::models::{Organization, OrganizationId, OrganizationMember, User, UserId};
4use scuffle_batching::DataLoaderFetcher;
5
6pub trait DataloaderInterface {
7    fn user_loader(
8        &self,
9    ) -> &scuffle_batching::DataLoader<impl DataLoaderFetcher<Key = UserId, Value = User> + Send + Sync + 'static>;
10    fn organization_loader(
11        &self,
12    ) -> &scuffle_batching::DataLoader<
13        impl DataLoaderFetcher<Key = OrganizationId, Value = Organization> + Send + Sync + 'static,
14    >;
15    fn organization_member_by_user_id_loader(
16        &self,
17    ) -> &scuffle_batching::DataLoader<
18        impl DataLoaderFetcher<Key = UserId, Value = Vec<OrganizationMember>> + Send + Sync + 'static,
19    >;
20}
21
22pub trait DataLoader {
23    type Key;
24    type Value;
25    type Error;
26
27    fn load(&self, key: Self::Key) -> impl Future<Output = Result<Option<Self::Value>, Self::Error>>;
28    fn load_many(
29        &self,
30        keys: impl IntoIterator<Item = Self::Key> + Send,
31    ) -> impl Future<Output = Result<HashMap<Self::Key, Self::Value>, Self::Error>>;
32}
33
34impl<E> DataLoader for scuffle_batching::DataLoader<E>
35where
36    E: scuffle_batching::DataLoaderFetcher + Send + Sync + 'static,
37{
38    type Error = ();
39    type Key = E::Key;
40    type Value = E::Value;
41
42    async fn load(&self, key: Self::Key) -> Result<Option<Self::Value>, Self::Error> {
43        scuffle_batching::DataLoader::load(self, key).await
44    }
45
46    async fn load_many(
47        &self,
48        keys: impl IntoIterator<Item = Self::Key> + Send,
49    ) -> Result<HashMap<Self::Key, Self::Value>, Self::Error> {
50        scuffle_batching::DataLoader::load_many(self, keys).await
51    }
52}