1. Go to this page and download the library: Download baopham/dynamodb library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Using scan operator, not too reliable since DynamoDb will only give 1MB total of data.
$model->all();
// Basically a scan but with limit of 1 item.
$model->first();
$nextPage = $query->after($last)->limit(2)->all();
// or
$nextPage = $query->afterKey($items->lastKey())->limit(2)->all();
// or (for query without index condition only)
$nextPage = $query->afterKey($last->getKeys())->limit(2)->all();
// update
$model->update($attributes);
// update asynchronously and wait on the promise for completion.
$model->updateAsync($attributes)->wait();
$model = new Model();
// Define fillable attributes in your Model class.
$model->fillableAttr1 = 'foo';
$model->fillableAttr2 = 'foo';
// DynamoDb doesn't support incremented Id, so you need to use UUID for the primary key.
$model->id = 'de305d54-75b4-431b-adb2-eb6b9e546014';
$model->save();
$model = new Model();
// Define fillable attributes in your Model class.
$model->fillableAttr1 = 'foo';
$model->fillableAttr2 = 'bar';
// DynamoDb doesn't support incremented Id, so you need to use UUID for the primary key.
$model->id = 'de305d54-75b4-431b-adb2-eb6b9e546014';
$model->saveAsync()->wait();
for($i = 0; $i < 10; $i++){
$model = new Model();
// Define fillable attributes in your Model class.
$model->fillableAttr1 = 'foo';
$model->fillableAttr2 = 'bar';
// DynamoDb doesn't support incremented Id, so you need to use UUID for the primary key.
$model->id = uniqid();
// Returns a promise which you can wait on later.
$promises[] = $model->saveAsync();
}
\GuzzleHttp\Promise\all($promises)->wait();
$model->delete();
$model->deleteAsync()->wait();
$model->chunk(10, function ($records) {
foreach ($records as $record) {
}
});
// Use this with caution unless your limit is small.
// DynamoDB has a limit of 1MB so if your limit is very big, the results will not be expected.
$model->where('name', 'foo')->take(3)->get();
$model->where('name', 'foo')->firstOrFail();
// for composite key
$model->where('id', 'foo')->where('id2', 'bar')->firstOrFail();
class Foo extends DynamoDbModel
{
protected static function boot()
{
parent::boot();
static::addGlobalScope('count', function (DynamoDbQueryBuilder $builder) {
$builder->where('count', '>', 6);
});
}
public function scopeCountUnderFour($builder)
{
return $builder->where('count', '<', 4);
}
public function scopeCountUnder($builder, $count)
{
return $builder->where('count', '<', $count);
}
}
$foo = new Foo();
// Global scope will be applied
$foo->all();
// Local scope
$foo->withoutGlobalScopes()->countUnderFour()->get();
// Dynamic local scope
$foo->withoutGlobalScopes()->countUnder(6)->get();
$model = new Model();
$model->where('id', 'foo')->removeAttribute('name', 'description', 'nested.foo', 'nestedArray[0]');
// Or
Model::find('foo')->removeAttribute('name', 'description', 'nested.foo', 'nestedArray[0]');
$raw = $model->where('count', '>', 10)->toDynamoDbQuery();
// $op is either "Scan" or "Query"
$op = $raw->op;
// The query body being sent to AWS
$query = $raw->query;
use BaoPham\DynamoDb\Facades\DynamoDb;
DynamoDb::table('articles')
// call set<key_name> to build the query body to be sent to AWS
->setFilterExpression('#name = :name')
->setExpressionAttributeNames(['#name' => 'author_name'])
->setExpressionAttributeValues([':name' => DynamoDb::marshalValue('Bao')])
->prepare()
// the query body will be sent upon calling this.
->scan(); // supports any DynamoDbClient methods (e.g. batchWriteItem, batchGetItem, etc.)
DynamoDb::table('articles')
->setIndexName('author_name')
->setKeyConditionExpression('#name = :name')
->setProjectionExpression('id, author_name')
// Can set the attribute mapping one by one instead
->setExpressionAttributeName('#name', 'author_name')
->setExpressionAttributeValue(':name', DynamoDb::marshalValue('Bao'))
->prepare()
->query();
DynamoDb::table('articles')
->setKey(DynamoDb::marshalItem(['id' => 'ae025ed8']))
->setUpdateExpression('REMOVE #c, #t')
->setExpressionAttributeName('#c', 'comments')
->setExpressionAttributeName('#t', 'tags')
->prepare()
->updateItem();
DynamoDb::table('articles')
->setKey(DynamoDb::marshalItem(['id' => 'ae025ed8']))
->prepare()
->deleteItem();
DynamoDb::table('articles')
->setItem(DynamoDb::marshalItem(['id' => 'ae025ed8', 'author_name' => 'New Name']))
->prepare()
->putItem();
// Or, instead of ::table()
DynamoDb::newQuery()
->setTableName('articles')
// Or access the DynamoDbClient instance directly
DynamoDb::client();
// pass in the connection name to get a different client instance other than the default.
DynamoDb::client('test');