Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions src/PostgresDocumentStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<EOT
SELECT $select
FROM {$this->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
*/
Expand Down
66 changes: 66 additions & 0 deletions tests/PostgresDocumentStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down