From b188469f23a0fc91c111caf0ce389ed6bb0857cf Mon Sep 17 00:00:00 2001 From: Dotan Nahum Date: Mon, 23 Sep 2024 10:31:15 +0300 Subject: [PATCH 1/2] dx: better story for query --- docs-site/config.toml | 14 ++--- src/model/query/dsl/mod.rs | 125 +++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 7 deletions(-) diff --git a/docs-site/config.toml b/docs-site/config.toml index 385444a50..cdf66b2ed 100644 --- a/docs-site/config.toml +++ b/docs-site/config.toml @@ -71,18 +71,16 @@ name = "Models" description = 'Model your business with rich entities and avoid writing SQL, backed by SeaORM. Build relations, validation and custom logic on your entities for the best maintainability.' example = '''```rust impl Model { - pub async fn find_by_email( - db: &DatabaseConnection, - email: &str) + pub async fn find_by_email(db: &DatabaseConnection, email: &str) -> ModelResult { - Users::find().filter( - condition().eq(users::Column::Email, email).build() - ) + + Users::find() + .filter(eq(Column::Email, email)) .one(db).await? .ok_or_else(|| ModelError::EntityNotFound) } - pub async fn create_report(&self, ctx: &AppContext) -> Result<()>{ + pub async fn create_report(&self, ctx: &AppContext) -> Result<()> { ReportWorker::perform_later( &ctx, ReportArgs{ user_id: self.id } @@ -90,6 +88,8 @@ impl Model { } } + + ``` ''' diff --git a/src/model/query/dsl/mod.rs b/src/model/query/dsl/mod.rs index a66558519..998db7921 100644 --- a/src/model/query/dsl/mod.rs +++ b/src/model/query/dsl/mod.rs @@ -1,5 +1,6 @@ use sea_orm::{ sea_query::{IntoCondition, Order}, + sqlx::query, ColumnTrait, Condition, Value, }; use serde::{Deserialize, Serialize}; @@ -45,6 +46,130 @@ pub const fn with(condition: Condition) -> ConditionBuilder { ConditionBuilder { condition } } +/// See [ConditionBuilder::eq] +#[must_use] +pub fn eq>(col: T, value: V) -> ConditionBuilder { + condition().eq(col, value) +} + +/// See [ConditionBuilder::ne] +#[must_use] +pub fn not_equal>(col: T, value: V) -> ConditionBuilder { + condition().ne(col, value) +} + +/// See [ConditionBuilder::gt] +#[must_use] +pub fn gt>(col: T, value: V) -> ConditionBuilder { + condition().gt(col, value) +} + +/// See [ConditionBuilder::gte] +#[must_use] +pub fn gt_equal>(col: T, value: V) -> ConditionBuilder { + condition().gte(col, value) +} + +/// See [ConditionBuilder::lt] +#[must_use] +pub fn lt>(col: T, value: V) -> ConditionBuilder { + condition().lt(col, value) +} + +/// See [ConditionBuilder::lte] +#[must_use] +pub fn lt_equal>(col: T, value: V) -> ConditionBuilder { + condition().lte(col, value) +} + +/// See [ConditionBuilder::between] +#[must_use] +pub fn between>(col: T, a: V, b: V) -> ConditionBuilder { + condition().between(col, a, b) +} + +/// See [ConditionBuilder::not_between] +#[must_use] +pub fn not_between>(col: T, a: V, b: V) -> ConditionBuilder { + condition().not_between(col, a, b) +} + +/// See [ConditionBuilder::like] +#[must_use] +pub fn like>(col: T, a: V) -> ConditionBuilder { + condition().like(col, a) +} + +/// See [ConditionBuilder::not_like] +#[must_use] +pub fn not_like>(col: T, a: V) -> ConditionBuilder { + condition().not_like(col, a) +} + +/// See [ConditionBuilder::starts_with] +#[must_use] +pub fn starts_with>(col: T, a: V) -> ConditionBuilder { + condition().starts_with(col, a) +} + +/// See [ConditionBuilder::ends_with] +#[must_use] +pub fn ends_with>(col: T, a: V) -> ConditionBuilder { + condition().ends_with(col, a) +} + +/// See [ConditionBuilder::contains] +#[must_use] +pub fn contains>(col: T, a: V) -> ConditionBuilder { + condition().contains(col, a) +} + +/// See [ConditionBuilder::is_null] +#[must_use] +#[allow(clippy::wrong_self_convention)] +pub fn is_null(col: T) -> ConditionBuilder { + condition().is_null(col) +} + +/// See [ConditionBuilder::is_not_null] +#[must_use] +#[allow(clippy::wrong_self_convention)] +pub fn is_not_null(col: T) -> ConditionBuilder { + condition().is_not_null(col) +} + +/// See [ConditionBuilder::is_in] +#[must_use] +#[allow(clippy::wrong_self_convention)] +pub fn is_in, I: IntoIterator>( + col: T, + values: I, +) -> ConditionBuilder { + condition().is_in(col, values) +} + +/// See [ConditionBuilder::is_not_in] +#[must_use] +#[allow(clippy::wrong_self_convention)] +pub fn is_not_in, I: IntoIterator>( + col: T, + values: I, +) -> ConditionBuilder { + condition().is_not_in(col, values) +} + +/// See [ConditionBuilder::date_range] +#[must_use] +pub fn date_range(col: T) -> date_range::DateRangeBuilder { + date_range::DateRangeBuilder::new(condition(), col) +} + +impl IntoCondition for ConditionBuilder { + fn into_condition(self) -> Condition { + self.build() + } +} + /// Builder query condition /// /// # Examples From 18612b77fd9a6de86c40b65f1a03da015f3b63fd Mon Sep 17 00:00:00 2001 From: Dotan Nahum Date: Mon, 23 Sep 2024 11:46:16 +0300 Subject: [PATCH 2/2] lint --- src/model/query/dsl/mod.rs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/model/query/dsl/mod.rs b/src/model/query/dsl/mod.rs index 998db7921..d1d48d165 100644 --- a/src/model/query/dsl/mod.rs +++ b/src/model/query/dsl/mod.rs @@ -1,6 +1,5 @@ use sea_orm::{ sea_query::{IntoCondition, Order}, - sqlx::query, ColumnTrait, Condition, Value, }; use serde::{Deserialize, Serialize}; @@ -46,99 +45,99 @@ pub const fn with(condition: Condition) -> ConditionBuilder { ConditionBuilder { condition } } -/// See [ConditionBuilder::eq] +/// See [`ConditionBuilder::eq`] #[must_use] pub fn eq>(col: T, value: V) -> ConditionBuilder { condition().eq(col, value) } -/// See [ConditionBuilder::ne] +/// See [`ConditionBuilder::ne`] #[must_use] pub fn not_equal>(col: T, value: V) -> ConditionBuilder { condition().ne(col, value) } -/// See [ConditionBuilder::gt] +/// See [`ConditionBuilder::gt`] #[must_use] pub fn gt>(col: T, value: V) -> ConditionBuilder { condition().gt(col, value) } -/// See [ConditionBuilder::gte] +/// See [`ConditionBuilder::gte`] #[must_use] pub fn gt_equal>(col: T, value: V) -> ConditionBuilder { condition().gte(col, value) } -/// See [ConditionBuilder::lt] +/// See [`ConditionBuilder::lt`] #[must_use] pub fn lt>(col: T, value: V) -> ConditionBuilder { condition().lt(col, value) } -/// See [ConditionBuilder::lte] +/// See [`ConditionBuilder::lte`] #[must_use] pub fn lt_equal>(col: T, value: V) -> ConditionBuilder { condition().lte(col, value) } -/// See [ConditionBuilder::between] +/// See [`ConditionBuilder::between`] #[must_use] pub fn between>(col: T, a: V, b: V) -> ConditionBuilder { condition().between(col, a, b) } -/// See [ConditionBuilder::not_between] +/// See [`ConditionBuilder::not_between`] #[must_use] pub fn not_between>(col: T, a: V, b: V) -> ConditionBuilder { condition().not_between(col, a, b) } -/// See [ConditionBuilder::like] +/// See [`ConditionBuilder::like`] #[must_use] pub fn like>(col: T, a: V) -> ConditionBuilder { condition().like(col, a) } -/// See [ConditionBuilder::not_like] +/// See [`ConditionBuilder::not_like`] #[must_use] pub fn not_like>(col: T, a: V) -> ConditionBuilder { condition().not_like(col, a) } -/// See [ConditionBuilder::starts_with] +/// See [`ConditionBuilder::starts_with`] #[must_use] pub fn starts_with>(col: T, a: V) -> ConditionBuilder { condition().starts_with(col, a) } -/// See [ConditionBuilder::ends_with] +/// See [`ConditionBuilder::ends_with`] #[must_use] pub fn ends_with>(col: T, a: V) -> ConditionBuilder { condition().ends_with(col, a) } -/// See [ConditionBuilder::contains] +/// See [`ConditionBuilder::contains`] #[must_use] pub fn contains>(col: T, a: V) -> ConditionBuilder { condition().contains(col, a) } -/// See [ConditionBuilder::is_null] +/// See [`ConditionBuilder::is_null`] #[must_use] #[allow(clippy::wrong_self_convention)] pub fn is_null(col: T) -> ConditionBuilder { condition().is_null(col) } -/// See [ConditionBuilder::is_not_null] +/// See [`ConditionBuilder::is_not_null`] #[must_use] #[allow(clippy::wrong_self_convention)] pub fn is_not_null(col: T) -> ConditionBuilder { condition().is_not_null(col) } -/// See [ConditionBuilder::is_in] +/// See [`ConditionBuilder::is_in`] #[must_use] #[allow(clippy::wrong_self_convention)] pub fn is_in, I: IntoIterator>( @@ -148,7 +147,7 @@ pub fn is_in, I: IntoIterator>( condition().is_in(col, values) } -/// See [ConditionBuilder::is_not_in] +/// See [`ConditionBuilder::is_not_in`] #[must_use] #[allow(clippy::wrong_self_convention)] pub fn is_not_in, I: IntoIterator>( @@ -158,7 +157,7 @@ pub fn is_not_in, I: IntoIterator>( condition().is_not_in(col, values) } -/// See [ConditionBuilder::date_range] +/// See [`ConditionBuilder::date_range`] #[must_use] pub fn date_range(col: T) -> date_range::DateRangeBuilder { date_range::DateRangeBuilder::new(condition(), col)