j5ik2o/event-store-adapter-php
最新稳定版本:v1.0.18
Composer 安装命令:
composer require j5ik2o/event-store-adapter-php
包简介
This library is designed to turn DynamoDB into an Event Store for CQRS/Event Sourcing.
README 文档
README
This library is designed to turn DynamoDB into an Event Store for CQRS/Event Sourcing.
The Status is WIP.
Installation
composer require j5ik2o/event-store-adapter-php
Usage
You can easily implement an Event Sourcing-enabled repository using 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); } }
The following is an example of the repository usage.
$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());
Table Specifications
CQRS/Event Sourcing Example
See akinoriakatsuka/cqrs-es-example-php.
License.
MIT License. See LICENSE for details.
Links
统计信息
- 总下载量: 591
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-16