treydempsey / sqlx-crud

Rust Sqlx CRUD Derive Macros
MIT License
27 stars 9 forks source link

Option to choose which crud operation to implement #17

Open MattDelac opened 2 weeks ago

MattDelac commented 2 weeks ago

First congrats on that library I thought about it a year ago and never took time to get a stab at it 👏 It's exactly what I was looking for in the past.

On my side, i often have different struct to have different guards and business logic.

So something like the following would be awesome. It would also much less work to maintain on this side of the library than having a field attribute (as seen in the roadmap)

#[derive(SqlxCrud(only: [get, delete]))
pub struct Contact {
    pub id: i32,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub email: Option<String>,
    pub phone_number: Option<String>,
}

#[derive(SqlxCrud(only: [create]))
pub struct NewContact {
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub email: Option<String>,
    pub phone_number: Option<String>,
}

#[derive(SqlxCrud(only: [update]))
pub struct UpdateContact {
    pub email: Option<String>,
    pub phone_number: Option<String>,
}

impl From<Contact> for UpdateContact {
    pub from(contact: Contact) -> Self {
        UpdateContact {
            first_name: contact.first_name,
            last_name: contact.last_name,
        }
    }
}

I wold be happy to give it a try. I have not looked at the code yet