helium / laravel-dynamodb
Plugin for creating DynamoDB migrations for Laravel
Requires
This package is auto-updated.
Last update: 2020-04-29 16:46:49 UTC
README
Plugin for creating DynamoDB "migrations" for Laravel
Installation
composer require helium/laravel-dynamodb
Usage
Configuration
Add the following line to your package service providers in app.php
Helium\DynamoDb\DynamoDbServiceProvider::class,
Add the following lines to your database connections in database.php
'dynamodb' => [
'group' => env('APP_NAME'),
'region' => env('DYNAMO_REGION'),
'version' => env('DYNAMO_VERSION', 'latest'),
//'endpoint' => env('DYNAMO_HOST'),
'credentials' => [
'key' => env('DYNAMO_KEY'),
'secret' => env('DYNAMO_SECRET'),
]
],
You can uncomment the endpoint key/value pair if you are using an endpoint other than the AWS default.
Optionally, set your default connection to dynamodb.
Migration
Create a migration
php artisan make:migration
Replace use Illuminate\Database\Schema\Blueprint; with use Helium\DynamoDb\Schema\Blueprint;
If you did not set dynamodb as your default connection, specify the correct connection
Schema::connection('dynamodb')->...
Supported schema functions
create($table, $callback)- Create new tabledrop($table)- Drop tabledropIfExists($table)- Alias fordrop
Supported blueprint functions
string($name)- Create a new attribute with typeSnumber($name)- Create a new attribute with typeNbinary($name)- Create a new attribute with typeBpartitionKey($name)- Set the table partition key using a previously created attribute namesortKey($name)- Set the table sort key using a previously created attribute nameglobalSecondaryIndex($name)- Create a new global secondary indexpartitionKey($name)- Set the index partition key using a previously created attribute namesortKey($name)- Set the index sort key using a previously created attribute name
localSecondaryIndex($name)- Create a new local secondary indexsortKey($name)- Set the index sort key using a previously created attribute name- Note that the partition key may not be set manually, and is set automatically to the table partition key in accordance with AWS specifications
Example
public function up() {
Schema::connection('dynamodb')->create('Account', function (Blueprint $table) {
$table->number('id');
$table->string('email');
$table->binary('active');
$table->partitionKey('id');
$table->sortKey('email);
$table->globalSecondaryIndex('byEmail')
->partitionKey('email')
->sortKey('active');
$table->localSecondaryIndex('byActive')
->sortKey('active');
});
}
public function down() {
Schema::connection('dynamodb')->dropIfExists('Account');
}
Note that the table which is created will be given the format ProjectName.TableName. This is to provide some
organization and to prevent duplicate tables between projects since DynamoDB does not support separate "databases" or
"collections" within a single instance, as is the standard with many other database systems.