j5ik2o/event-store-adapter-php

该库旨在将DynamoDB转换为CQRS/事件源的事件存储。

v1.0.13 2024-06-21 00:21 UTC

README

CI Latest Version on Packagist Renovate License

该库旨在将DynamoDB转换为CQRS/事件源的事件存储。

状态为WIP。

安装

composer require j5ik2o/event-store-adapter-php

用法

您可以使用EventStore轻松实现支持事件源存储的仓库。

final class UserAccountRepository {
  private readonly EventStore $eventStore;

  public function __construct(EventStore $eventStore) {
    $this->eventStore = $eventStore;
  }

  public function storeEvent(UserAccountEvent $event, int $version): void {
    $this->eventStore->persistEvent($event, $version);
  }

  public function storeEventAndSnapshot(UserAccountEvent $event, UserAccount $userAccount): void {
    $this->eventStore->persistEventAndSnapshot($event, $userAccount);
  }

  public function findById(UserAccountId $id): ?UserAccount {
    $latestSnapshot = $this->eventStore->getLatestSnapshotById($id);
    if ($latestSnapshot === null) {
      return null;
    }
    $events = $this->eventStore->getEventsByIdSinceSequenceNumber($id, $latestSnapshot->getSequenceNumber());
    return UserAccount::replay($events, $latestSnapshot);
  }
}

以下是一个仓库使用示例。

$eventStore = EventStoreFactory::create(
  $client,
  $journalTableName,
  $snapshotTableName,
  $journalAidIndexName,
  $snapshotAidIndexName,
  $shardCount,
  $eventConverter,
  $snapshotConverter,
);

$userAccountRepository = new UserAccountRepository($eventStore);

$userAccountId = new UserAccountId();

// Generates a new aggregate
[$userAccount1, $created] = UserAccount::create($userAccountId, "test-1");
// Store the snapshot and event at first
$userAccountRepository->storeEventAndSnapshot($created, $userAccount1);

// Replay if necessary
$userAccount2 = $userAccountRepository->findById($userAccountId);
// Executes business logic
[$userAccount3, $renamed] = $userAccount2->rename("test-2");
// Store the event only
$userAccountRepository->storeEvent($renamed, $userAccount3->getVersion());

表规格

docs/DATABASE_SCHEMA.md

CQRS/事件源示例

待办事项

许可证。

MIT许可证。有关详细信息,请参阅LICENSE

链接