feat: anyquery uses count for faster checks

This commit is contained in:
əlemi 2024-06-25 04:05:23 +02:00
parent 934d8ca8ef
commit fde3372bcc
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -1,4 +1,4 @@
use sea_orm::ConnectionTrait;
use sea_orm::{ConnectionTrait, PaginatorTrait};
#[async_trait::async_trait]
@ -7,16 +7,19 @@ pub trait AnyQuery {
}
#[async_trait::async_trait]
impl<T : sea_orm::EntityTrait> AnyQuery for sea_orm::Select<T> {
impl<T : sea_orm::EntityTrait> AnyQuery for sea_orm::Select<T>
where
T::Model : Sync,
{
async fn any(self, db: &impl ConnectionTrait) -> Result<bool, sea_orm::DbErr> {
Ok(self.one(db).await?.is_some())
Ok(self.count(db).await? > 0)
}
}
#[async_trait::async_trait]
impl<T : sea_orm::SelectorTrait + Send> AnyQuery for sea_orm::Selector<T> {
impl<T : sea_orm::SelectorTrait + Send + Sync> AnyQuery for sea_orm::Selector<T> {
async fn any(self, db: &impl ConnectionTrait) -> Result<bool, sea_orm::DbErr> {
Ok(self.one(db).await?.is_some())
Ok(self.count(db).await? > 0)
}
}