From 9baab738657da942dcd37bb98764669d2612906a Mon Sep 17 00:00:00 2001 From: biondizzle Date: Sat, 7 Mar 2026 17:15:57 -0500 Subject: [PATCH] adding client examples --- ClientExamples/php_client.php | 136 ++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 ClientExamples/php_client.php diff --git a/ClientExamples/php_client.php b/ClientExamples/php_client.php new file mode 100644 index 0000000..7cd4cb7 --- /dev/null +++ b/ClientExamples/php_client.php @@ -0,0 +1,136 @@ + '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