kettle/dynamodb-orm

A lightweight object-dynamodb mapper for PHP5

Installs: 12 781

Dependents: 0

Stars: 17

Forks: 12

Open Issues: 1

Language: PHP

v0.2.0 2015-06-23 02:02 UTC

README

Kettle is a lightweight object-dynamodb mapper for PHP. Kettle provides a simple interface to Amazon DynamoDB.

See Some Code

<?php
use Kettle\ORM;

$user = ORM::factory('User')->findOne(10);
$user->name = 'John';
$user->save();


$tweets = ORM::factory('Tweet')->where('user_id', 10)
                 ->findMany();

foreach ($tweets as $tweet) {
    echo $tweet->text . PHP_EOL;
}

1. Configuration

<?php
use Kettle\ORM;

ORM::configure("key",    'AWS_KEY');
ORM::configure("secret", 'AWS_SECRET');
ORM::configure("region", 'AWS_REGION');

// In order to use DynamoDB Local, you need to set "base_url".
// ORM::configure("base_url", 'http://localhost:8000/');

2. Create Model Class

<?php

class User extends ORM {
    protected $_table_name = 'user';
    protected $_hash_key   = 'user_id';
    protected $_schema = array(
      'user_id'    => 'N',  // user_id is number
      'name'       => 'S',  // name is string
      'age'        => 'N',
      'country'    => 'S',
     );
}

3. Create

<?php

$user = ORM::factory('User')->create();
$user->user_id = 1;
$user->name    = 'John';
$user->age     = 20;
$user->save();

4. Retrieve

<?php

$user = ORM::factory('User')->findOne(1);
echo $user->name. PHP_EOL;

print_r($user->asArray());

5. Update

<?php

$user = ORM::factory('User')->findOne(1);
$user->age = 21;
$user->save();

6. Delete

<?php

$user = ORM::factory('User')->findOne(1);
$user->delete();

7. Find

<?php

$tweets = ORM::factory('Tweets')
        ->where('user_id', 1)
        ->where('timestamp', '>', 1397264554)
        ->findMany();

foreach ($tweets as $tweet) {
     echo $tweet->text . PHP_EOL;
}

8. Find first record

<?php

$tweet = ORM::factory('Tweets')
        ->where('user_id', 1)
        ->where('timestamp', '>', 1397264554)
        ->findFirst();

echo $tweet->text . PHP_EOL;

9. Find by Global Secondary Index

<?php

$users = ORM::factory('User')
        ->where('country', 'Japan')
        ->where('age', '>=', 20)
        ->index('country-age-index')  // specify index name
        ->findMany();

10. Query Filtering

<?php

$tweets = ORM::factory('Tweets')
          ->where('user_id', 1)
          ->filter('is_deleted', 0) // using filter
          ->findMany();