InsertMany function for create trait.

Changed path to DateTimeInterval.

Changed lifetimes on derives with async_trait
This commit is contained in:
Martin Berg Alstad
2024-08-25 17:56:56 +02:00
parent 762330189d
commit eeab1257e3
11 changed files with 80 additions and 56 deletions

View File

@ -9,6 +9,6 @@ pub(crate) struct PrimaryKey {
pub(crate) fn return_type(output: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
quote! {
std::pin::Pin<Box<dyn core::future::Future<Output = Result<#output, lib::diesel_crud_trait::CrudError>> + Send + 'b>>
std::pin::Pin<Box<dyn core::future::Future<Output = Result<#output, lib::diesel_crud_trait::CrudError>> + Send + 'async_trait>>
}
}

View File

@ -10,15 +10,16 @@ pub(crate) fn derive_diesel_crud_create_impl(
}: &Attributes,
) -> proc_macro2::TokenStream {
let return_type = common::return_type(quote! { Self });
let many_return_type = common::return_type(quote! { Vec<Self> });
quote! {
#[automatically_derived]
impl lib::diesel_crud_trait::DieselCrudCreate<#table::table> for #struct_ident {
type Insert = #insert;
fn create<'a, 'b>(insert: Self::Insert, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
fn insert<'a, 'async_trait>(insert: Self::Insert, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
where
Self: Sized + Sync + 'a,
'a: 'b,
'a: 'async_trait,
{
Box::pin(async move {
use diesel::associations::HasTable;
@ -30,6 +31,23 @@ pub(crate) fn derive_diesel_crud_create_impl(
.map_err(Into::into)
})
}
fn insert_many<'a, 'b, 'async_trait>(insert: &'a [Self::Insert], conn: &'b mut diesel_async::AsyncPgConnection) -> #many_return_type
where
Self: Sized + Sync + 'async_trait,
'a: 'async_trait,
'b: 'async_trait,
{
Box::pin(async move {
use diesel::associations::HasTable;
diesel_async::RunQueryDsl::get_results(
diesel::dsl::insert_into(#table::table::table()).values(insert),
conn
)
.await
.map_err(Into::into)
})
}
}
}
}

View File

@ -22,10 +22,10 @@ pub(crate) fn derive_diesel_crud_delete_impl(
#[automatically_derived]
impl lib::diesel_crud_trait::DieselCrudDelete for #struct_ident {
type PK = #pk_type;
fn delete<'a, 'b>(pk: Self::PK, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
fn delete<'a, 'async_trait>(pk: Self::PK, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
where
Self: Sized + Sync + 'a,
'a: 'b,
'a: 'async_trait,
{
Box::pin(async move {
use diesel::QueryDsl;

View File

@ -13,10 +13,10 @@ pub(crate) fn derive_diesel_crud_list_impl(
quote! {
#[automatically_derived]
impl lib::diesel_crud_trait::DieselCrudList for #struct_ident {
fn list<'a, 'b>(conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
fn list<'a, 'async_trait>(conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
where
Self: Sized + Sync + 'a,
'a: 'b
'a: 'async_trait
{
Box::pin(async move {
use diesel::associations::HasTable;

View File

@ -20,10 +20,10 @@ pub(crate) fn derive_diesel_crud_read_impl(
#[automatically_derived]
impl lib::diesel_crud_trait::DieselCrudRead for #struct_ident {
type PK = #pk_type;
fn read<'a, 'b>(pk: Self::PK, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
fn read<'a, 'async_trait>(pk: Self::PK, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
where
Self: Sized + Sync + 'a,
'a: 'b
'a: 'async_trait
{
Box::pin(async move {
use diesel::associations::HasTable;

View File

@ -15,10 +15,10 @@ pub(crate) fn derive_diesel_crud_update_impl(
#[automatically_derived]
impl lib::diesel_crud_trait::DieselCrudUpdate for #struct_ident {
type Update = #update;
fn update<'a, 'b>(update: Self::Update, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
fn update<'a, 'async_trait>(update: Self::Update, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
where
Self: Sized + Sync + 'a,
'a: 'b,
'a: 'async_trait,
{
Box::pin(async move {
use diesel::associations::HasTable;

View File

@ -34,7 +34,11 @@ where
Self: Sized,
{
type Insert: Insertable<Table>;
async fn create(insert: Self::Insert, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
async fn insert(insert: Self::Insert, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
async fn insert_many(
insert: &[Self::Insert],
conn: &mut AsyncPgConnection,
) -> Result<Vec<Self>, CrudError>;
}
/// Gets an entity from the database

View File

@ -42,7 +42,7 @@ async fn test_insert_user() {
let database_url = dotenv!("DATABASE_URL");
let mut conn = AsyncPgConnection::establish(database_url).await.unwrap();
conn.begin_test_transaction().await.unwrap();
let _user = User::create(
let _user = User::insert(
InsertUser {
email: "test".to_string(),
},