Laravel Event Sourcing
Simple Laravel implementation of event sourcing mechanism., (*1)
, (*2)
Be advised: this package is work in progress and some breaking changes may appear on the way., (*3)
Package realizes concept of event sourcing with Laravel framework. Package consists of two main elements:
implementation of database-like event store and projection manager., (*4)
Event store is a append-only storage dedicated for storing events.
Projection manager allows to register projections and update them with generated stream of events., (*5)
Installation
To install the package via Composer, simply run the following command:, (*6)
composer require desmart/laravel-event-sourcing
In order to use provided event store implementation, register service provider in your config/app.php
file:, (*7)
'providers' => [
// ...
DeSmart\EventSourcing\Laravel\EventStore\ServiceProvider::class,
]
In order to use projection manager, register service provider in your config/app.php
file:, (*8)
'providers' => [
// ...
DeSmart\EventSourcing\Laravel\Projections\ServiceProvider::class,
]
After registering service provider(s), run artisan command to publish configuration files:, (*9)
php artisan vendor:publish
Configuration
Package adds two configuration files: event-store.php
and read-model.php
., (*10)
event-store.php
Here you can configure:
- which connection will be used to connect with your event store,
- which event's payload serializer will be used., (*11)
return [
/*
|-------------------------------------------------------------------------------------
| Database Connection For Event Store
|-------------------------------------------------------------------------------------
|
| Specify which database connection should be used for storing events in events store.
| All database connections can be found in config/database.php configuration file.
|
*/
'connection' => null,
/*
|-------------------------------------------------------------------------------------
| Event Store Payload Serializer
|-------------------------------------------------------------------------------------
|
| Serializer used for serializing/deserializing event and it's payload.
|
| Payload is usually of array type. It is sufficient to store payload in
| JSON format.
|
| Supported serializers:
| - 'JsonSerializer' -> use for event store that does not have automatic json serialization/deserialization, like mysql databases
| - 'ArraySerializer -> use for event store that has automatic json serialization/deserialization, like mongodb databases
|
*/
'serializer' => \DeSmart\EventSourcing\Laravel\EventStore\Serializers\JsonSerializer::class
];
read-model.php
Here you can register all projections that should be notified with stream of events., (*12)
return [
/*
|--------------------------------------------------------------------------
| Read Model Projections
|--------------------------------------------------------------------------
|
| Array of projection classes.
|
| These projections will be notified about saved stream of events and they
| can react with read model updates.
|
*/
'projections' => []
];
Event store implementation
This package's event store implementation was successfully used/tested with MySql database, MongoDB database as well as
with mix of the two: one database served as an event store, where second was storing projections., (*13)
Event store required structure (however different column types allowed) can be found here., (*14)
License
Package is released under the MIT License (MIT). Please, check LICENSE for more details., (*15)
Example usage
For example use of event store as well as project manager, check example/
directory., (*16)
Additionally, package provides artisan command for rebuilding all projections:, (*17)
php artisan projections:rebuild