Skip to content

Commit

Permalink
feat(Database): added deleteDocuments
Browse files Browse the repository at this point in the history
  • Loading branch information
phodoval committed Aug 4, 2023
1 parent 04f2bc1 commit ce97b31
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public function updateDocument(Document $document): void {

/**
* @param Document[] $documents
* @return int Number of updated rows
* @throws GuzzleException
*/
public function updateDocuments(array $documents): void {
public function updateDocuments(array $documents): int {
$requestData = [
'docs' => array_map(fn (Document $document) => $document->toArray(), $documents),
];
Expand All @@ -93,11 +94,15 @@ public function updateDocuments(array $documents): void {
*/
$responseData = json_decode($response->getBody()->getContents(), true);

$affectedRows = 0;
foreach ($responseData as $index => $document) {
if ($document['ok']) {
$documents[$index]->setRevision($document['rev']);
$affectedRows++;
}
}

return $affectedRows;
}

public function deleteDocument(Document $document): bool {
Expand All @@ -114,6 +119,28 @@ public function deleteDocument(Document $document): bool {
return true;
}

/**
* @param Document[] $documents
* @return int Number of deleted rows
* @throws GuzzleException
*/
public function deleteDocuments(array $documents): int {
foreach ($documents as $document) {
$document->setData(['_deleted' => true]);
}

return $this->updateDocuments($documents);
}

/**
* @param array<string, mixed> $query
* @throws GuzzleException
*/
public function deleteDocumentsByQuery(array $query): int {
$documents = $this->findDocuments($query);
return $this->deleteDocuments($documents);
}

/**
* @param array<string, mixed> $query
* @return Document[]
Expand Down
15 changes: 13 additions & 2 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function testFindDocumentsSuccess(): void {
/**
* @throws GuzzleException
*/
public function updateDocumentsSuccess(): void {
public function testUpdateDocumentsSuccess(): void {
$database = $this->getClient([
new Response(200, [], '{"docs":[{"_id":"myDocId","_rev":"1-967a00dff5e02add41819138abb3284d","name":"John Doe"},{"_id":"myDocId2","_rev":"1-967a00dff5e02add41819138abb3284d","name":"Jane Doe"}]}'),
new Response(200, [], '[{"ok":true,"id":"myDocId","rev":"2-7051cbe5c8faecd085a3fa619e6e6337"},{"ok":true,"id":"myDocId2","rev":"2-5c8faecd085a3fa619e6e6337"}]'),
Expand All @@ -146,8 +146,19 @@ public function updateDocumentsSuccess(): void {
$documents = $database->findDocuments([]);
$this->assertCount(2, $documents);

$database->updateDocuments($documents);
$count = $database->updateDocuments($documents);
$this->assertEquals(2, $count);
$this->assertEquals('2-7051cbe5c8faecd085a3fa619e6e6337', $documents[0]->getRevision());
$this->assertEquals('2-5c8faecd085a3fa619e6e6337', $documents[1]->getRevision());
}

public function testDeleteDocumentsSuccess(): void {
$database = $this->getClient([
new Response(200, [], '{"docs":[{"_id":"myDocId","_rev":"1-967a00dff5e02add41819138abb3284d","name":"John Doe"},{"_id":"myDocId2","_rev":"1-967a00dff5e02add41819138abb3284d","name":"John Doe"}]}'),
new Response(200, [], '[{"ok":true,"id":"myDocId","rev":"2-7051cbe5c8faecd085a3fa619e6e6337","_deleted":true},{"ok":true,"id":"myDocId2","rev":"2-5c8faecd085a3fa619e6e6337","_deleted":true}]'),
])->database('test');

$count = $database->deleteDocumentsByQuery(['selector' => ['name' => 'John Doe']]);
$this->assertEquals(2, $count);
}
}

0 comments on commit ce97b31

Please sign in to comment.