An AWS DynamoDB driver for Spatie Laravel Event Sourcing
blackfrog/laravel-event-sourcing-dynamodb is a Laravel package for an aws dynamodb driver for spatie laravel event sourcing.
It currently has 3 GitHub stars and 11 downloads on Packagist.
Install it with composer require blackfrog/laravel-event-sourcing-dynamodb.
Discover more Laravel packages by blackfrog
or browse all Laravel packages to compare alternatives.
Last updated
! Work In Progress ! Not yet suitable for use. Please wait for the first SemVer versioned release.
A DynamoDB driver for spatie/laravel-event-sourcing allowing for a
serverless approach to your event and snapshot data storage.
TODOs for first release:
StoredEventRepository and SnapshotRepository.EloquentStoredEventRepository:store() implementation converts a null $uuid argument to an empty
string for storage. DynamoDB does not allow empty strings, so we store this as the string 'null'.persistInTransaction() on AggregateRoots, the package doesn't use this method
out of the box itself, but you might. Read More."spatie/laravel-event-sourcing": "^7.3.3",DynamoDB brings a number of advantages:
When it's not right for you:
aggregate_uuid as HASH (partition) key and id as RANGE key.StoredEventRepository interface.id as both the HASH and RANGE keys supports fetching events without their aggregate
uuid while preserving their order, both find($id) and retrieveAll(null) use this.version_id (composite of version and id) as RANGE to support getLatestAggregateVersion(). This
can be changed to a GSI if you definitely don't need the read consistency feature. See DynamoDB Limitations for more
info.aggregate_uuid as the HASH key and id_part as the RANGE key.id_part is a composite of a randomly generated int id and the 'part number' of the snapshot. This does two things,
it means that the most recent snapshots are returned first when queried (using a DESC sort) and that the snapshot
parts are returned in the correct order if the snapshot required more than one DynamoDB item to be stored.serialize() is used on the output of you aggregate root's getState() method and the results are then base64
encoded and split into multiple parts if too large to fit inside a single DynamoDB item (400KB limit).read_consistency config key (defaults to false.) This only applies to methods where you pass an
aggregate root UUID as an argument. Some method calls on the EventRepository such as find($id)
and retrieveAll(null) will remain eventually consistent.->remember() on the collection.aggregate_uuid-version-id-index to the GlobalSecondaryIndexes before creating tables.PAY_PER_REQUEST billing mode behaviour and doesn't currently support provisioned throughput.
For example, there's no handling of throughput exceeded exceptions nor a wait/retry mechanism for this.AggregateRoot base class called persistInTransaction(), this creates a
Laravel DB transaction around the storage of events. There's currently no way for the Repository to know about this
transaction, so we aren't able to implement it for DynamoDB. This is not used by the package internally, so you only
need to be aware if you use this method yourself.int Ids for events. The Spatie package interfaces expect integer ids and the
logic expects them to be incrementing. DynamoDB does not provide incrementing ids.IdGenerator interface and
updating the config key id_generator. 'id_generator' => TimeStampIdGenerator::class,
TimeStampIdGenerator by
implementing the TimestampProvider interface and updating the config key id_timestamp_provider. If you return a
shorter timestamp (e.g. seconds or milliseconds) the TimeStampIdGenerator will fill the remainder of the 64bit Int
with random digits. 'id_timestamp_provider' => TimeStampIdGenerator::class,
Install the package via composer:
composer require blackfrog/laravel-event-sourcing-dynamodb
Publish the config file with:
php artisan vendor:publish --tag="laravel-event-sourcing-dynamodb-config"
Review the config key dynamodb-client and make sure the appropriate ENV variables are set, or you may wish to use
your own ENV variable names if the package defaults clash for you. This array is the configuration array passed to
Aws\DynamoDb\DynamoDbClient so you can modify it to use anything the AWS package supports, including alternative
authentication options. If you already use AWS, for example with DynamoDB as a Cache driver for Laravel, you should
check and align your configuration for this with the one for this package to avoid confusion or duplication.
You can change the default table names using the event-table and snapshot-table config keys.
You can create the relevant DynamoDb tables with php artisan event-sourcing-dynamodb:create-tables. This requires
appropriate AWS permissions to do so and is probably unwise to use in a production scenario. You can see (and modify at
your own risk) the table specifications in event-sourcing-dynamodb.php. For production, we recommend you take these
table specs and move them into your preferred mechanism for managing AWS resources, such as Terraform or CloudFormation.
Update the config for the Spatie Laravel Event Sourcing package in config/event-sourcing.php setting the value
for stored_event_repository to DynamoDbStoredEventRepository::class and snapshot_repository
to DynamoDbSnapshotRepository::class.
Running the test suite requires DynamoDBLocal, see Local Development for setup.
The test suite expects this to be present and running at default ports.
Run:
composer test
For local development you can use: DynamoDB Local. There are some minor differences in behaviour from the real service, and we recommend testing against real DynamoDB in your AWS account before launching your project.
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.