quankim/dynamodb

DynamoDb 包装器,用于您的 Laravel 模型和助手 => 来自 https://github.com/baopham/laravel-dynamodb 的分支

0.79 2017-01-11 09:07 UTC

README

来自 https://github.com/baopham/laravel-dynamodb

Latest Stable Version Total Downloads Latest Unstable Version License

支持所有键类型 - 主哈希键和组合键。

仅限高级用户。如果您不熟悉 Laravel、Laravel Eloquent 和 DynamoDB,则建议您首先熟悉这些内容。

版本 0.4 的重大更改

  • 如果您正在使用 v0.3 及以下版本,请参阅 此处
  • 要升级到 v0.4,请参阅 迁移说明

安装

  • Composer 安装

    composer require baopham/dynamodb
  • 安装服务提供者

    // config/app.php
    
    'providers' => [
        ...
        BaoPham\DynamoDb\DynamoDbServiceProvider::class,
        ...
    ];
  • 将 DynamoDb 配置放在 config/aws.php

    // config/aws.php
    ...
    'credentials' => [
        'key'    => env('AWS_ACCESS_KEY_ID', ''),
        'secret' => env('AWS_SECRET_ACCESS_KEY', ''),
    ],
    'region' => env('AWS_REGION', 'us-east-1'),
    'version' => 'latest',
    'endpoint' => env('AWS_ENDPOINT', ''),
    ...

使用方法

  • 使用 BaoPham\DynamoDb\DynamoDbModel 扩展您的模型,然后您可以使用支持的方法。这里的想法是,您可以在不更改查询的情况下切换回 Eloquent。

支持的方法

// find and delete
$model->find(<id>);
$model->delete();

// Using getIterator(). If 'key' is the primary key or a global/local index and the condition is EQ, will use 'Query', otherwise 'Scan'.
$model->where('key', 'key value')->get();

// See BaoPham\DynamoDb\ComparisonOperator
$model->where(['key' => 'key value']);
// Chainable for 'AND'. 'OR' is not supported.
$model->where('foo', 'bar')
    ->where('foo2', '!=' 'bar2')
    ->get();

// 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();

// update
$model->update($attributes);

$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();

// chunk
$model->chunk(10, function ($records) {
    foreach ($records as $record) {

    }
});
  • 或者,如果您想将您的数据库表与 DynamoDb 表同步,使用 trait BaoPham\DynamoDb\ModelTrait,它在模型保存后将调用 PutItem

索引

如果您的表有索引,请确保在您的模型类中声明它们,如下所示

/**
 * Indexes.
 * [
 *     'simple_index_name' => [
 *          'hash' => 'index_key'
 *     ],
 *     'composite_index_name' => [
 *          'hash' => 'index_hash_key',
 *          'range' => 'index_range_key'
 *     ],
 * ].
 *
 * @var array
 */
protected $dynamoDbIndexKeys = [
    'count_index' => [
        'hash' => 'count'
    ],
];

请注意,当键存在于多个索引中时,索引的顺序很重要。
例如,我们有这个

$this->where('user_id', 123)->where('count', '>', 10)->get();

with

protected $dynamoDbIndexKeys = [
    'count_index' => [
        'hash' => 'user_id',
        'range' => 'count'
    ],
    'user_index' => [
        'hash' => 'user_id',
    ],
];

将使用 count_index

protected $dynamoDbIndexKeys = [
    'user_index' => [
        'hash' => 'user_id',
    ],
    'count_index' => [
        'hash' => 'user_id',
        'range' => 'count'
    ]
];

将使用 user_index

组合键

要使用组合键与您的模型一起使用

  • $compositeKey 设置为包含键属性名称的数组,例如
protected $primaryKey = ['customer_id'];
protected $compositeKey = ['customer_id', 'agent_id'];
  • 要查找具有组合键的记录
$model->find(['id1' => 'value1', 'id2' => 'value2']);

测试

运行

$ java -Djava.library.path=./DynamoDBLocal_lib -jar dynamodb_local/DynamoDBLocal.jar --port 3000
$ ./vendor/bin/phpunit

要求

Laravel ^5.1

TODO

  • 升级一些旧属性:AttributesToGetScanFilter、...

许可证

MIT

作者和贡献者