From cace6eb13542a2e4ba9115c6fd026d581c1751c2 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 9 May 2021 10:10:17 +0200 Subject: [PATCH] Add sqlx as conditional dependency to the model That way, we can use query_as --- model/Cargo.toml | 3 +++ model/src/lib.rs | 12 ++++++------ src/domain/handler.rs | 10 +--------- src/domain/sql_tables.rs | 1 - 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/model/Cargo.toml b/model/Cargo.toml index 9df159b..f4528de 100644 --- a/model/Cargo.toml +++ b/model/Cargo.toml @@ -10,3 +10,6 @@ js = [] [dependencies] chrono = "*" serde = "*" + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +sqlx = { version = "0.5", features = [ "runtime-actix-native-tls", "any", "sqlite", "mysql", "postgres", "macros" , "chrono"] } diff --git a/model/src/lib.rs b/model/src/lib.rs index 64e4443..ba87c7b 100644 --- a/model/src/lib.rs +++ b/model/src/lib.rs @@ -1,11 +1,10 @@ -#[cfg_attr(test, derive(PartialEq, Eq, Debug))] +#[derive(PartialEq, Eq, Debug)] pub struct BindRequest { pub name: String, pub password: String, } -#[derive(PartialEq, Eq)] -#[cfg_attr(test, derive(Debug))] +#[derive(PartialEq, Eq, Debug)] pub enum RequestFilter { And(Vec), Or(Vec), @@ -13,12 +12,13 @@ pub enum RequestFilter { Equality(String, String), } -#[cfg_attr(test, derive(PartialEq, Eq, Debug))] +#[derive(PartialEq, Eq, Debug)] pub struct ListUsersRequest { pub filters: Option, } -#[cfg_attr(test, derive(PartialEq, Eq, Debug))] +#[derive(PartialEq, Eq, Debug)] +#[cfg_attr(not(target_arch = "wasm32"), derive(sqlx::FromRow))] pub struct User { pub user_id: String, pub email: String, @@ -29,7 +29,7 @@ pub struct User { pub creation_date: chrono::NaiveDateTime, } -#[cfg_attr(test, derive(PartialEq, Eq, Debug))] +#[derive(PartialEq, Eq, Debug)] pub struct Group { pub display_name: String, pub users: Vec, diff --git a/src/domain/handler.rs b/src/domain/handler.rs index 7fee3ce..9fee892 100644 --- a/src/domain/handler.rs +++ b/src/domain/handler.rs @@ -105,15 +105,7 @@ impl BackendHandler for SqlBackendHandler { query_builder.to_string(SqliteQueryBuilder) }; - let results = sqlx::query(&query) - .map(|row: DbRow| User { - user_id: row.get::("user_id"), - email: row.get::("email"), - display_name: row.get::("display_name"), - first_name: row.get::("first_name"), - last_name: row.get::("last_name"), - creation_date: row.get::("creation_date"), - }) + let results = sqlx::query_as::<_, User>(&query) .fetch(&self.sql_pool) .collect::>>() .await; diff --git a/src/domain/sql_tables.rs b/src/domain/sql_tables.rs index cc74834..ed53c70 100644 --- a/src/domain/sql_tables.rs +++ b/src/domain/sql_tables.rs @@ -2,7 +2,6 @@ use sea_query::*; pub type Pool = sqlx::sqlite::SqlitePool; pub type PoolOptions = sqlx::sqlite::SqlitePoolOptions; -pub type DbRow = sqlx::sqlite::SqliteRow; #[derive(Iden)] pub enum Users {