41 lines
1.4 KiB
Rust

use crate::{Attributes, common};
use quote::quote;
pub(crate) fn derive_diesel_crud_create_impl(
Attributes {
struct_ident,
table,
insert,
..
}: &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;
async fn insert(insert: Self::Insert, conn: &mut diesel_async::AsyncPgConnection) -> #return_type {
use diesel::associations::HasTable;
diesel_async::RunQueryDsl::get_result(
diesel::dsl::insert_into(#table::table::table()).values(insert),
conn
)
.await
.map_err(Into::into)
}
async fn insert_many(insert: &[Self::Insert], conn: &mut diesel_async::AsyncPgConnection) -> #many_return_type {
use diesel::associations::HasTable;
diesel_async::RunQueryDsl::get_results(
diesel::dsl::insert_into(#table::table::table()).values(insert),
conn
)
.await
.map_err(Into::into)
}
}
}
}