Skip to content

Commit 93025fd

Browse files
authored
Merge pull request #804 from utopia-php/feat-foreach-generator
2 parents aa80f86 + 502f7ab commit 93025fd

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/Database/Database.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7856,8 +7856,8 @@ public function find(string $collection, array $queries = [], string $forPermiss
78567856
}
78577857

78587858
/**
7859-
* Call callback for each document of the given collection
7860-
* that matches the given queries
7859+
* Helper method to iterate documents in collection using callback pattern
7860+
* Alterative is
78617861
*
78627862
* @param string $collection
78637863
* @param callable $callback
@@ -7867,6 +7867,23 @@ public function find(string $collection, array $queries = [], string $forPermiss
78677867
* @throws \Utopia\Database\Exception
78687868
*/
78697869
public function foreach(string $collection, callable $callback, array $queries = [], string $forPermission = Database::PERMISSION_READ): void
7870+
{
7871+
foreach ($this->iterate($collection, $queries, $forPermission) as $document) {
7872+
$callback($document);
7873+
}
7874+
}
7875+
7876+
/**
7877+
* Return each document of the given collection
7878+
* that matches the given queries
7879+
*
7880+
* @param string $collection
7881+
* @param array<Query> $queries
7882+
* @param string $forPermission
7883+
* @return \Generator
7884+
* @throws \Utopia\Database\Exception
7885+
*/
7886+
public function iterate(string $collection, array $queries = [], string $forPermission = Database::PERMISSION_READ): \Generator
78707887
{
78717888
$grouped = Query::groupByType($queries);
78727889
$limitExists = $grouped['limit'] !== null;
@@ -7906,9 +7923,7 @@ public function foreach(string $collection, callable $callback, array $queries =
79067923
$sum = count($results);
79077924

79087925
foreach ($results as $document) {
7909-
if (is_callable($callback)) {
7910-
$callback($document);
7911-
}
7926+
yield $document;
79127927
}
79137928

79147929
$latestDocument = $results[array_key_last($results)];

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4014,6 +4014,26 @@ public function testForeach(): void
40144014
/** @var Database $database */
40154015
$database = $this->getDatabase();
40164016

4017+
/**
4018+
* Test, foreach generator on empty collection
4019+
*/
4020+
$database->createCollection('moviesEmpty');
4021+
$documents = [];
4022+
foreach ($database->iterate('moviesEmpty', queries: [Query::limit(2)]) as $document) {
4023+
$documents[] = $document;
4024+
}
4025+
$this->assertEquals(0, \count($documents));
4026+
$this->assertTrue($database->deleteCollection('moviesEmpty'));
4027+
4028+
/**
4029+
* Test, foreach generator
4030+
*/
4031+
$documents = [];
4032+
foreach ($database->iterate('movies', queries: [Query::limit(2)]) as $document) {
4033+
$documents[] = $document;
4034+
}
4035+
$this->assertEquals(6, count($documents));
4036+
40174037
/**
40184038
* Test, foreach goes through all the documents
40194039
*/

0 commit comments

Comments
 (0)