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
75 changes: 75 additions & 0 deletions src/PostgresDocumentStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use EventEngine\DocumentStore\Postgres\Exception\InvalidArgumentException;
use EventEngine\DocumentStore\Postgres\Exception\RuntimeException;
use EventEngine\Util\VariableType;

use function implode;
use function is_string;
use function json_decode;
Expand Down Expand Up @@ -433,6 +434,80 @@ public function upsertDoc(string $collectionName, string $docId, array $docOrSub
}
}

/**
* @param string $collectionName
* @param string $docId
* @param array $doc
* @throws \Throwable if updating did not succeed
*/
public function replaceDoc(string $collectionName, string $docId, array $doc): void
{
$metadataStr = '';
$metadata = [];

if($this->useMetadataColumns && array_key_exists('metadata', $doc)) {
$metadata = $doc['metadata'];
unset($doc['metadata']);


foreach ($metadata as $k => $v) {
$metadataStr .= ', '.$k.' = :'.$k;
}
}

$cmd = <<<EOT
UPDATE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
SET doc = :doc{$metadataStr}
WHERE id = :id
;
EOT;
$this->transactional(function () use ($cmd, $docId, $doc, $metadata) {
$this->connection->prepare($cmd)->execute(array_merge([
'id' => $docId,
'doc' => json_encode($doc)
], $metadata));
});
}

/**
* @param string $collectionName
* @param Filter $filter
* @param array $set
* @throws \Throwable in case of connection error or other issues
*/
public function replaceMany(string $collectionName, Filter $filter, array $set): void
{
[$filterStr, $args] = $this->filterToWhereClause($filter);

$where = $filterStr? "WHERE $filterStr" : '';

$metadataStr = '';
$metadata = [];

if($this->useMetadataColumns && array_key_exists('metadata', $set)) {
$metadata = $set['metadata'];
unset($set['metadata']);


foreach ($metadata as $k => $v) {
$metadataStr .= ', '.$k.' = :'.$k;
}
}

$cmd = <<<EOT
UPDATE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
SET doc = :doc{$metadataStr}
$where;
EOT;

$args['doc'] = json_encode($set);
$args = array_merge($args, $metadata);

$this->transactional(function () use ($cmd, $args) {
$this->connection->prepare($cmd)->execute($args);
});
}

/**
* @param string $collectionName
* @param string $docId
Expand Down
58 changes: 58 additions & 0 deletions tests/PostgresDocumentStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,64 @@ public function it_adds_collection_with_multi_field_index_unique(): void
$this->assertStringStartsWith('CREATE UNIQUE INDEX', $indexes[1]['indexdef']);
}

/**
* @test
*/
public function it_replaces_a_doc()
{
$collectionName = 'test_replaces_a_doc';
$this->documentStore->addCollection($collectionName);

$doc = [
'some' => [
'prop' => 'foo',
'other' => [
'nested' => 42
]
],
'baz' => 'bat',
];

$docId = Uuid::uuid4()->toString();
$this->documentStore->addDoc($collectionName, $docId, $doc);

$doc = ['baz' => 'changed val'];

$this->documentStore->replaceDoc($collectionName, $docId, $doc);

$filter = new EqFilter('baz', 'changed val');

$filteredDocs = $this->documentStore->findDocs($collectionName, $filter);

$this->assertCount(1, $filteredDocs);
}

/**
* @test
*/
public function it_replaces_many()
{
$collectionName = 'test_replaces_many';
$this->documentStore->addCollection($collectionName);

$this->documentStore->addDoc($collectionName, Uuid::uuid4()->toString(), ['some' => ['prop' => 'foo', 'other' => ['prop' => 'bat']]]);
$this->documentStore->addDoc($collectionName, Uuid::uuid4()->toString(), ['some' => ['prop' => 'bar', 'other' => ['prop' => 'bat']]]);
$this->documentStore->addDoc($collectionName, Uuid::uuid4()->toString(), ['some' => ['prop' => 'bar']]);

$doc = ['some' => ['prop' => 'fuzz']];
$this->documentStore->replaceMany(
$collectionName,
new EqFilter('some.other.prop', 'bat'),
$doc
);

$filteredDocs = array_values(iterator_to_array($this->documentStore->findDocs($collectionName, new EqFilter('some.prop', 'fuzz'))));

$this->assertCount(2, $filteredDocs);
$this->assertEquals($doc, $filteredDocs[0]);
$this->assertEquals($doc, $filteredDocs[1]);
}

/**
* @test
*/
Expand Down