Skip to main content

nexus-persistence-dbal

Doctrine DBAL adapter for Nexus persistence -- SQL-backed stores using parameterized queries. Supports any database supported by DBAL (SQLite, PostgreSQL, MySQL, etc.).

Composer: nexus-actors/persistence-dbal

Namespace: Monadial\Nexus\Persistence\Dbal\

View class diagram

Dependencies: doctrine/dbal ^4.0

Classes

ClassDescription
DbalEventStoreEventStore implementation using DBAL. Constructor: Connection, MessageSerializer (default: PhpNativeSerializer). Throws ConcurrentModificationException on duplicate sequence numbers. Stores writer_id (ULID) per event.
DbalSnapshotStoreSnapshotStore implementation using DBAL. Constructor: Connection, MessageSerializer (default: PhpNativeSerializer). Stores writer_id (ULID) per snapshot.
DbalDurableStateStoreDurableStateStore implementation using DBAL. Constructor: Connection, MessageSerializer (default: PhpNativeSerializer). Uses optimistic locking via WHERE version = ? on updates. Stores writer_id (ULID).
PersistenceSchemaManagerCreates and drops database tables. Constructor: Connection. Methods: createSchema(), dropSchema(). Lives in Schema\ sub-namespace.

Table schema

The PersistenceSchemaManager creates the following tables:

nexus_event_journal

ColumnTypeNotes
persistence_idVARCHAR(255)Primary key (composite)
sequence_nrBIGINTPrimary key (composite)
event_typeVARCHAR(255)
event_dataTEXT
metadataTEXTNullable
writer_idVARCHAR(26)ULID of the writing ActorSystem
timestampDATETIMEImmutable

Index: idx_event_journal_pid on persistence_id.

nexus_snapshot_store

ColumnTypeNotes
persistence_idVARCHAR(255)Primary key (composite)
sequence_nrBIGINTPrimary key (composite)
state_typeVARCHAR(255)
state_dataTEXT
writer_idVARCHAR(26)ULID of the writing ActorSystem
timestampDATETIMEImmutable

nexus_durable_state

ColumnTypeNotes
persistence_idVARCHAR(255)Primary key
versionBIGINTOptimistic lock version
state_typeVARCHAR(255)
state_dataTEXT
writer_idVARCHAR(26)ULID of the writing ActorSystem
timestampDATETIMEImmutable

Usage

use Doctrine\DBAL\DriverManager;
use Monadial\Nexus\Persistence\Dbal\DbalEventStore;
use Monadial\Nexus\Persistence\Dbal\DbalSnapshotStore;
use Monadial\Nexus\Persistence\Dbal\DbalDurableStateStore;
use Monadial\Nexus\Persistence\Dbal\Schema\PersistenceSchemaManager;

$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => 'nexus.db']);

// Create tables (idempotent -- skips existing tables)
(new PersistenceSchemaManager($connection))->createSchema();

$eventStore = new DbalEventStore($connection);
$snapshotStore = new DbalSnapshotStore($connection);
$durableStateStore = new DbalDurableStateStore($connection);

// With a custom serializer
use Monadial\Nexus\Serialization\MessageSerializer;

$eventStore = new DbalEventStore($connection, $customSerializer);
$durableStateStore = new DbalDurableStateStore($connection, $customSerializer);