quankim/laravel-dynamodb-eloquent-syntax

来自 https://github.com/baopham/laravel-dynamodb 的 DynamoDB 自定义 Eloquent 语法

1.1 2017-07-10 08:39 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:36:41 UTC


README

DynamoDB 的 Eloquent 语法

来自 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 quankim/laravel-dynamodb-eloquent-syntax   ```

  • 安装服务提供者

    // config/app.php
    
    'providers' => [
        ...
          QuanKim\DynamoDbEloquentSyntax\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', ''),
    ...

用法

  • 通过扩展模型 QuanKim\DynamoDbEloquentSyntax\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();

$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) {

    }
});

// Additional
// Where in
$model->where('id', 'in', [])
// Sub query
$model->where(function($q) {
    $q->where('id', 1)
        ->where('name', 'contains', 'a');
})->orWhere('email', 'contains', 'abc'));

// Delete all
$model->where('id', 'in', [1,2,3])->deleteAll();
// Increment/ Decrement a column
$model->increment('view_count', 1);
$model->decrement('total_product', 1);

// Paginate
$model->paginate([], $limit, $lastEvaluatedKey);
  • 或者,如果您想将数据库表与 DynamoDb 表同步,请使用特质 QuanKim\DynamoDbEloquentSyntax\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();

使用

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']);