PHP code example of kitar / laravel-dynamodb
1. Go to this page and download the library: Download kitar/laravel-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/ */
kitar / laravel-dynamodb example snippets
'connections' => [
'dynamodb' => [
'driver' => 'dynamodb',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'token' => env('AWS_SESSION_TOKEN', null),
'endpoint' => env('DYNAMODB_ENDPOINT', null),
'prefix' => '', // table prefix
],
...
],
$connection = new Kitar\Dynamodb\Connection([
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'token' => env('AWS_SESSION_TOKEN', null),
'endpoint' => env('DYNAMODB_ENDPOINT', null),
'prefix' => '', // table prefix
]);
$connection->table('your-table')->...
use Kitar\Dynamodb\Model\Model;
class ProductCatalog extends Model
{
protected $table = 'ProductCatalog';
protected $primaryKey = 'Id';
protected $fillable = ['Id', 'Price', 'Title'];
}
use Kitar\Dynamodb\Model\Model;
class Thread extends Model
{
protected $table = 'Thread';
protected $primaryKey = 'ForumName';
protected $sortKey = 'Subject';
protected $fillable = ['ForumName', 'Subject'];
}
use Kitar\Dynamodb\Model\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class User extends Model implements AuthenticatableContract
{
use Authenticatable;
protected $table = 'User';
protected $primaryKey = 'email';
protected $sortKey = 'type';
protected $sortKeyDefault = 'profile';
protected $fillable = [
'name', 'email', 'password', 'type',
];
}
$products = ProductCatalog::scan();
$products = ProductCatalog::all();
public static function scan($exclusiveStartKey = null, $sort = 'asc', $limit = 50)
{
return static::index('GSI1')
->keyCondition('GSI1PK', '=', 'PRODUCT#')
->keyCondition('GSI1SK', 'begins_with', 'PRODUCT#')
->exclusiveStartKey($exclusiveStartKey)
->scanIndexForward($sort == 'desc' ? false : true)
->limit($limit)
->query();
}
ProductCatalog::find(101);
Thread::find([
'ForumName' => 'Amazon DynamoDB', // Partition key
'Subject' => 'DynamoDB Thread 1' // Sort key
]);
User::find('[email protected] '); // Partition key. sortKeyDefault will be used for Sort key.
public static function find($userId)
{
return parent::find([
'PK' => str_starts_with($userId, 'USER#') ? $userId : 'USER#'.$userId,
'SK' => 'USER#',
]);
}
$user = User::create([
'email' => '[email protected] ',
'type' => 'profile' // Sort key. If we don't specify this, sortKeyDefault will be used.
]);
$user = new User([
'email' => '[email protected] ',
'type' => 'profile'
]);
$user->save();
$user->name = 'foo';
$user->save();
$user->update([
'name' => 'foobar'
]);
$user->delete();
$user->increment('views', 1);
$user->decrement('views', 1);
$user->increment('views', 1, [
'last_viewed_at' => '...',
]);
Thread::keyCondition('ForumName', '=', 'Amazon DynamoDB')
->keyCondition('Subject', 'begins_with', 'DynamoDB')
->filter('Views', '=', 0)
->query();
use Kitar\Dynamodb\Model\AuthUserProvider;
...
public function boot()
{
$this->registerPolicies();
Auth::provider('dynamodb', function ($app, array $config) {
return new AuthUserProvider(
$app['hash'],
$config['model'],
$config['api_token_name'] ?? null,
$config['api_token_index'] ?? null
);
});
}
'providers' => [
// Eloquent
// 'users' => [
// 'driver' => 'eloquent',
// 'model' => App\User::class,
// ],
// DynamoDB
'users' => [
'driver' => 'dynamodb',
'model' => App\User::class,
'api_token_name' => 'api_token',
'api_token_index' => 'api_token-index'
],
],
class RegisteredUserController extends Controller
{
...
public function store(Request $request)
{
$request->validate([
'name' => ' $fail('The '.$attribute.' has already been taken.');
}
}],
'password' => ' return redirect(RouteServiceProvider::HOME);
}
}
$result = DB::table('Thread')->scan();
$connection = new Kitar\Dynamodb\Connection([
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'token' => env('AWS_SESSION_TOKEN', null),
'endpoint' => env('DYNAMODB_ENDPOINT', null),
'prefix' => '', // table prefix
]);
$result = $connection->table('Thread')->scan();
$threads = Thread::scan();
$response = DB::table('ProductCatalog')
->getItem(['Id' => 101]);
DB::table('Thread')
->putItem([
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'New discussion thread',
'Message' => 'First post in this thread',
'LastPostedBy' => '[email protected] ',
'LastPostedDateTime' => '201603190422'
]);
DB::table('Thread')
->key([
'ForumName' => 'Laravel',
'Subject' => 'Laravel Thread 1'
])->updateItem([
'LastPostedBy' => null, // REMOVE
'Replies' => null, // REMOVE
'Message' => 'Updated' // SET
]);
DB::table('Thread')
->deleteItem([
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'New discussion thread'
]);
$response = DB::table('ProductCatalog')
->select('Price', 'Title')
->getItem(['Id' => 101]);
DB::table('ProductCatalog')
->condition('Id', 'attribute_not_exists')
->putItem([
'Id' => 101,
'ProductCategory' => 'Can I overwrite?'
]);
DB::table('ProductCatalog')
->condition('Id', 'attribute_not_exists')
->orCondition('Price', 'attribute_not_exists)
->putItem([...]);
DB::table('ProductCatalog')
->condition('Id', 'attribute_not_exists')
->condition('Price', 'attribute_not_exists)
->putItem([...]);
ProductCatalog::key(['Id' => 101])
->conditionIn('ProductCategory', ['Book', 'Bicycle'])
->updateItem([
'Description' => 'updated!'
]);
ProductCatalog::key(['Id' => 101])
->conditionBetween('Price', [0, 10])
->updateItem([
'Description' => 'updated!'
]);
$response = DB::table('Thread')
->keyCondition('ForumName', '=', 'Amazon DynamoDB')
->keyCondition('Subject', 'begins_with', 'DynamoDB')
->query();
$response = DB::table('Thread')
->keyCondition('ForumName', '=', 'Amazon DynamoDB')
->keyConditionBetween('Subject', ['DynamoDB Thread 1', 'DynamoDB Thread 2'])
->query();
$response = DB::table('Thread')
->keyCondition('ForumName', '=', 'Amazon DynamoDB')
->scanIndexForward(false)
->query();
$response = DB::table('Thread')->scan();
$response = DB::table('Thread')
->filter('LastPostedBy', '=', 'User A')
->scan();
$response = DB::table('Thread')
->filter('LastPostedBy', '=', 'User A')
->orFilter('LastPostedBy', '=', 'User B')
->scan();
$response = DB::table('Thread')
->filter('LastPostedBy', '=', 'User A')
->filter('Subject', 'begins_with', 'DynamoDB')
->scan();
$response = DB::table('Thread')
->filterIn('LastPostedBy', ['User A', 'User B'])
->scan();
$response = DB::table('ProductCatalog')
->filterBetween('Price', [0, 100])
->scan();
$response = DB::table('ProductCatalog')
->limit(5)
->scan();
$response['LastEvaluatedKey']; // array
$response = DB::table('ProductCatalog')
->exclusiveStartKey($response['LastEvaluatedKey'])
->limit(5)
->scan();
$products = ProductCatalog::limit(5)->scan();
$products->getLastEvaluatedKey(); // array
$products->first()->meta()['LastEvaluatedKey']; // array
$response = DB::table('Reply')
->index('PostedBy-Message-index')
->keyCondition('PostedBy', '=', 'User A')
->keyCondition('Message', '=', 'DynamoDB Thread 2 Reply 1 text')
->query();
DB::('Thread')->key([
'ForumName' => 'Laravel',
'Subject' => 'Laravel Thread 1'
])->increment('Replies', 2);
DB::('Thread')->key([
'ForumName' => 'Laravel',
'Subject' => 'Laravel Thread 1'
])->increment('Replies', 2, [
'LastPostedBy' => 'User A',
]);
DB::table('Thread')
->batchGetItem([
[
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 1'
],
[
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 2'
]
]);
DB::table('Thread')
->batchPutItem([
[
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 3'
],
[
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 4'
]
]);
DB::table('Thread')
->batchDeleteItem([
[
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 1'
],
[
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 2'
]
]);
DB::table('Thread')
->batchWriteItem([
[
'PutRequest' => [
'Item' => [
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 3'
]
]
],
[
'DeleteRequest' => [
'Key' => [
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 1'
]
]
]
]);
filter($key, $comparator, $value);
filter($key, 'attribute_exists');
filter($key, 'attribute_not_exists');
filter($key, 'attribute_type', $type);
filter($key, 'begins_with', $value);
filter($key, 'contains', $value);
// via Model
$request = ProductCatalog::dryRun()->getItem(['Id' => 101]);
// via Query Builder
$request = DB::table('ProductCatalog')->dryRun()->getItem(['Id' => 101]);
dump($request);