laravel-event-sourcing can be installed via composer:
composer require spatie/laravel-event-sourcing
You need to publish and run the migrations to create the stored_events
table:
php artisan vendor:publish --provider="Spatie\EventSourcing\EventSourcingServiceProvider" --tag="migrations"
php artisan migrate
You must publish the config file with this command:
php artisan vendor:publish --provider="Spatie\EventSourcing\EventSourcingServiceProvider" --tag="config"
This is the default content of the config file that will be published at config/event-sourcing.php
:
use Spatie\EventSourcing\EventSerializers\JsonEventSerializer;use Spatie\EventSourcing\Models\EloquentStoredEvent;use Spatie\EventSourcing\StoredEvents\HandleStoredEventJob;return [
'auto_discover_projectors_and_reactors' => [
app_path(),
],
'projectors' => [
],
'reactors' => [
],
'queue' => env('EVENT_PROJECTOR_QUEUE_NAME', null),
'catch_exceptions' => env('EVENT_PROJECTOR_CATCH_EXCEPTIONS', false),
'stored_event_model' => EloquentStoredEvent::class,
'stored_event_job' => HandleStoredEventJob::class,
'event_class_map' => [],
'event_serializer' => JsonEventSerializer::class,
'cache_path' => storage_path('app/event-sourcing'),
];
The package will scan all classes of your project to automatically discover projectors and reactors. In a production environment you probably should cache auto discovered projectors and reactors.
It's recommended that you set up a queue. Specify the connection name in the queue
key of the event-sourcing
config file. This queue will be used to guarantee that the events will be processed by all projectors in the right order. You should make sure that the queue will process only one job at a time. In a local environment, where events have a very low chance of getting fired concurrently, it's probably ok to just use the sync
driver.
When using Laravel Horizon you can update your horizon.php
config file as such:
'environments' => [
'production' => [
'event-sourcing-supervisor-1' => [
'connection' => 'redis',
'queue' => [env('EVENT_PROJECTOR_QUEUE_NAME')],
'balance' => 'simple',
'processes' => 1,
'tries' => 3,
],
],
'local' => [
'event-sourcing-supervisor-1' => [
'connection' => 'redis',
'queue' => [env('EVENT_PROJECTOR_QUEUE_NAME')],
'balance' => 'simple',
'processes' => 1,
'tries' => 3,
],
],
],