From 47e8071b6826f27e2a562680b4948c37dffa68cb Mon Sep 17 00:00:00 2001 From: dull b Date: Tue, 25 Jul 2023 18:31:28 +0000 Subject: [PATCH] Attempt Crud::create --- crates/db_schema/src/traits.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index c26ba939a..3f62f81ae 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -7,7 +7,8 @@ use diesel::{ dsl, expression::{AsExpression, TypedExpressionType}, expression_methods::ExpressionMethods, - query_builder::{DeleteStatement, IntoUpdateTarget}, + insert_into, + query_builder::{DeleteStatement, InsertStatement, IntoUpdateTarget}, query_dsl::methods::{FindDsl, LimitDsl}, result::Error, sql_types::SqlType, @@ -29,10 +30,13 @@ pub type Delete = DeleteStatement<::Table, = dsl::Find<::Table, >::IdType>; +pub type InsertValues<'a, 'b, T> = + <&'a >::InsertForm as Insertable<::Table>>::Values; + // When using `RunQueryDsl::execute`, directly building futures with `Box::pin` and `TryFutureExt::and_then` // instead of `async` + `await` fixes weird compile errors. // https://github.com/rust-lang/rust/issues/102211 -// When using `RunQueryDsl::first`, `async` + `await` works, and it must be used otherwise the closure for `and_then` +// When using `RunQueryDsl::first` or 'RunQueryDsl::get_result`, `async` + `await` works, and it must be used otherwise the closure for `and_then` // will both own `conn` and return a future that references it. #[async_trait] pub trait Crud<'a> @@ -46,27 +50,32 @@ where for<'b> Delete>: ExecuteDsl + Send + 'static, for<'b> as IntoUpdateTarget>::WhereClause: 'static + Send, for<'b> as HasTable>::Table: 'static + Send, + for<'b> &'a >::InsertForm: Insertable, + for<'b> InsertValues<'a, 'b, Self>: 'a, + for<'b, 'query> InsertStatement>: + LoadQuery<'query, AsyncPgConnection, Self> + 'query + Send, { - /*for<'a> &'a Self::InsertForm: Insertable, - for<'a> InsertStatement>::Values>: - LoadQuery<'a, AsyncPgConnection, Self> + 'a, - for<'a> <&'a Self::InsertForm as Insertable>::Values: 'a,*/ - type InsertForm; - type UpdateForm; + type InsertForm: 'static + Send + Sync; + type UpdateForm: 'static + Send + Sync; type IdType: 'static + Hash + Eq + Sized + Send + AsExpression<<::PrimaryKey as Expression>::SqlType>; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result + async fn create<'life0, 'life1>( + pool: &'life0 mut DbPool<'life1>, + form: &'a Self::InsertForm, + ) -> Result + //Pin> + Send + 'async_trait>> where - 'a: 'async_trait; - /*{ + 'a: 'async_trait, + { let query = insert_into(Self::table()).values(form); let conn = &mut *get_conn(pool).await?; query.get_result::(conn).await - }*/ + //Box::pin(get_conn(pool).and_then(move |mut conn| query.get_result::(&mut *conn))) + } async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result where 'a: 'async_trait,