Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Adds DynamoDB Adapter #333

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
fix delete and add renameAttribute
  • Loading branch information
ajwad-shaikh committed Oct 7, 2023
commit 67170aa83353d8b9daeac175bb9f5b8d0a71f460
30 changes: 28 additions & 2 deletions src/Database/Adapter/DynamoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ public function updateAttribute(string $collection, string $id, string $type, in
public function deleteAttribute(string $collection, string $id): bool
{
$collection = $this->filter($collection);
$id = $this->filter($id);
$queryParams = [
'TableName' => "{$this->getNamespace()}_{$collection}",
'ProjectionExpression' => '_id'
Expand All @@ -355,8 +356,8 @@ public function deleteAttribute(string $collection, string $id): bool
foreach ($items as $item) {
$updateParams = [
'TableName' => "{$this->getNamespace()}_{$collection}",
'Key' => ['_id' => $item['_id']], // Replace with your primary key
'UpdateExpression' => 'REMOVE ' . $id,
'Key' => ['_id' => [ 'N' => $item['_id']]], // Replace with your primary key
'UpdateExpression' => "REMOVE {$id}",
];
try {
$this->client->updateItem($updateParams);
Expand All @@ -377,6 +378,31 @@ public function deleteAttribute(string $collection, string $id): bool
*/
public function renameAttribute(string $collection, string $old, string $new): bool
{
$collection = $this->filter($collection);
$old = $this->filter($old);
$new = $this->filter($new);
$queryParams = [
'TableName' => "{$this->getNamespace()}_{$collection}",
'ProjectionExpression' => "_id, {$old}",
];
$items = ($this->client->query($queryParams))['Items'];
foreach ($items as $item) {
$oldItemValues = $item[$old];
$oldItemValue = null;
foreach ($oldItemValues as $value) {
$oldItemValue = $value;
}
$updateParams = [
'TableName' => "{$this->getNamespace()}_{$collection}",
'Key' => ['_id' => [ 'N' => $item['_id']['N']]], // Replace with your primary key
'UpdateExpression' => "SET {$new} = {$oldItemValue} REMOVE {$old}",
];
try {
$this->client->updateItem($updateParams);
} catch (DynamoDbException $e) {
return false;
}
}
return true;
}

Expand Down