laravel-event-sourcing can be installed via composer:
composer require spatie/laravel-event-sourcing:^1.0
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
:
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' => \Spatie\EventSourcing\Models\StoredEvent::class,
'stored_event_job' => \Spatie\EventSourcing\HandleStoredEventJob::class,
'event_class_map' => [],
'event_serializer' => \Spatie\EventSourcing\EventSerializers\JsonEventSerializer::class,
'replay_chunk_size' => 1000,
'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 should 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,
],
],
],