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

80
Cargo.lock generated
View File

@ -55,7 +55,7 @@ checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -182,9 +182,9 @@ checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
[[package]]
name = "cc"
version = "1.1.13"
version = "1.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48"
checksum = "50d2eb3cd3d1bf4529e31c215ee6f93ec5a3d536d9f578f93d9d33ee19562932"
dependencies = [
"shlex",
]
@ -256,7 +256,7 @@ dependencies = [
"proc-macro2",
"quote",
"strsim 0.11.1",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -267,7 +267,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [
"darling_core",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -320,7 +320,7 @@ dependencies = [
"deluxe-macros",
"once_cell",
"proc-macro2",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -333,7 +333,7 @@ dependencies = [
"proc-macro2",
"quote",
"strsim 0.10.0",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -348,7 +348,7 @@ dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -368,14 +368,14 @@ checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
name = "diesel"
version = "2.2.2"
version = "2.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf97ee7261bb708fa3402fa9c17a54b70e90e3cb98afb3dc8999d5512cb03f94"
checksum = "65e13bab2796f412722112327f3e575601a3e9cdcbe426f0d30dbf43f3f5dc71"
dependencies = [
"bitflags 2.6.0",
"byteorder",
@ -406,7 +406,7 @@ dependencies = [
"deluxe",
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -422,15 +422,15 @@ dependencies = [
[[package]]
name = "diesel_derives"
version = "2.2.2"
version = "2.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6ff2be1e7312c858b2ef974f5c7089833ae57b5311b334b30923af58e5718d8"
checksum = "e7f2c3de51e2ba6bf2a648285696137aaf0f5f487bcbea93972fe8a364e131a4"
dependencies = [
"diesel_table_macro_syntax",
"dsl_auto_type",
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -439,7 +439,7 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "209c735641a413bc68c4923a9d6ad4bcb3ca306b794edaa7eb0b3228a99ffb25"
dependencies = [
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -482,7 +482,7 @@ dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -551,7 +551,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -771,7 +771,7 @@ name = "into-response-derive"
version = "1.1.0"
dependencies = [
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -1035,7 +1035,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -1118,9 +1118,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.36"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
@ -1161,7 +1161,7 @@ version = "0.1.0"
dependencies = [
"quote",
"regex",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -1247,29 +1247,29 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.208"
version = "1.0.209"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2"
checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.208"
version = "1.0.209"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf"
checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
name = "serde_json"
version = "1.0.125"
version = "1.0.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed"
checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad"
dependencies = [
"itoa",
"memchr",
@ -1404,9 +1404,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.75"
version = "2.0.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9"
checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525"
dependencies = [
"proc-macro2",
"quote",
@ -1453,7 +1453,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -1505,7 +1505,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -1639,7 +1639,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]
[[package]]
@ -1706,9 +1706,9 @@ dependencies = [
[[package]]
name = "unicode-properties"
version = "0.1.1"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4259d9d4425d9f0661581b804cb85fe66a4c631cadd8f490d1c13a35d5d9291"
checksum = "52ea75f83c0137a9b98608359a5f1af8144876eb67bcb1ce837368e906a9f524"
[[package]]
name = "valuable"
@ -1762,7 +1762,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
"wasm-bindgen-shared",
]
@ -1784,7 +1784,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -1947,5 +1947,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.75",
"syn 2.0.76",
]

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(),
},

View File

@ -20,7 +20,7 @@ impl DateTimeInterval {
/// # Examples
/// ```
/// use chrono::{NaiveDateTime, Duration, Utc};
/// use lib::time::common::DateTimeInterval;
/// use lib::time::DateTimeInterval;
///
/// let start = Utc::now().naive_utc();
/// let end = start + Duration::days(1);
@ -30,7 +30,7 @@ impl DateTimeInterval {
/// Illegal interval:
/// ```
/// use chrono::{NaiveDateTime, Utc};
/// use lib::time::common::DateTimeInterval;
/// use lib::time::DateTimeInterval;
/// let start = Utc::now().naive_utc();
/// let end = start;
/// let interval = DateTimeInterval::new_safe(start, end);

View File

@ -1 +1,3 @@
pub mod common;
mod common;
pub use common::DateTimeInterval;