43 lines
1.3 KiB
Rust
43 lines
1.3 KiB
Rust
use async_trait::async_trait;
|
|
use dataloader::non_cached::Loader;
|
|
use dataloader::BatchFn;
|
|
use diesel::prelude::*;
|
|
use std::collections::HashMap;
|
|
use uuidv7::Uuid;
|
|
|
|
use crate::{api::models, db};
|
|
|
|
pub struct RepositoryBatcher;
|
|
|
|
#[async_trait]
|
|
impl BatchFn<Uuid, models::repository::Repository> for RepositoryBatcher {
|
|
async fn load(&mut self, keys: &[Uuid]) -> HashMap<Uuid, models::repository::Repository> {
|
|
let db_conn = &mut db::POOL.get().unwrap();
|
|
let mut map = HashMap::new();
|
|
for row in db::schema::repositories::table
|
|
.select((
|
|
db::schema::repositories::id,
|
|
db::schema::repositories::user_id,
|
|
db::schema::repositories::name,
|
|
))
|
|
.filter(db::schema::repositories::id.eq_any(keys))
|
|
.load::<(Uuid, Uuid, String)>(db_conn)
|
|
.unwrap()
|
|
{
|
|
let row: (Uuid, Uuid, String) = row;
|
|
let data = models::repository::Repository {
|
|
id: row.0,
|
|
user_id: row.1,
|
|
name: row.2,
|
|
};
|
|
map.insert(data.id, data);
|
|
}
|
|
|
|
map
|
|
}
|
|
}
|
|
|
|
pub type RepositoryLoader = Loader<Uuid, models::repository::Repository, RepositoryBatcher>;
|
|
|
|
pub const YIELD_COUNT: usize = 100;
|