Crate deque_loader[−][src]
Expand description
use async_graphql::SimpleObject; use deque_loader::diesel::{DieselError, DieselLoader}; use db::schema::users; use diesel::prelude::*; use diesel_connection::PooledConnection; use std::{collections::HashMap, sync::Arc}; derive_id! { #[derive(Identifiable)] #[table_name = "users"] #[graphql(name = "UserID")] pub struct UserId( #[column_name = "id"] i32); } #[derive(SimpleObject, Identifiable, Associations, Queryable, Debug, Clone)] #[belongs_to(UserId, foreign_key = "id")] #[table_name = "users"] pub struct User { pub id: i32, pub name: String, } pub struct UserLoader; impl DieselLoader for UserLoader { type Key = UserId; type Value = User; fn load( conn: PooledConnection, keys: Vec<UserId>, ) -> Result<HashMap<Self::Key, Arc<Self::Value>>, DieselError> { let users: Vec<User> = User::belonging_to(&keys) .select(users::all_columns) .load::<User>(&conn)?; let users = users.into_iter().map(Arc::new); let mut data: HashMap<UserId, Arc<User>> = HashMap::new(); data.extend(keys.into_iter().zip(users)); Ok(data) } } define_diesel_loader!(UserLoader); attach_loader!(User, UserLoader);
Modules
Macros
Implements Loadable
using the current thread local [DataLoader
]
Setup thread local [DataLoader
] instances using a BatchLoader
to define the [TaskHandler
]
Register cache factory for a TaskHandler
using inventory
. This will require adding inventory
to your dependencies as well.
Setup thread local [DataLoader
] instances using a DieselLoader
to define the [TaskHandler
]
Thread local [DataLoader
] instances grouped into worker groups and statically pre-allocated per core as to be lock free
Traits
Params to crate::Loadable::load_by
; typically i32
or newtype wrapper
Loadable types load using the corresponding static loader associated by type via define_static_loader