Store events in the multiple tables, split by AggregateRoot. #408
otar
started this conversation in
Show and tell
Replies: 1 comment
-
I found another workaround which seems to be a better approach. Hope someone else will find it useful. Migration: return new class extends Migration {
public function up(): void
{
Schema::create(
table: 'stored_events_of_transactions',
callback: function (Blueprint $table) {
$table->id();
$table->uuid('aggregate_uuid')->nullable()->index();
$table->unsignedBigInteger('aggregate_version')->nullable();
$table->unsignedTinyInteger('event_version')->default(1);
$table->string('event_class')->index();
$table->jsonb('event_properties');
$table->jsonb('meta_data');
$table->timestamp('created_at');
$table->unique(['aggregate_uuid', 'aggregate_version']);
}
);
}
}; Model: <?php
namespace App\Models;
use Spatie\EventSourcing\StoredEvents\Models\EloquentStoredEvent;
class StoredEventsOfTransactions extends EloquentStoredEvent
{
protected $table = 'stored_events_of_transactions';
} Repository: <?php
namespace App\Repositories;
use Spatie\EventSourcing\StoredEvents\Repositories\EloquentStoredEventRepository;
use App\Models\StoredEventsOfTransactions;
class StoredEventsOfTransactionsRepository extends EloquentStoredEventRepository
{
public function __construct(
protected string $storedEventModel = StoredEventsOfTransactions::class,
) {}
} Aggregate Root: <?php
use App\Repositories\StoredEventsOfTransactionsRepository;
use Spatie\EventSourcing\AggregateRoots\AggregateRoot;
use Spatie\EventSourcing\StoredEvents\Repositories\StoredEventRepository;
class Transactions extends AggregateRoot
{
protected function getStoredEventRepository(): StoredEventRepository
{
return app(StoredEventsOfTransactionsRepository::class);
}
... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
Does anybody has an experience how to store events into multiple tables for the scalability purposes?
I have money transaction events (30m+ records) and the application specific events (1m records. I don’t want to mix them together in one table. In the ideal scenario I would love to store events from one AggregateRoot in a separate table and the rest events in a default stored_events table.
The only solution that comes to mind is to override default Eloquent model and dynamically set the table depending on event or aggregate. I wonder maybe there’s a better way to do this?
@freekmurze @brendt maybe you guys have some suggestions?
Beta Was this translation helpful? Give feedback.
All reactions