Skip to content

Commit 04f2bc1

Browse files
committed
feat(Database): added updateDocuments method
1 parent c7890b7 commit 04f2bc1

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/Database.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,29 @@ public function updateDocument(Document $document): void {
7777
$document->setRevision($responseData['rev']);
7878
}
7979

80+
/**
81+
* @param Document[] $documents
82+
* @throws GuzzleException
83+
*/
84+
public function updateDocuments(array $documents): void {
85+
$requestData = [
86+
'docs' => array_map(fn (Document $document) => $document->toArray(), $documents),
87+
];
88+
89+
$response = $this->client->post('/' . $this->name . '/_bulk_docs', $requestData);
90+
91+
/**
92+
* @var array<array{id: string, rev: string, ok: bool}> $responseData
93+
*/
94+
$responseData = json_decode($response->getBody()->getContents(), true);
95+
96+
foreach ($responseData as $index => $document) {
97+
if ($document['ok']) {
98+
$documents[$index]->setRevision($document['rev']);
99+
}
100+
}
101+
}
102+
80103
public function deleteDocument(Document $document): bool {
81104
try {
82105
$this->client->delete('/' . $this->name . '/' . $document->getId(), [

src/Document.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ public function get(string $key): mixed {
4343
public function setData(array $data): void {
4444
$this->data = array_merge($this->data, $data);
4545
}
46+
47+
/**
48+
* @return array<string, mixed>
49+
*/
50+
public function toArray(): array {
51+
return [
52+
'_id' => $this->id,
53+
'_rev' => $this->rev,
54+
] + $this->data;
55+
}
4656
}

tests/DocumentTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,21 @@ public function testFindDocumentsSuccess(): void {
133133
$this->assertEquals('1-967a00dff5e02add41819138abb3284d', $documents[0]->getRevision());
134134
$this->assertEquals(['name' => 'John Doe'], $documents[0]->getData());
135135
}
136+
137+
/**
138+
* @throws GuzzleException
139+
*/
140+
public function updateDocumentsSuccess(): void {
141+
$database = $this->getClient([
142+
new Response(200, [], '{"docs":[{"_id":"myDocId","_rev":"1-967a00dff5e02add41819138abb3284d","name":"John Doe"},{"_id":"myDocId2","_rev":"1-967a00dff5e02add41819138abb3284d","name":"Jane Doe"}]}'),
143+
new Response(200, [], '[{"ok":true,"id":"myDocId","rev":"2-7051cbe5c8faecd085a3fa619e6e6337"},{"ok":true,"id":"myDocId2","rev":"2-5c8faecd085a3fa619e6e6337"}]'),
144+
])->database('test');
145+
146+
$documents = $database->findDocuments([]);
147+
$this->assertCount(2, $documents);
148+
149+
$database->updateDocuments($documents);
150+
$this->assertEquals('2-7051cbe5c8faecd085a3fa619e6e6337', $documents[0]->getRevision());
151+
$this->assertEquals('2-5c8faecd085a3fa619e6e6337', $documents[1]->getRevision());
152+
}
136153
}

0 commit comments

Comments
 (0)