khonsulabs / bonsaidb

A developer-friendly document database that grows with you, written in Rust
https://bonsaidb.io/
Apache License 2.0
998 stars 37 forks source link

Create `#[derive(Views)]` macro for collections #292

Open ModProg opened 1 year ago

ModProg commented 1 year ago

My idea would be to allow something like:

#[drive(Collection, Views)]
#[collection(name="timer")]
#[view(TimerIsRunning, name = "timer_by_running", condition = |document| document.contents.running)] // Creates a View that emits all Documents where running = true
pub struct Timer {
    #[view(TimerByRunning. name = "timer_by_running")] // Creates a View, that emits the key `running`
    #[view(TimerIsRunning, name = "timer_by_running", when_eq = true)] // Creates a View that emits all Documents where running = true
    pub running: bool
}

First argument to #[view(...)] would be the struct name of the view, following would be the properties.

Multiple attributes with the same struct name would be the same view.

I think there is more to add here, just some initial ideas. There is also probably a limit till where it makes sense to be able to derive it like so, and after which a custom implementation will be more sensible.