From 018737f79f248f214fc961729b560f5bde5edc6a Mon Sep 17 00:00:00 2001 From: codeliner Date: Tue, 31 Mar 2020 22:11:39 +0200 Subject: [PATCH] Add method to retrieve a single partial doc --- composer.json | 2 +- src/PostgresDocumentStore.php | 25 +++++++++++ tests/PostgresDocumentStoreTest.php | 66 +++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 694da52..4c18e4b 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "require": { "php": "^7.1", "ext-pdo": "*", - "event-engine/php-persistence": "^0.6" + "event-engine/php-persistence": "^0.7" }, "require-dev": { "roave/security-advisories": "dev-master", diff --git a/src/PostgresDocumentStore.php b/src/PostgresDocumentStore.php index 1ac0b5b..4e7ef62 100644 --- a/src/PostgresDocumentStore.php +++ b/src/PostgresDocumentStore.php @@ -498,6 +498,31 @@ public function getDoc(string $collectionName, string $docId): ?array return json_decode($row, true); } + /** + * @inheritDoc + */ + public function getPartialDoc(string $collectionName, PartialSelect $partialSelect, string $docId): ?array + { + $select = $this->makeSelect($partialSelect); + + $query = <<schemaName($collectionName)}.{$this->tableName($collectionName)} +WHERE id = :id +EOT; + $stmt = $this->connection->prepare($query); + + $stmt->execute(['id' => $docId]); + + $row = $stmt->fetch(\PDO::FETCH_ASSOC); + + if(!$row) { + return null; + } + + return $this->transformPartialDoc($partialSelect, $row); + } + /** * @inheritDoc */ diff --git a/tests/PostgresDocumentStoreTest.php b/tests/PostgresDocumentStoreTest.php index 54f959b..6753023 100644 --- a/tests/PostgresDocumentStoreTest.php +++ b/tests/PostgresDocumentStoreTest.php @@ -627,6 +627,72 @@ public function it_finds_partial_docs() ], $result[$docCId]); } + /** + * @test + */ + public function it_gets_partial_doc_by_id() + { + $collectionName = 'test_get_partial_doc'; + $this->documentStore->addCollection($collectionName); + + $docAId = Uuid::uuid4()->toString(); + $docA = [ + 'some' => [ + 'prop' => 'foo', + 'other' => [ + 'nested' => 42 + ] + ], + 'baz' => 'bat', + ]; + $this->documentStore->addDoc($collectionName, $docAId, $docA); + + $docBId = Uuid::uuid4()->toString(); + $docB = [ + 'some' => [ + 'prop' => 'bar', + 'other' => [ + 'nested' => 43 + ], + //'baz' => 'bat', missing so should be null + ], + ]; + $this->documentStore->addDoc($collectionName, $docBId, $docB); + + $docCId = Uuid::uuid4()->toString(); + $docC = [ + 'some' => [ + 'prop' => 'foo', + 'other' => [ + //'nested' => 42, missing, so should be null + 'ignoredNested' => 'value' + ] + ], + 'baz' => 'bat', + ]; + $this->documentStore->addDoc($collectionName, $docCId, $docC); + + $partialSelect = new PartialSelect([ + 'some.alias' => 'some.prop', // Nested alias <- Nested field + 'magicNumber' => 'some.other.nested', // Top level alias <- Nested Field + 'baz', // Top level field, + ]); + + $partialDocA = $this->documentStore->getPartialDoc($collectionName, $partialSelect, $docAId); + + $this->assertEquals([ + 'some' => [ + 'alias' => 'foo', + ], + 'magicNumber' => 42, + 'baz' => 'bat', + ], $partialDocA); + + $partialDocD = $this->documentStore->getPartialDoc($collectionName, $partialSelect, Uuid::uuid4()->toString()); + + $this->assertNull($partialDocD); + } + /** * @test */