- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13
 
Home
This module, dynamodb-php-wrapper, allows you to access DynamoDB more easily through interfaces shown below. Each interface is just a wrapper, so accepts same arguments original one does and you can get that information from the document provided by AWS if needed.
This section assumes that:
- There is a table named 'User', which has hash key named 'UserId' as Number.
 - There is a table named 'Friend', which has hash key named 'UserId' as Number and range key 'FriendUserId' as Number.
 
First of all, DynamoDBWrapper needs to be instantiated like this:
<?php
require_once '/path/to/aws-autoloader.php'; // provided by AWS
require_once '/path/to/DynamoDBWrapper.php'; // this module
$ddb = new DynamoDBWrapper(array(
    'key'    => 'YOUR_KEY',
    'secret' => 'YOUR_SECRET_KEY',
    'region' => 'SOME_REGION'
));then, you can access DynamoDB through this instance.
get is a wrapper of GetItem and will be used like this:
<?php
$key = array(
    'UserId' => array('N' => 2),
);
$result = $ddb->get('User', $key);
/*
Array
(
    [Name] => User2
    [UserId] => 2
)
*/batchGet is a wrapper of BatchGetItem and will be used like this:
<?php
$keys = array(
    array(
        'UserId' => array('N' => 2),
    ),
    array(
        'UserId' => array('N' => 3),
    ),
);
$result = $ddb->batchGet('User', $keys);
/*
Array
(   
    [0] => Array
        (   
            [Name] => Masayuki Tanaka
            [UserId] => 2
        )
    [1] => Array
        (   
            [Name] => Masayuki Tanaka
            [UserId] => 3
        )
)
*/batchGet can get more than 25 items by one call. This wrapper sends multiple requests to DynamoDb if needed, and merge them as a single result.
query is a wrapper of Query and will be used like this:
<?php
$keyConditions = array(
    'UserId' => array(
        'AttributeValueList' => array(
            array('N' => 2),
        ),
        'ComparisonOperator' => 'EQ'
    ),
);
$result = $ddb->query('Friend', $keyConditions);
/*
Array
(
    [0] => Array
        (
            [UserId] => 2
            [FriendUserId] => 3
        )
    [1] => Array
        (
            [UserId] => 2
            [FriendUserId] => 4
        )
    ...
)
*/query can accept options as $options, such as Limit, ExclusiveStartKey, ScanIndexForward and IndexName. If you use SLI, IndexName must be included in options.
scan is a wrapper of Scan and will be used like this:
<?php
$filter = array(
    'UserId' => array(
        'AttributeValueList' => array(
            array('N' => 1),
        ),
        'ComparisonOperator' => 'GT'
    ),
    'FriendUserId' => array(
        'AttributeValueList' => array(
            array('N' => 4),
            array('N' => 5),
        ),
        'ComparisonOperator' => 'IN'
    ),
);
$result = $ddb->scan('Friend', $filter);
/*
Array
(
    [0] => Array
        (
            [UserId] => 2
            [FriendUserId] => 4
        )
    [1] => Array
        (
            [UserId] => 2
            [FriendUserId] => 5
        )
    ...
)
*/count is a wrapper of Query with Select param as COUNT. This return the number of items query fetches and will be used like this:
<?php
$keyConditions = array(
    'UserId' => array(
        'AttributeValueList' => array(
            array('N' => 2),
        ),
        'ComparisonOperator' => 'EQ'
    ),
);
$result = $ddb->count('Friend', $keyConditions);
// 5put is a wrapper of putItem and will be used like this:
<?php
$item = array(
    'UserId' => array('N' => 1),
    'Name'   => array('S' => 'Masayuki Tanaka'),
);
$result = $ddb->put('User', $item);
// true, if successIf operation needs to be conditional, you can specify the expectation like this:
<?php
$item = array(
    'UserId' => array('N' => 1),
    'Name'   => array('S' => 'User1'),
);
$expected = array(
    'UserId' => array('Exists' => false)
);
$result = $ddb->put('User', $item, $expected);
// false, if UserId = 1 existsbatchPut is a wrapper of BatchWriteItem with put requests and will be used like this:
<?php
$items = array(
    array(
        'UserId' => array('N' => 2),
        'Name'   => array('S' => 'User2'),
    ),
    array(
        'UserId' => array('N' => 3),
        'Name'   => array('S' => 'User3'),
    ),
);
$result = $ddb->batchPut('User', $items);
// true, if successupdate is a wrapper of UpdateItem and will be used like this:
$key = array(
    'UserId' => array('N' => 2),
);
$update = array(
    'Name' => array(
        'Value' => array('S' => 'New User Name'),
        'Action' => 'PUT',
    ),
);
$result = $ddb->update('User', $key, $update);
/*
Array
(
    [Name] => New User Name
)
*/
If operation succeeded, the updated value will be returned.
If operation needs to be conditional, you can specify the expectation as 3rd arg.