hirethunk / verbs

Verbs is an event sourcing package for PHP artisans
https://verbs.thunk.dev
MIT License
412 stars 32 forks source link

Add naive load many approach to load #171

Closed joshhanley closed 3 weeks ago

joshhanley commented 1 month ago

This PR adds support for passing an array or collection of IDs to State::load(), to load multiple states. load() will return a StateCollection of loaded states.

$states = MyState::load([1, 2, 5, 8]);

The caveat here is that this just wraps up the existing load functionality, so it does cause n+1 issues.

This saves people having to do this manually

$states = StateCollection::wrap([1, 2, 5, 8])->map(
    fn($id) => MyState::load($id)
);

I looked into creating a load method that can load multiple states with only a couple of queries, but due to the state event queries using the after_event_id makes the query a lot more difficult.

So for now I have just opted to keep it simple.