Skip to content

Commit 1e980c8

Browse files
author
Alexander Miertsch
authored
Merge pull request #16 from event-engine/feature/get_partial_doc
Add method to retrieve a single partial doc
2 parents 350ebd4 + e940163 commit 1e980c8

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/DocumentStore.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ public function deleteMany(string $collectionName, Filter $filter): void;
126126
*/
127127
public function getDoc(string $collectionName, string $docId): ?array;
128128

129+
/**
130+
* @param string $collectionName
131+
* @param PartialSelect $partialSelect
132+
* @param string $docId
133+
* @return array|null
134+
*/
135+
public function getPartialDoc(string $collectionName, PartialSelect $partialSelect, string $docId): ?array;
136+
129137
/**
130138
* @deprecated use findDocs instead
131139
*

src/InMemoryDocumentStore.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,20 @@ public function getDoc(string $collectionName, string $docId): ?array
287287
return $this->inMemoryConnection['documents'][$collectionName][$docId] ?? null;
288288
}
289289

290+
/**
291+
* @inheritDoc
292+
*/
293+
public function getPartialDoc(string $collectionName, PartialSelect $partialSelect, string $docId): ?array
294+
{
295+
$doc = $this->inMemoryConnection['documents'][$collectionName][$docId] ?? null;
296+
297+
if(null === $doc) {
298+
return null;
299+
}
300+
301+
return $this->transformToPartialDoc($doc, $partialSelect);
302+
}
303+
290304
/**
291305
* @inheritDoc
292306
*/

tests/InMemoryDocumentStoreTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,68 @@ public function it_filters_partial_docs()
242242
], $result['c']);
243243
}
244244

245+
/**
246+
* @test
247+
*/
248+
public function it_gets_partial_doc_by_id()
249+
{
250+
$this->store->addCollection('test');
251+
252+
$docA = [
253+
'some' => [
254+
'prop' => 'foo',
255+
'other' => [
256+
'nested' => 42
257+
]
258+
],
259+
'baz' => 'bat',
260+
];
261+
$this->store->addDoc('test', 'a', $docA);
262+
263+
$docB = [
264+
'some' => [
265+
'prop' => 'bar',
266+
'other' => [
267+
'nested' => 43
268+
],
269+
//'baz' => 'bat', missing so should be null
270+
],
271+
];
272+
$this->store->addDoc('test', 'b', $docB);
273+
274+
$docC = [
275+
'some' => [
276+
'prop' => 'foo',
277+
'other' => [
278+
//'nested' => 42, missing, so should be null
279+
'ignoredNested' => 'value'
280+
]
281+
],
282+
'baz' => 'bat',
283+
];
284+
$this->store->addDoc('test', 'c', $docC);
285+
286+
$partialSelect = new PartialSelect([
287+
'some.alias' => 'some.prop', // Nested alias <- Nested field
288+
'magicNumber' => 'some.other.nested', // Top level alias <- Nested Field
289+
'baz', // Top level field
290+
]);
291+
292+
$docA = $this->store->getPartialDoc('test', $partialSelect, 'a');
293+
294+
$this->assertEquals([
295+
'some' => [
296+
'alias' => 'foo',
297+
],
298+
'magicNumber' => 42,
299+
'baz' => 'bat'
300+
], $docA);
301+
302+
$docD = $this->store->getPartialDoc('test', $partialSelect, 'd');
303+
304+
$this->assertNull($docD);
305+
}
306+
245307
/**
246308
* @test
247309
*/

0 commit comments

Comments
 (0)