Last active
October 2, 2018 22:41
-
-
Save rolebi/e0c56113f5e36246bbf5f84d3e94627b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$client = $this->get('aws_dynamodb.client'); | |
$client->createTable([ | |
'TableName' => 'test_set_management', | |
'ProvisionedThroughput' => [ | |
'ReadCapacityUnits' => 1, | |
'WriteCapacityUnits' => 1, | |
], | |
'AttributeDefinitions' => [ | |
['AttributeName' => 'id', 'AttributeType' => 'S'], | |
], | |
'KeySchema' => [ | |
['AttributeName' => 'id', 'KeyType' => 'HASH'], | |
], | |
]); | |
$client->waitUntil('TableExists', [ | |
'TableName' => 'test_set_management', | |
'@waiter' => [ | |
'delay' => 1, | |
'maxAttempts' => 20, | |
], | |
]); | |
$baseQuery = [ | |
'TableName' => 'test_set_management', | |
'Key' => [ | |
'id' => ['S' => 'foo'], | |
] | |
]; | |
$client->updateItem($baseQuery + [ | |
'UpdateExpression' => 'SET colors = :colors', | |
'ExpressionAttributeValues' => [ | |
':colors' => ['SS' => ['red', 'blue', 'green']], | |
] | |
]); | |
$item = $client->getItem($baseQuery + [ | |
'ConsistentRead' => true | |
]); | |
var_dump($item->search('Item.colors')); // blue, green, red | |
$client->updateItem($baseQuery + [ | |
'UpdateExpression' => 'ADD colors :colors', | |
'ExpressionAttributeValues' => [ | |
':colors' => ['SS' => ['black']], | |
], | |
]); | |
// unique set, adding same value two times in a row does not duplicate value in set | |
// you can also add multiple value, basically it merges the two given unique sets together | |
$client->updateItem($baseQuery + [ | |
'UpdateExpression' => 'ADD colors :colors', | |
'ExpressionAttributeValues' => [ | |
':colors' => ['SS' => ['black', 'purple']], | |
], | |
]); | |
$item = $client->getItem($baseQuery + [ | |
'ConsistentRead' => true | |
]); | |
var_dump($item->search('Item.colors')); // black, blue, green, purple, red | |
$client->updateItem($baseQuery + [ | |
'UpdateExpression' => 'DELETE colors :colors', | |
'ExpressionAttributeValues' => [ | |
':colors' => ['SS' => ['green']], | |
], | |
]); | |
// deleting works the same way, you exclude one set from the other | |
// entry that are not present will be ignored, the rest will be removed | |
$client->updateItem($baseQuery + [ | |
'UpdateExpression' => 'DELETE colors :colors', | |
'ExpressionAttributeValues' => [ | |
':colors' => ['SS' => ['green', 'purple']], | |
], | |
]); | |
$item = $client->getItem($baseQuery + [ | |
'ConsistentRead' => true | |
]); | |
var_dump($item->search('Item.colors')); // black, blue, red | |
$client->deleteTable(['TableName' => 'test_set_management']); | |
$client->waitUntil('TableNotExists', [ | |
'TableName' => 'test_set_management', | |
'@waiter' => [ | |
'delay' => 5, | |
'maxAttempts' => 20, | |
], | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment