Installation
Install with Composer
composer require domainflow/event-sourcing-mysql
Connect the adapter
The adapter accepts an existing PDO connection:
use DomainFlow\EventSourcing\Facade\EventSourcingFacade;
use DomainFlow\EventSourcing\Snapshot\GenericSnapshotFactory;
use DomainFlow\EventSourcingMySQL\Snapshot\MySqlSnapshotStorage;
use DomainFlow\EventSourcingMySQL\Storage\MySqlEventStorage;
use PDO;
$pdo = new PDO(
'mysql:host=127.0.0.1;dbname=my_app;charset=utf8mb4',
$user,
$password,
);
$facade = new EventSourcingFacade(
new MySqlEventStorage($pdo),
new MySqlSnapshotStorage($pdo),
new GenericSnapshotFactory(),
);
Snapshots are optional. Event storage alone supports full replay.
Create the schema
The package ships SQL files and a schema manager that reads those files in dependency order:
use DomainFlow\EventSourcingMySQL\Schema\MySqlSchemaManager;
$schema = new MySqlSchemaManager($pdo);
$schema->ensureSchema();
The default files create events, snapshots, snapshot_history,
process_manager_states, outbox, and outbox_dead. ensureSchema() is
idempotent because the files use CREATE TABLE IF NOT EXISTS.
You can pass a custom directory containing the same migration filenames as the
second MySqlSchemaManager argument:
$schema = new MySqlSchemaManager($pdo, migrationsPath: '/path/to/migrations');
The custom directory is read by the adapter; it is not a versioned migration framework.