nordsoftware / lumen-dynamodb

此包已被废弃且不再维护。未建议替代包。

Lumen框架的DynamoDB模块。基于https://github.com/baopham/laravel-dynamodb

0.3.1 2016-04-11 09:28 UTC

This package is not auto-updated.

Last update: 2020-01-24 16:12:02 UTC


README

基于Bao Pham的laravel-dynamodb的Lumen框架对DynamoDB的实现。

先决条件

要本地安装DynamoDB,请参阅在您的计算机上运行DynamoDB

当DynamoDB设置好后,使用以下命令启动:java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

您现在可以使用DynamoDB artisan命令来管理DynamoDB

php artisan dynamodb:create --config=<TABLE_CONFIGURATION> # To create tables
php artisan dynamodb:delete --config=<TABLE_CONFIGURATION> -y # To delete tables

表配置文件应返回一个表配置数组

return [
    [
        'TableName'             => 'users',
        'AttributeDefinitions'  => [
            [
                'AttributeName' => 'id',
                'AttributeType' => 'S',
            ],
        ],
        'KeySchema'             => [
            [
                'AttributeName' => 'id',
                'KeyType'       => 'HASH',
            ],
        ],
        'ProvisionedThroughput' => [
            'ReadCapacityUnits'  => 10,
            'WriteCapacityUnits' => 20,
            'OnDemand'           => false,
        ],
    ],
    [
        'TableName'             => 'orders',
        'AttributeDefinitions'  => [
            [
                'AttributeName' => 'id',
                'AttributeType' => 'S',
            ],
        ],
        'KeySchema'             => [
            [
                'AttributeName' => 'id',
                'KeyType'       => 'HASH',
            ],
        ],
        'ProvisionedThroughput' => [
            'ReadCapacityUnits'  => 10,
            'WriteCapacityUnits' => 20,
            'OnDemand'           => false,
        ],
    ],
];

如果您不想使用配置文件,也可以覆盖Create/DeleteTablesCommand并将表定义放入protected static $tables = []数组中覆盖表。

请记得在您的Kernel.php文件中添加覆盖后的命令。

安装

// .env:
DYNAMODB_KEY=<AWS_KEY>
DYNAMODB_SECRET=<AWS_SECRET_KEY>
DYNAMODB_REGION=<AWS_REGION>
DYNAMODB_VERSION=latest
DYNAMODB_LOCAL_ENDPOINT=http://localhost:8000 # Only used for local DynamoDB


// config/services.php
return [
    ...
    'dynamodb' => [
        'key'      => env('DYNAMODB_KEY', 'dynamodb_local'),
        'secret'   => env('DYNAMODB_SECRET', 'secret'),
        'region'   => env('DYNAMODB_REGION', 'eu-central-1'),
        'version'  => env('DYNAMODB_VERSION', 'latest'),
        'endpoint' => env('DYNAMODB_LOCAL_ENDPOINT', 'http://localhost:8000'),
    ],
    ...
];

// bootstrap/app.php
...
$app->configure('services');
...
$app->register(Nord\Lumen\DynamoDb\DynamoDBServiceProvider::class);

使用

从\Nord\Lumen\DynamoDb\Domain\Model\DynamoDbModel扩展所有模型

您需要为模型设置以下属性以使DynamoDbModel正常工作

// The keys are defined when creating the table. If you are using only one primary key, set the $primaryKey,
// if using both primaryKey and sortKey, define the $compositeKey.
protected $primaryKey = '<primaryKey>'; // Ignore if using composite key.
protected $compositeKey = ['<primaryKey>', '<sortKey>']; // Ignore if you don't have a composite key.
protected $table = '<table_name>'; // Set the DynamoDB table this model uses
// If using global or local indexes, define the key => indexName values here.
protected $dynamoDbIndexKeys = [
    '<globalIndexKey>' => '<globalIndexName>',
    '<localIndexKey>' => '<localIndexName>',
];

// Set this to be able to mass assign attributes.
protected $fillable = ['email', 'name'];
protected $guarded = ['address'];

使用模型的方式基本上与Eloquent相同

$model = DynamoDbModel::find(1); // Find a model with the primary key 1
// Using where:
$model = DynamoDbModel::where(['email' => 'test@example.com']);
$model->get()->first(); // Returns the first record.

$model = new DynamoDbModel(['name' => 'Demo user', 'email' => 'test@example.com']); // Fillable attributes.
$model->setAddress('Teststreet 1'); // Set the guarded attribute.
$model->save();

许可证

LICENSE