phoogkamer / dynamodb
此包已弃用且不再维护。未建议替代包。
DynamoDb 为您 Laravel 模型和辅助工具的包装器
0.4.0
2016-12-26 05:33 UTC
Requires
- aws/aws-sdk-php: ^3.0.0
- illuminate/database: ~5.1
- illuminate/support: ~5.1
Requires (Dev)
- phpunit/phpunit: ^5.0@dev
This package is not auto-updated.
Last update: 2024-01-04 13:49:28 UTC
README
支持所有键类型 - 主哈希键和组合键。
仅适用于高级用户。如果您不熟悉 Laravel、Laravel Eloquent 和 DynamoDB,那么我建议您首先熟悉这些。
版本 0.4 的重大更改
安装
-
Composer 安装
composer require baopham/dynamodb
-
安装服务提供者
// config/app.php 'providers' => [ ... BaoPham\DynamoDb\DynamoDbServiceProvider::class, ... ];
-
将 DynamoDb 配置放入
config/services.php
// config/services.php ... 'dynamodb' => [ 'key' => env('DYNAMODB_KEY'), 'secret' => env('DYNAMODB_SECRET'), 'region' => env('DYNAMODB_REGION'), 'local_endpoint' => env('DYNAMODB_LOCAL_ENDPOINT'), // see http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html 'local' => env('DYNAMODB_LOCAL'), // true or false? should use dynamodb_local or not? ], ...
使用方法
- 通过扩展
BaoPham\DynamoDb\DynamoDbModel
来扩展您的模型,然后您可以使用受支持的 Eloquent 方法。这里的想法是您可以在不更改查询的情况下切换回 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 表同步,请使用特性
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
-
DynamoDb 本地版本:2016-01-07_1.0
-
DynamoDb 本地测试模式由 DynamoDb 本地外壳 创建,位于 此处
要求
Laravel ^5.1
待办事项
- 升级一些旧属性:
AttributesToGet
、ScanFilter
、...
许可证
MIT