bonsaidb icon indicating copy to clipboard operation
bonsaidb copied to clipboard

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

Open ModProg opened this issue 2 years ago • 0 comments

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.

ModProg avatar Apr 25 '23 22:04 ModProg