Skip to content

Commit

Permalink
. update to modql v0.4.0-rc.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychone committed Mar 6, 2024
1 parent f4b7aaa commit 5809d20
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 31 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ members = [
# -- Serde
serde_with = {version = "3", features = ["time_0_3"] }
# -- Data
modql = { version = "0.3.7", features = ["with-sea-query"]}
derive_more = {version = "1.0.0-beta", features = ["from", "display"] }
modql = { version = "0.4.0-rc", features = ["with-sea-query"]}
# -- Others
derive_more = {version = "1.0.0-beta", features = ["from", "display"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ This update ([GitHub tag: E06](https://github.com/rust10x/rust-web-app/releases/
# Start postgresql server docker image:
docker run --rm --name pg -p 5432:5432 \
-e POSTGRES_PASSWORD=welcome \
postgres:15
postgres:16

# (optional) To have a psql terminal on pg.
# In another terminal (tab) run psql:
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/lib-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ serde_json = "1"
serde_with = { workspace = true }
# -- Data
sqlx = { version = "0.7", features = [ "macros", "runtime-tokio", "postgres", "uuid" ] }
sea-query = "0.30"
sea-query-binder = { version = "0.5", features = ["sqlx-postgres", "with-uuid", "with-time" ] }
sea-query = "0.31.0-rc"
sea-query-binder = { version = "0.6.0-rc", features = ["sqlx-postgres", "with-uuid", "with-time" ] }
modql = { workspace = true }
# -- Tracing
tracing = "0.1"
Expand Down
20 changes: 10 additions & 10 deletions crates/libs/lib-core/src/model/base/crud_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::model::base::{
};
use crate::model::ModelManager;
use crate::model::{Error, Result};
use modql::field::HasFields;
use modql::field::HasSeaFields;
use modql::filter::{FilterGroups, ListOptions};
use sea_query::{Condition, Expr, PostgresQueryBuilder, Query};
use sea_query_binder::SqlxBinder;
Expand All @@ -15,12 +15,12 @@ use sqlx::FromRow;
pub async fn create<MC, E>(ctx: &Ctx, mm: &ModelManager, data: E) -> Result<i64>
where
MC: DbBmc,
E: HasFields,
E: HasSeaFields,
{
let user_id = ctx.user_id();

// -- Extract fields (name / sea-query value expression)
let mut fields = data.not_none_fields();
let mut fields = data.not_none_sea_fields();
prep_fields_for_create::<MC>(&mut fields, user_id);

// -- Build query
Expand All @@ -46,13 +46,13 @@ pub async fn get<MC, E>(_ctx: &Ctx, mm: &ModelManager, id: i64) -> Result<E>
where
MC: DbBmc,
E: for<'r> FromRow<'r, PgRow> + Unpin + Send,
E: HasFields,
E: HasSeaFields,
{
// -- Build query
let mut query = Query::select();
query
.from(MC::table_ref())
.columns(E::field_column_refs())
.columns(E::sea_column_refs())
.and_where(Expr::col(CommonIden::Id).eq(id));

// -- Exec query
Expand Down Expand Up @@ -80,7 +80,7 @@ where
MC: DbBmc,
F: Into<FilterGroups>,
E: for<'r> FromRow<'r, PgRow> + Unpin + Send,
E: HasFields,
E: HasSeaFields,
{
let list_options = match list_options {
Some(mut list_options) => {
Expand Down Expand Up @@ -117,11 +117,11 @@ where
MC: DbBmc,
F: Into<FilterGroups>,
E: for<'r> FromRow<'r, PgRow> + Unpin + Send,
E: HasFields,
E: HasSeaFields,
{
// -- Build the query
let mut query = Query::select();
query.from(MC::table_ref()).columns(E::field_column_refs());
query.from(MC::table_ref()).columns(E::sea_column_refs());

// condition from filter
if let Some(filter) = filter {
Expand Down Expand Up @@ -150,10 +150,10 @@ pub async fn update<MC, E>(
) -> Result<()>
where
MC: DbBmc,
E: HasFields,
E: HasSeaFields,
{
// -- Prep Fields
let mut fields = data.not_none_fields();
let mut fields = data.not_none_sea_fields();
prep_fields_for_update::<MC>(&mut fields, ctx.user_id());

// -- Build query
Expand Down
36 changes: 24 additions & 12 deletions crates/libs/lib-core/src/model/base/utils.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
use crate::model::base::{CommonIden, DbBmc, TimestampIden};
use lib_utils::time::now_utc;
use modql::field::{Field, Fields};
use modql::field::{SeaField, SeaFields};
use sea_query::IntoIden;

/// This method must be called when a model controller intends to create its entity.
pub fn prep_fields_for_create<MC>(fields: &mut Fields, user_id: i64)
pub fn prep_fields_for_create<MC>(fields: &mut SeaFields, user_id: i64)
where
MC: DbBmc,
{
if MC::has_owner_id() {
fields.push(Field::new(CommonIden::OwnerId.into_iden(), user_id.into()));
fields.push(SeaField::new(
CommonIden::OwnerId.into_iden(),
user_id.into(),
));
}
if MC::has_timestamps() {
add_timestamps_for_create(fields, user_id);
}
}

/// This method must be calledwhen a Model Controller plans to update its entity.
pub fn prep_fields_for_update<MC>(fields: &mut Fields, user_id: i64)
pub fn prep_fields_for_update<MC>(fields: &mut SeaFields, user_id: i64)
where
MC: DbBmc,
{
Expand All @@ -28,19 +31,28 @@ where

/// Update the timestamps info for create
/// (e.g., cid, ctime, and mid, mtime will be updated with the same values)
fn add_timestamps_for_create(fields: &mut Fields, user_id: i64) {
fn add_timestamps_for_create(fields: &mut SeaFields, user_id: i64) {
let now = now_utc();
fields.push(Field::new(TimestampIden::Cid.into_iden(), user_id.into()));
fields.push(Field::new(TimestampIden::Ctime.into_iden(), now.into()));
fields.push(SeaField::new(
TimestampIden::Cid.into_iden(),
user_id.into(),
));
fields.push(SeaField::new(TimestampIden::Ctime.into_iden(), now.into()));

fields.push(Field::new(TimestampIden::Mid.into_iden(), user_id.into()));
fields.push(Field::new(TimestampIden::Mtime.into_iden(), now.into()));
fields.push(SeaField::new(
TimestampIden::Mid.into_iden(),
user_id.into(),
));
fields.push(SeaField::new(TimestampIden::Mtime.into_iden(), now.into()));
}

/// Update the timestamps info only for update.
/// (.e.g., only mid, mtime will be udpated)
fn add_timestamps_for_update(fields: &mut Fields, user_id: i64) {
fn add_timestamps_for_update(fields: &mut SeaFields, user_id: i64) {
let now = now_utc();
fields.push(Field::new(TimestampIden::Mid.into_iden(), user_id.into()));
fields.push(Field::new(TimestampIden::Mtime.into_iden(), now.into()));
fields.push(SeaField::new(
TimestampIden::Mid.into_iden(),
user_id.into(),
));
fields.push(SeaField::new(TimestampIden::Mtime.into_iden(), now.into()));
}
9 changes: 5 additions & 4 deletions crates/libs/lib-core/src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::model::modql_utils::time_to_sea_value;
use crate::model::ModelManager;
use crate::model::{Error, Result};
use lib_auth::pwd::{self, ContentToHash};
use modql::field::{Field, Fields, HasFields};
use modql::field::{Fields, HasSeaFields, SeaField, SeaFields};
use modql::filter::{
FilterNodes, ListOptions, OpValsInt64, OpValsString, OpValsValue,
};
Expand Down Expand Up @@ -67,7 +67,7 @@ pub struct UserForAuth {
}

/// Marker trait
pub trait UserBy: HasFields + for<'r> FromRow<'r, PgRow> + Unpin + Send {}
pub trait UserBy: HasSeaFields + for<'r> FromRow<'r, PgRow> + Unpin + Send {}

impl UserBy for User {}
impl UserBy for UserForLogin {}
Expand Down Expand Up @@ -171,7 +171,7 @@ impl UserBmc {
let mut query = Query::select();
query
.from(Self::table_ref())
.columns(E::field_idens())
.columns(E::sea_idens())
.and_where(Expr::col(UserIden::Username).eq(username));

// -- Execute query
Expand Down Expand Up @@ -207,7 +207,8 @@ impl UserBmc {
.await?;

// -- Prep the data
let mut fields = Fields::new(vec![Field::new(UserIden::Pwd, pwd.into())]);
let mut fields =
SeaFields::new(vec![SeaField::new(UserIden::Pwd, pwd.into())]);
prep_fields_for_update::<Self>(&mut fields, ctx.user_id());

// -- Build query
Expand Down

0 comments on commit 5809d20

Please sign in to comment.