Download the PHP package kitar/laravel-dynamodb without Composer
On this page you can find all versions of the php package kitar/laravel-dynamodb. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-dynamodb
Laravel DynamoDB
A DynamoDB based Eloquent model and Query builder for Laravel.
You can find an example implementation in kitar/simplechat.
- Motivation
- Installation
- Laravel
- Non-Laravel projects
- Sample data
- Model
- Extending the base model
- Basic Usage
- Retrieving all models
- Retrieving a model
- create()
- save()
- update()
- delete()
- increment() / decrement()
- Advanced Queries
- Authentication with model
- Register custom user provider
- Change auth config
- Query Builder
- Basic Usage
- getItem()
- putItem()
- updateItem()
- deleteItem()
- Projection Expressions
- select()
- Condition Expressions
- condition()
- conditionIn()
- conditionBetween()
- Working with Queries
- query() and keyCondition()
- keyConditionBetween()
- Sort order
- Working with Scans
- scan()
- Filtering the Results
- filter()
- filterIn()
- filterBetween()
- Paginating the Results
- exclusiveStartKey()
- Using Global Secondary Indexes
- index()
- Atomic Counter
- Batch Operations
- batchGetItem()
- batchPutItem()
- batchDeleteItem()
- batchWriteItem()
- DynamoDB-specific operators for condition() and filter()
- Comparators
- functions
- Debugging
- Testing
Motivation
- I want to use DynamoDB with Laravel. (e.g., authenticate with custom user provider)
- I want to use a simple API which doesn't need to worry about cumbersome things like manually handling Expression Attributes.
- I want to extend Laravel's code as much as I can to:
- Rely on Laravel's robust codes.
- keep the additional implementation simple and maintainable.
- I don't want to make it fully compatible with Eloquent because DynamoDB is different from relational databases.
- I'm longing for jessengers/laravel-mongodb. What if we have that for DynamoDB?
Installation
Install the package via Composer:
Laravel (6.x, 7.x, 8.x, 9.x, 10.x, 11.x)
Add dynamodb configs to config/database.php
:
Update the DB_CONNECTION
variable in your .env
file:
Non-Laravel projects
For usage outside Laravel, you can create the connection manually and start querying with Query Builder.
Sample data
Many of the example codes in this document are querying to DynamoDB's official sample data. If you want to try these codes with actual DynamoDB tables, it's handy to load them to your tables before.
Model
DynamoDB model extends Eloquent model so that we can use familiar features such as mutators, serialization, etc.
The main difference between Eloquent model and DynamoDB model is:
- Eloquent model
- Can handle relations.
- Forward calls to model (Eloquent) query builder. (e.g.,
create
,createOrFirst
where
with
)
- DynamoDB model
- Cannot handle relations.
- Forward calls to database (DynamoDB) query builder. (e.g.,
getItem
,putItem
,scan
,filter
)
Extending the base model
Most of the attributes are the same as the original Eloquent model, but there are few DynamoDB-specific attributes.
Name | Required | Description |
---|---|---|
table | yes | Name of the Table. |
primaryKey | yes | Name of the Partition Key. |
sortKey | Name of the Sort Key. | |
sortKeyDefault | Default value for the Sort Key. |
For example, if our table has only partition key, the model will look like this:
If our table also has sort key:
If we set sortKeyDefault
, it will be used when we instantiate or call find
without sort key.
Note that this model is implementing
Illuminate\Contracts\Auth\Authenticatable
and usingIlluminate\Auth\Authenticatable
. This is optional, but if we use them, we can use this model with authentication as well. For authentication, please refer to Authentication section) for more details.
Basic Usage
Retrieving all models
or alternatively,
You also can override the scan()
method to fit your needs, such as filtering models for single table design. For example:
DynamoDB can only handle result set up to 1MB per call, so we have to paginate if there are more results. see Paginating the Results for more details.
Retrieving a model
If the model has only partition key:
If the model also has sort key:
If the model has sort key and sortKeyDefault
is defined:
You also can modify the behavior of the find()
method to fit your needs. For example:
create()
save()
update()
delete()
increment() / decrement()
When we call increment()
and decrement()
, the Atomic Counter will be used under the hood.
We can also pass additional attributes to update.
Advanced Queries
We can use Query Builder functions through model such as query
scan
filter
condition
keyCondition
etc.
For example:
Please refer to Query Builder for the details.
Authentication with model
We can create a Custom User Provider to authenticate with DynamoDB. For the detail, please refer to Laravel's official document.
To use authentication with the model, the model should implement Illuminate\Contracts\Auth\Authenticatable
contract. In this section, we'll use the example User
model above.
Register custom user provider
After we prepare authenticatable model, we need to make the custom user provider. We can make it own (it's simple), but we'll use Kitar\Dynamodb\Model\AuthUserProvider
in this section.
To register custom user provider, add codes below in App/Providers/AuthServiceProvider.php
.
Change auth config
Then specify driver and model name for authentication in config/auth.php
.
api_token_name
and api_token_index
are optional, but we need them if we use api token authentication.
Registration Controller
You might need to modify the registration controller. For example, if we use Laravel Breeze, the modification looks like below.
There are two modifications. The first one is adding the closure validator for email
instead of unique
validator. The second one is using the save()
method to create user instead of the create()
method.
Query Builder
We can use Query Builder without model.
Or even outside Laravel.
If we query through the model, we don't need to specify the table name, and the response will be the model instance(s).
Basic Usage
getItem()
Instead of marshaling manually, pass a plain array.
Kitar\Dynamodb\Query\Grammar
will automatically marshal them before querying.
putItem()
updateItem()
Currently, we only support simple SET
and REMOVE
actions. If the attribute has value, it will be passed to SET
action. If the value is null, it will be passed to REMOVE
action.
deleteItem()
Projection Expressions
A Projection Expression is a string that identifies the attributes that web want. (It's like select
statement for SQL)
select()
We can specify Projection Expressions in the same manner as the original select
clause.
Condition Expressions
When we manipulate data in Amazon DynamoDB table, we use putItem
, updateItem
and DeleteItem
. We can use Condition Expressions to determine which items should be modified.
condition()
To specify Condition Expression, we use condition
clause. This works basically same as the original where
clause, but it's for Condition Expressions.
Note that we specify
attribute_not_exists
for the operator of condition. This is DynamoDB-specific operator which calledfunction
. See DynamoDB-specific operators for condition() and filter() for more details.
OR statements
AND statements
conditionIn()
conditionBetween()
Working with Queries
The Query operation in Amazon DynamoDB finds items based on primary key values.
query() and keyCondition()
When we query
, we must specify keyCondition
as well.
We can use some comparison operators for sort key, but we must use the equality condition for the partition key.
keyConditionBetween()
Sort order
query
results are always sorted by the sort key value. To reverse the order, set the ScanIndexForward
parameter to false
.
Note that DynamoDB's
ScanIndexForward
is a feature forquery
. It will not work withscan
.
Working with Scans
scan()
Filtering the Results
When we query
or scan
, we can filter results with Filter Expressions before it returned.
It can't reduce the amount of read capacity, but it can reduce the size of traffic data.
filter()
OR statement
AND statement
filterIn()
filterBetween()
Paginating the Results
A single query
or scan
only returns a result set that fits within the 1 MB size limit. If there are more results, we need to paginate.
exclusiveStartKey()
If there are more results, the response contains LastEvaluatedKey
.
We can pass this key to exclusiveStartKey
to get next results.
If you are using Query Builder through model, you can access to exclusiveStartKey
by:
Alternatively, you can achieve the same result using individual models; however, please be aware that this approach is planned to be deprecated in versions subsequent to v2.x.
Using Global Secondary Indexes
Some applications might need to perform many kinds of queries, using a variety of different attributes as query criteria. To support these requirements, you can create one or more global secondary indexes and issue query
requests against these indexes in Amazon DynamoDB.
index()
Use index
clause to specify Global Secondary Index name.
Atomic Counter
DynamoDB supports Atomic Counter. When we call increment()
and decrement()
through Model or Query Builder, Atomic Counter will be used under the hood.
We can also pass additional attributes to update.
Batch Operations
Batch operations can get, put or delete multiple items with a single call. There are some DynamoDB limitations (such as items count, payload size, etc), so please check the documentation in advance. (BatchGetItem, BatchWriteItem)
batchGetItem()
batchPutItem()
This is a handy method to batch-put items using
batchWriteItem
batchDeleteItem()
This is a handy method to batch-delete items using
batchWriteItem
batchWriteItem()
DynamoDB-specific operators for condition() and filter()
For condition
and filter
clauses, we can use DynamoDB's comparators and functions.
Comparators
=
<>
<
<=
>
>=
can be used in the form of:
functions
Available functions are:
size
function is not supported at this time.
Debugging
dryRun()
We can inspect what parameters (and which method) will actually send to DynamoDB by adding dryRun()
to our query. For example:
Our PHPUnit tests also use this feature, without actually calling DynamoDB
Testing
All versions of laravel-dynamodb with dependencies
illuminate/support Version ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
illuminate/container Version ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
illuminate/database Version ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
illuminate/hashing Version ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
aws/aws-sdk-php Version ^3.0