137 lines
2.8 KiB
PHP
137 lines
2.8 KiB
PHP
<?php
|
|
use Aws\DynamoDb\DynamoDbClient;
|
|
use Aws\DynamoDb\Marshaler;
|
|
|
|
$client = new DynamoDbClient([
|
|
'region' => 'us-east-1',
|
|
'version' => 'latest',
|
|
'endpoint' => 'http://127.0.0.1:8002',
|
|
'credentials' => [
|
|
'key' => 'AKIAIOSFODNN7EXAMPLE',
|
|
'secret' => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', // value doesn't matter, jormundb only checks the key ID
|
|
],
|
|
]);
|
|
|
|
$marshaler = new Marshaler();
|
|
|
|
// Creates table.
|
|
try
|
|
{
|
|
$client->createTable([
|
|
'TableName' => 'Users',
|
|
'KeySchema' => [
|
|
['AttributeName' => 'userId', 'KeyType' => 'HASH'],
|
|
['AttributeName' => 'createdAt', 'KeyType' => 'RANGE'],
|
|
],
|
|
'AttributeDefinitions' => [
|
|
['AttributeName' => 'userId', 'AttributeType' => 'S'],
|
|
['AttributeName' => 'createdAt', 'AttributeType' => 'N'],
|
|
],
|
|
]);
|
|
echo "Table created.\n";
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
echo "Table already exists or error: " . $e->getMessage() . "\n";
|
|
}
|
|
|
|
// Rando
|
|
|
|
$users = [
|
|
[
|
|
'userId' => 'user-001',
|
|
'createdAt' => 1700000000,
|
|
'name' => 'Alice',
|
|
'email' => 'alice@example.com',
|
|
'age' => 30,
|
|
],
|
|
[
|
|
'userId' => 'user-001',
|
|
'createdAt' => 1700000100,
|
|
'name' => 'Alice (updated)',
|
|
'email' => 'alice2@example.com',
|
|
'age' => 31,
|
|
],
|
|
[
|
|
'userId' => 'user-002',
|
|
'createdAt' => 1700000200,
|
|
'name' => 'Bob',
|
|
'email' => 'bob@example.com',
|
|
'age' => 25,
|
|
],
|
|
];
|
|
|
|
foreach ($users as $user)
|
|
{
|
|
$client->putItem([
|
|
'TableName' => 'Users',
|
|
'Item' => $marshaler->marshalItem($user),
|
|
]);
|
|
}
|
|
echo "Items inserted.\n";
|
|
|
|
// Get one
|
|
|
|
$result = $client->getItem([
|
|
'TableName' => 'Users',
|
|
'Key' => $marshaler->marshalItem([
|
|
'userId' => 'user-001',
|
|
'createdAt' => 1700000000,
|
|
]),
|
|
]);
|
|
|
|
if (!empty($result['Item']))
|
|
{
|
|
$item = $marshaler->unmarshalItem($result['Item']);
|
|
echo "GetItem result:\n";
|
|
print_r($item);
|
|
}
|
|
|
|
// Get a bunch
|
|
|
|
$result = $client->query([
|
|
'TableName' => 'Users',
|
|
'KeyConditionExpression' => 'userId = :uid',
|
|
'ExpressionAttributeValues' => $marshaler->marshalItem([
|
|
':uid' => 'user-001',
|
|
]),
|
|
]);
|
|
|
|
echo "\nQuery results for user-001:\n";
|
|
foreach ($result['Items'] as $item)
|
|
{
|
|
print_r($marshaler->unmarshalItem($item));
|
|
}
|
|
|
|
// Search
|
|
|
|
$result = $client->query([
|
|
'TableName' => 'Users',
|
|
'KeyConditionExpression' => 'userId = :uid AND createdAt BETWEEN :start AND :end',
|
|
'ExpressionAttributeValues' => $marshaler->marshalItem([
|
|
':uid' => 'user-001',
|
|
':start' => 1700000000,
|
|
':end' => 1700000200,
|
|
]),
|
|
]);
|
|
|
|
echo "\nQuery with range:\n";
|
|
foreach ($result['Items'] as $item)
|
|
{
|
|
print_r($marshaler->unmarshalItem($item));
|
|
}
|
|
|
|
// Get all
|
|
|
|
$result = $client->scan([
|
|
'TableName' => 'Users',
|
|
]);
|
|
|
|
echo "\nScan all items:\n";
|
|
foreach ($result['Items'] as $item)
|
|
{
|
|
print_r($marshaler->unmarshalItem($item));
|
|
}
|
|
|
|
// ... Some random formatter keeps deleing my new line file ending
|