Skip to content

Commit

Permalink
dx: better story for query
Browse files Browse the repository at this point in the history
  • Loading branch information
jondot committed Sep 23, 2024
1 parent 7e7ab89 commit b188469
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 7 deletions.
14 changes: 7 additions & 7 deletions docs-site/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,25 @@ 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<Self> {
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 }
).await?;
}
}
```
'''

Expand Down
125 changes: 125 additions & 0 deletions src/model/query/dsl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use sea_orm::{
sea_query::{IntoCondition, Order},
sqlx::query,
ColumnTrait, Condition, Value,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -45,6 +46,130 @@ pub const fn with(condition: Condition) -> ConditionBuilder {
ConditionBuilder { condition }
}

/// See [ConditionBuilder::eq]
#[must_use]
pub fn eq<T: ColumnTrait, V: Into<Value>>(col: T, value: V) -> ConditionBuilder {
condition().eq(col, value)
}

/// See [ConditionBuilder::ne]
#[must_use]
pub fn not_equal<T: ColumnTrait, V: Into<Value>>(col: T, value: V) -> ConditionBuilder {
condition().ne(col, value)
}

/// See [ConditionBuilder::gt]
#[must_use]
pub fn gt<T: ColumnTrait, V: Into<Value>>(col: T, value: V) -> ConditionBuilder {
condition().gt(col, value)
}

/// See [ConditionBuilder::gte]
#[must_use]
pub fn gt_equal<T: ColumnTrait, V: Into<Value>>(col: T, value: V) -> ConditionBuilder {
condition().gte(col, value)
}

/// See [ConditionBuilder::lt]
#[must_use]
pub fn lt<T: ColumnTrait, V: Into<Value>>(col: T, value: V) -> ConditionBuilder {
condition().lt(col, value)
}

/// See [ConditionBuilder::lte]
#[must_use]
pub fn lt_equal<T: ColumnTrait, V: Into<Value>>(col: T, value: V) -> ConditionBuilder {
condition().lte(col, value)
}

/// See [ConditionBuilder::between]
#[must_use]
pub fn between<T: ColumnTrait, V: Into<Value>>(col: T, a: V, b: V) -> ConditionBuilder {
condition().between(col, a, b)
}

/// See [ConditionBuilder::not_between]
#[must_use]
pub fn not_between<T: ColumnTrait, V: Into<Value>>(col: T, a: V, b: V) -> ConditionBuilder {
condition().not_between(col, a, b)
}

/// See [ConditionBuilder::like]
#[must_use]
pub fn like<T: ColumnTrait, V: Into<String>>(col: T, a: V) -> ConditionBuilder {
condition().like(col, a)
}

/// See [ConditionBuilder::not_like]
#[must_use]
pub fn not_like<T: ColumnTrait, V: Into<String>>(col: T, a: V) -> ConditionBuilder {
condition().not_like(col, a)
}

/// See [ConditionBuilder::starts_with]
#[must_use]
pub fn starts_with<T: ColumnTrait, V: Into<String>>(col: T, a: V) -> ConditionBuilder {
condition().starts_with(col, a)
}

/// See [ConditionBuilder::ends_with]
#[must_use]
pub fn ends_with<T: ColumnTrait, V: Into<String>>(col: T, a: V) -> ConditionBuilder {
condition().ends_with(col, a)
}

/// See [ConditionBuilder::contains]
#[must_use]
pub fn contains<T: ColumnTrait, V: Into<String>>(col: T, a: V) -> ConditionBuilder {
condition().contains(col, a)
}

/// See [ConditionBuilder::is_null]
#[must_use]
#[allow(clippy::wrong_self_convention)]
pub fn is_null<T: ColumnTrait>(col: T) -> ConditionBuilder {
condition().is_null(col)
}

/// See [ConditionBuilder::is_not_null]
#[must_use]
#[allow(clippy::wrong_self_convention)]
pub fn is_not_null<T: ColumnTrait>(col: T) -> ConditionBuilder {
condition().is_not_null(col)
}

/// See [ConditionBuilder::is_in]
#[must_use]
#[allow(clippy::wrong_self_convention)]
pub fn is_in<T: ColumnTrait, V: Into<Value>, I: IntoIterator<Item = V>>(
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<T: ColumnTrait, V: Into<Value>, I: IntoIterator<Item = V>>(
col: T,
values: I,
) -> ConditionBuilder {
condition().is_not_in(col, values)
}

/// See [ConditionBuilder::date_range]
#[must_use]
pub fn date_range<T: ColumnTrait>(col: T) -> date_range::DateRangeBuilder<T> {
date_range::DateRangeBuilder::new(condition(), col)
}

impl IntoCondition for ConditionBuilder {
fn into_condition(self) -> Condition {
self.build()
}
}

/// Builder query condition
///
/// # Examples
Expand Down

0 comments on commit b188469

Please sign in to comment.