crudly 0.6.0

Derivable crud helpers for sqlx. Write your SELECT, INSERT, UPDATE and DELETE queries once and reuse them for all your entities.
Documentation
use crudly::sql::generic_insert_returning_id;
use crudly::{DBAssignedId, IntoRow, Schema};
use sqlx::{FromRow, Sqlite, SqliteExecutor, SqlitePool, query};
use std::future::Future;

const SQL_SCHEMA: &str =
    "CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT NOT NULL);";

#[derive(FromRow, IntoRow, Schema)]
struct User {
    #[crudly(id)]
    id: i64,
    name: String,
}

/// Define a trait for your operations.
///
/// Note that this trait is not generic over the database. That is to make this
/// example more readable. The crudly traits are all generic over the db but the
/// generics of that are much more ugly than committing to a single database.
trait MyCustomInsert: Sized {
    fn insert<'e>(
        self,
        executor: impl SqliteExecutor<'e>,
    ) -> impl Future<Output = sqlx::Result<i64>> + Send;
}

// Create a blanked impl that implements your trait.
impl<T> MyCustomInsert for T
where
    T: Schema + IntoRow<Sqlite> + DBAssignedId,
{
    fn insert<'e>(
        self,
        executor: impl SqliteExecutor<'e>,
    ) -> impl Future<Output = sqlx::Result<i64>> + Send {
        async move {
            let _inserted_id = generic_insert_returning_id::<T, Sqlite>(executor, self).await?;
            // ... why are all my ids 42?
            Ok(42)
        }
    }
}

#[tokio::main]
async fn main() {
    let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
    query(SQL_SCHEMA).execute(&pool).await.unwrap();

    let user = User {
        id: 0,
        name: "John Doe".to_string(),
    };

    let inserted_id = user.insert(&pool).await.unwrap();
    assert_eq!(inserted_id, 42);
}