55use Carbon \Carbon ;
66use Illuminate \Database \Eloquent \Builder ;
77use Illuminate \Support \LazyCollection ;
8+ use Illuminate \Support \Collection ;
89use ReflectionClass ;
910use ReflectionException ;
1011use Spatie \EventSourcing \AggregateRoots \Exceptions \InvalidEloquentStoredEventModel ;
@@ -49,9 +50,9 @@ public function retrieveAll(?string $uuid = null): LazyCollection
4950 return $ query ->orderBy ('id ' )->cursor ()->map (fn (EloquentStoredEvent $ storedEvent ) => $ storedEvent ->toStoredEvent ());
5051 }
5152
52- public function retrieveAllStartingFrom (int $ startingFrom , ?string $ uuid = null ): LazyCollection
53+ public function retrieveAllStartingFrom (int $ startingFrom , ?string $ uuid = null , array $ events = [] ): LazyCollection
5354 {
54- $ query = $ this ->prepareEventModelQuery ($ startingFrom , $ uuid );
55+ $ query = $ this ->prepareEventModelQuery ($ startingFrom , $ uuid, $ events );
5556
5657 /** @var LazyCollection $lazyCollection */
5758 $ lazyCollection = $ query
@@ -61,9 +62,24 @@ public function retrieveAllStartingFrom(int $startingFrom, ?string $uuid = null)
6162 return $ lazyCollection ->map (fn (EloquentStoredEvent $ storedEvent ) => $ storedEvent ->toStoredEvent ());
6263 }
6364
64- public function countAllStartingFrom (int $ startingFrom , ?string $ uuid = null ): int
65+
66+ public function runForAllStartingFrom (int $ startingFrom , callable |\Closure $ function , int $ chunkSize = 1000 , ?string $ uuid = null , array $ events = []): bool {
67+ $ query = $ this ->prepareEventModelQuery ($ startingFrom , $ uuid , $ events );
68+
69+ $ query = $ query
70+ ->orderBy ('id ' );
71+
72+ return $ query ->chunk ($ chunkSize , function (Collection $ events ) use ($ function ) {
73+ foreach ($ events as $ event ) {
74+ $ storedEVent = $ event ->toStoredEvent ();
75+ $ function ($ storedEVent );
76+ }
77+ });
78+ }
79+
80+ public function countAllStartingFrom (int $ startingFrom , ?string $ uuid = null , array $ events = []): int
6581 {
66- return $ this ->prepareEventModelQuery ($ startingFrom , $ uuid )->count ('id ' );
82+ return $ this ->prepareEventModelQuery ($ startingFrom , $ uuid, $ events )->count ('id ' );
6783 }
6884
6985 public function retrieveAllAfterVersion (int $ aggregateVersion , string $ aggregateUuid ): LazyCollection
@@ -171,14 +187,18 @@ public function getLatestAggregateVersion(string $aggregateUuid): int
171187 ->max ('aggregate_version ' ) ?? 0 ;
172188 }
173189
174- private function prepareEventModelQuery (int $ startingFrom , ?string $ uuid = null ): Builder
190+ private function prepareEventModelQuery (int $ startingFrom , ?string $ uuid = null , array $ events = [] ): Builder
175191 {
176192 $ query = $ this ->getQuery ()->startingFrom ($ startingFrom );
177193
178194 if ($ uuid ) {
179195 $ query ->whereAggregateRoot ($ uuid );
180196 }
181197
198+ if (!empty ($ events )) {
199+ $ query ->whereEvent (...$ events );
200+ }
201+
182202 return $ query ;
183203 }
184204
0 commit comments