$ cat create-a-domain-using-spatie-event-sourcing.md
Create a domain using Spatie event sourcing
Event sourcing is a powerful pattern for tracking changes to application state, offering a robust way to manage domain-driven design in Laravel. However, setting up an event-sourcing domain can be time-consuming and repetitive, especially when following best practices.
albertoarena.it26 Dec 2024 · summary
Read on albertoarena.it ↗Event sourcing is a powerful pattern for tracking changes to application state, offering a robust way to manage domain-driven design in Laravel. To streamline this process, I’ve created a Laravel package that automates the creation of event-sourcing domains using Spatie’s event sourcing library.
Running this will generate the following structure: app/ └── Domain/ └── Sales/ ├── Actions/ │ ├── CreateOrder.php │ ├── DeleteOrder.php │ └── UpdateOrder.php ├── Aggregates/ │ └── OrderAggregate.php ├── Events/ │ ├── OrderCreated.php │ ├── OrderDeleted.php │ └── OrderUpdated.php ├── Projections/ │ └── Order.php └──Projectors/ └── OrderProjector.php #example-workflowExample workflow
Running this will generate the following structure: app/ └── Domain/ └── Sales/ ├── Aggregates/ │ └── OrderAggregate.php ├── DataTrasnferObjects/ │ └── OrderData.php ├── Events/ │ ├── OrderCreated.php │ ├── OrderUpdated.php │ └── OrderDeleted.php ├── Projections/ │ └── Order.php └── Projectors/ └── OrderProjector.php #step-2-use-itStep Use it use App\Domain\Sales\Actions\CreateOrder; use App\Domain\Sales\DataTransferObjects\OrderData; use App\Domain\Sales\Projections\Order; (new CreateOrder)(new OrderData( customer_email: 'john@acme.org', customer_name: 'John Doe', total_amount: 110.2, items: [[ 'id' => 12345, 'name' => 'Whiskey bottle', 'quantity' => 2, 'price' => 55.1, ]], )); $order = Order::query()->where('email', 'john@acme.org')->first(); #advanced-examplesAdvanced Examples #generate-phpunit-testsGenerate PHPUnit tests https://github.com/albertoarena/laravel-event-sourcing-generator/blob/main/docs/unit-tests.md
Running this will generate the following structure: app/ └── Domain/ └── Sales/ ├── Aggregates/ │ └── OrderAggregate.php ├── DataTrasnferObjects/ │ └── OrderData.php ├── Events/ │ ├── OrderCreated.php │ ├── OrderUpdated.php │ └── OrderDeleted.php ├── Notifications/ │ ├── Concerns/ │ │ └── HasDataAsArray │ ├── OrderCreated.php │ ├── OrderUpdated.php │ └── OrderDeleted.php ├── Projections/ │ └── Order.php └── Projectors/ └── OrderProjector.php #whats-nextWhat’s next
source
This is a summary of an article first published by albertoarena.it. The full piece is on their site.