Download the PHP package justraviga/laravel-dynamodb-extreme without Composer
On this page you can find all versions of the php package justraviga/laravel-dynamodb-extreme. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download justraviga/laravel-dynamodb-extreme
More information about justraviga/laravel-dynamodb-extreme
Files in justraviga/laravel-dynamodb-extreme
Package laravel-dynamodb-extreme
Short Description JustRaviga DynamoDb access package
License MIT
Informations about the package laravel-dynamodb-extreme
DynamoDb Query Builder
This is a Query Builder package for DynamoDb access. Inspired by other versions that didn't quite feel eloquent enough (see what I did there?), this one has the following features, just like Laravel:
Get a single Model instance from DynamoDb
Or, to throw Illuminate\Database\Eloquent\ModelNotFoundException
if no results are returned:
Get a Collection of Model instances from DynamoDb
Notes:
- Only a single Model type is supported here. You must make sure your query will only return models of the same type.
- An Exception will be thrown if a property found in the database is not in the $fillable array on the model.
- DynamoDb only supports exact matches on Partition keys, and
<
,<=
,=
,>=
,>
,begins_with
, andbetween
matches on Sort Keys.
You can optionally sort on the sortKey and limit the number of results:
If you know you're going to exceed the 1mb query size limit of DynamoDb, you can use the getAll()
method to make
many queries until the entire result set is returned:
You can also get a specific set of results combining limit()
and after()
:
A shortcut for this is built into the paginate
method:
Get a collection of models using a Secondary Index
Provided the secondary index is configured (see below), it will be detected from the fields you are querying.
Create a Model instance in DynamoDb
Immediately persist the model with create
:
Build the model in memory without making a database query with make
:
Alternatively use the new Model()
syntax:
Accessing Model attributes
Once you've created your model, attributes are accessed the same as you would with Laravel, using object property syntax:
You can update many properties at the same time using the fill
method:
These changes are not automatically persisted back to Dynamo, you need to save them manually:
or
or even, to combine fill
and save
:
Setup and global config
Environment variables
DYNAMODB_REGION
defaults tolocalhost
, should be set to your main DynamoDb instance region (eu-west-2, for example)DYNAMODB_VERSION
defaults tolatest
, should be set to the version of your DynamoDb instance if you need itDYNAMODB_KEY
your DynamoDb access key (username)DYNAMODB_SECRET
your DynamoDb secret (password)DYNAMODB_ENDPOINT
defaults tohttp://localhost:8000
, the address of your DynamoDb installationDYNAMODB_TABLE
to define a default table for all models (useful when working with a single-table design in a specific application)DYNAMODB_CONSISTENT_READ
defaults totrue
, use to set a default consistent read value (can still be overwritten by specific models)DYNAMODB_LOG_QUERIES
defaults tofalse
, use to add logging for all DynamoDb queries made (the json object being sent to Dynamo will be logged)
Configuration options
The config file can be exported with Laravel's publish command: php artisan vendor:publish
, it looks like this:
Apart from the environment-based config, here you can specify defaults for partition key, sort key, and global secondary indexes. These are intended to be sensible defaults based on general usage patterns of DynamoDb.
Model Configuration
Creating a model is easy. At a bare minimum, you just need to define the table name, the partitionKey and sortKey, and an array of "fillable" attributes (note the partition and sort keys need to be fillable!):
To write more verbose code, you can create a mapping from internal/database fields to friendly attributes by overriding
the fieldMappings
protected property. The mappings are applied when fetching from the database and when saving to the
database. The rest of the time, you always use the mapped property name.
By default, all models' Partition Keys are built with the model class name followed by a #
then a UUID-7 (time-ordered UUID).
Sort Keys default to the model name.
For example:
You can also change these default values when creating new models by overriding these methods:
Related models
You can configure related models that share the same Partition Key with some minor extra setup:
By default, this uses the Partition key of the parent model, and matches on the Sort key with a "begins_with" query against the child model's class name followed by a hash (#), something like begins_with(sk, 'CHILDMODEL#')
.
The matching itself can be configured on the child models by overriding the relationSearchParams
method:
The related models can be accessed as a Collection using a property of the same name as the relationship method, e.g:
"inline" relations
In addition to using different rows in a DynamoDb table to hold related models, you can use a JSON attribute on a record as a relation. We call this an "inline relation" and they can be accessed the same way as other relations:
If you want to use a secondary index on your table, just define the Partition and Sort Key names in the $indexes
array.
Be sure to add them to the $fillable
array as well.
You can define mappings from secondary indexes to friendly model properties in the $fieldMappings
array too.
Minimum Requirements
- PHP 8.2
- Laravel 10.0
Wishlist
The things we might like to include (eventually) but didn't have time to properly consider:
Wrapping DynamoDb responses in a cache layer
Optionally have models fetched from DynamoDb stored in memory for the duration of the request so multiple calls to the same partition/sort key return the same data without querying Dynamo.
Attribute casts
Similar to the Laravel model $casts array, attributes should be coerced to/from these data types when fetching and saving with DynamoDb.
Extend the Collection class to include filter methods based on DynamoDb models
Potential for new methods on the Collection class for pagination based on the Last Evaluated Key from DynamoDb.
Better documentation 🙈
Docs can always be improved.
Concurrent requests
Figure out how to make many concurrent requests that can be awaited and only continue execution once all of the requests have returned their data.
Table generation in code
Essentially Laravel's migration system for DynamoDb tables
Cascade deletions
Delete related models after deleting a parent model