Skip to content

Commit 7be5362

Browse files
author
Alexander Miertsch
authored
Merge pull request #13 from event-engine/feature/get_partial_doc
Add method to retrieve a single partial doc
2 parents c10ec7e + 018737f commit 7be5362

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.1",
2020
"ext-pdo": "*",
21-
"event-engine/php-persistence": "^0.6"
21+
"event-engine/php-persistence": "^0.7"
2222
},
2323
"require-dev": {
2424
"roave/security-advisories": "dev-master",

src/PostgresDocumentStore.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,31 @@ public function getDoc(string $collectionName, string $docId): ?array
498498
return json_decode($row, true);
499499
}
500500

501+
/**
502+
* @inheritDoc
503+
*/
504+
public function getPartialDoc(string $collectionName, PartialSelect $partialSelect, string $docId): ?array
505+
{
506+
$select = $this->makeSelect($partialSelect);
507+
508+
$query = <<<EOT
509+
SELECT $select
510+
FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
511+
WHERE id = :id
512+
EOT;
513+
$stmt = $this->connection->prepare($query);
514+
515+
$stmt->execute(['id' => $docId]);
516+
517+
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
518+
519+
if(!$row) {
520+
return null;
521+
}
522+
523+
return $this->transformPartialDoc($partialSelect, $row);
524+
}
525+
501526
/**
502527
* @inheritDoc
503528
*/

tests/PostgresDocumentStoreTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,72 @@ public function it_finds_partial_docs()
627627
], $result[$docCId]);
628628
}
629629

630+
/**
631+
* @test
632+
*/
633+
public function it_gets_partial_doc_by_id()
634+
{
635+
$collectionName = 'test_get_partial_doc';
636+
$this->documentStore->addCollection($collectionName);
637+
638+
$docAId = Uuid::uuid4()->toString();
639+
$docA = [
640+
'some' => [
641+
'prop' => 'foo',
642+
'other' => [
643+
'nested' => 42
644+
]
645+
],
646+
'baz' => 'bat',
647+
];
648+
$this->documentStore->addDoc($collectionName, $docAId, $docA);
649+
650+
$docBId = Uuid::uuid4()->toString();
651+
$docB = [
652+
'some' => [
653+
'prop' => 'bar',
654+
'other' => [
655+
'nested' => 43
656+
],
657+
//'baz' => 'bat', missing so should be null
658+
],
659+
];
660+
$this->documentStore->addDoc($collectionName, $docBId, $docB);
661+
662+
$docCId = Uuid::uuid4()->toString();
663+
$docC = [
664+
'some' => [
665+
'prop' => 'foo',
666+
'other' => [
667+
//'nested' => 42, missing, so should be null
668+
'ignoredNested' => 'value'
669+
]
670+
],
671+
'baz' => 'bat',
672+
];
673+
$this->documentStore->addDoc($collectionName, $docCId, $docC);
674+
675+
$partialSelect = new PartialSelect([
676+
'some.alias' => 'some.prop', // Nested alias <- Nested field
677+
'magicNumber' => 'some.other.nested', // Top level alias <- Nested Field
678+
'baz', // Top level field,
679+
]);
680+
681+
$partialDocA = $this->documentStore->getPartialDoc($collectionName, $partialSelect, $docAId);
682+
683+
$this->assertEquals([
684+
'some' => [
685+
'alias' => 'foo',
686+
],
687+
'magicNumber' => 42,
688+
'baz' => 'bat',
689+
], $partialDocA);
690+
691+
$partialDocD = $this->documentStore->getPartialDoc($collectionName, $partialSelect, Uuid::uuid4()->toString());
692+
693+
$this->assertNull($partialDocD);
694+
}
695+
630696
/**
631697
* @test
632698
*/

0 commit comments

Comments
 (0)