Implement LoaderTrait for slices (#1368)

This commit is contained in:
Billy Chan 2023-01-11 19:12:45 +08:00 committed by GitHub
parent 2a713b06cd
commit a0d972633a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,8 +32,35 @@ pub trait LoaderTrait {
#[async_trait] #[async_trait]
impl<M> LoaderTrait for Vec<M> impl<M> LoaderTrait for Vec<M>
where where
M: ModelTrait, M: ModelTrait + Sync,
Vec<M>: Sync, {
type Model = M;
async fn load_one<R, C>(&self, stmt: Select<R>, db: &C) -> Result<Vec<Option<R::Model>>, DbErr>
where
C: ConnectionTrait,
R: EntityTrait,
R::Model: Send + Sync,
<<Self as LoaderTrait>::Model as ModelTrait>::Entity: Related<R>,
{
self.as_slice().load_one(stmt, db).await
}
async fn load_many<R, C>(&self, stmt: Select<R>, db: &C) -> Result<Vec<Vec<R::Model>>, DbErr>
where
C: ConnectionTrait,
R: EntityTrait,
R::Model: Send + Sync,
<<Self as LoaderTrait>::Model as ModelTrait>::Entity: Related<R>,
{
self.as_slice().load_many(stmt, db).await
}
}
#[async_trait]
impl<M> LoaderTrait for &[M]
where
M: ModelTrait + Sync,
{ {
type Model = M; type Model = M;