Coder Social home page Coder Social logo

vanquang9387 / laravel-dynamodb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from baopham/laravel-dynamodb

0.0 2.0 0.0 91.73 MB

Eloquent syntax for DynamoDB

Home Page: https://packagist.org/packages/baopham/dynamodb

JavaScript 7.79% PHP 92.21%

laravel-dynamodb's Introduction

laravel-dynamodb

Latest Stable Version Total Downloads Latest Unstable Version License

Supports all key types - primary hash key and composite keys.

For advanced users only. If you're not familiar with Laravel, Laravel Eloquent and DynamoDB, then I suggest that you get familiar with those first.

Breaking Changes for v0.4 - if you're using v0.3 and below, please see here

Install

  • Composer install

    composer require baopham/dynamodb
  • Install service provider:

    // config/app.php
    
    'providers' => [
        ...
        BaoPham\DynamoDb\DynamoDbServiceProvider::class,
        ...
    ];
  • Put DynamoDb config in 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?
        ],
        ...

Usage

  • Extends your model with BaoPham\DynamoDb\DynamoDbModel, then you can use Eloquent methods that are supported. The idea here is that you can switch back to Eloquent without changing your queries.

Supported methods:

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

    }
});
  • Or if you want to sync your DB table with a DynamoDb table, use trait BaoPham\DynamoDb\ModelTrait, it will call a PutItem after the model is saved.

Indexes

If your table has indexes, make sure to declare them in your model class like so

   /**
     * 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'
        ],
    ];

Note that order of index matters when a key exists in multiple indexes.
For example, we have this

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

with

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

will use count_index.

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

will use user_index.

Composite Keys

To use composite keys with your model:

  • Set $compositeKey to an array of the attributes names comprising the key, e.g.
protected $primaryKey = ['customer_id'];
protected $compositeKey = ['customer_id', 'agent_id'];
  • To find a record with a composite key
$model->find(['id1' => 'value1', 'id2' => 'value2']);

Test

Run:

$ java -Djava.library.path=./DynamoDBLocal_lib -jar dynamodb_local/DynamoDBLocal.jar --port 3000
$ ./vendor/bin/phpunit
  • DynamoDb local version: 2016-01-07_1.0

  • DynamoDb local schema for tests created by the DynamoDb local shell is located here

Requirements

Laravel ^5.1

TODO

  • Upgrade a few legacy attributes: AttributesToGet, ScanFilter, ...

License

MIT

Author and Contributors

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.