mysql-repo

A tiny library that exposes a Repository trait (and some helper traits) to structure some CRUD SQL boilerplate.

Currently just for rust-mysql-simple.

Just implement some of the library's traits:

```rust pub enum Users {}

impl Repository for Users {}

impl<'a> Insertable for &'a NewUser { fn sql() -> &'static str { "INSERT INTO users(email, data) VALUES (?, ?)" } fn to_params(&self) -> ::mysql::Params { (&self.email, &self.data).into() } }

impl<'a> Updatable for &'a User { fn sql() -> &'static str { "UPDATE users SET email = ?, data = ? WHERE id = ?" } fn to_params(&self) -> ::mysql::Params { (&self.email, &self.data, &self.id).into() } }

impl Findable for User { fn sql() -> &'static str { "SELECT id, email, data FROM users WHERE id = ? LIMIT 1" } fn fromrow(row: ::mysql::Row) -> Result { let (id, email, data) = ::mysql::fromrow_opt(row)?; Ok(User { id, email, data, ..Default::default() }) } }

impl Whereable for User { fn wrappedsql(sql: &str) -> String { ["SELECT id, email, data FROM users WHERE ", sql].concat() } fn fromrow(row: ::mysql::Row) -> Result { >::from_row(row) } }

impl Deletable for User { fn sql() -> &'static str { "DELETE FROM users WHERE id = ?" } fn to_params(&self) -> ::mysql::Params { (&self.id,).into() } } ```

And then you can do:

let mut user: User = Repository::find(&mut conn, 1)?.ok_or("No users")?;    
user.email = "something@email.com";
Repository::update(&mut conn, &user)?;
let user: User = Repository::where_one("email = ?", ("something@email.com",))?.ok_or("No users")?;

etc.