morebec/orkestra-postgresql-eventstore

Orkestra 的 PostgreSQL 事件存储

v2.5.6 2023-03-31 18:46 UTC

This package is auto-updated.

Last update: 2024-09-30 01:32:33 UTC


README

该组件提供了一个使用 PostgreSQL 实现的事件存储(来自事件存储组件)。

在内部,它使用 DBAL 与 PostgreSQL 进行通信。

安装

composer require morebec/orkestra-orkestra-postgresql-eventstore

使用方法

use Morebec\Orkestra\PostgreSqlEventStore\PostgreSqlEventStore;
use Morebec\Orkestra\PostgreSqlEventStore\PostgreSqlEventStoreConfiguration;

$connection = DriverManager::getConnection([
    'url' => '...'
], new Configuration()); 

$config = new PostgreSqlEventStoreConfiguration();
$store = new PostgreSqlEventStore($connection, $config);

事件存储订阅

事件存储的 pu/sub 机制通过 PostgreSQL 的 LISTEN/NOTIFY 功能实现。遗憾的是,由于 PHP 是同步运行时,唯一实现 Pub/Sub 功能的方法是运行一个 LISTEN 循环。

// This method will start a loop and listen for communications from PostgreSQL's LISTEN/NOTIFY mechanis.
$store->notifySubscribers();

这可以与 事件处理器 一起使用。

PostgreSqlEventStorePositionStorage

组件中还包括了 EventStorePositionStorageInterface 的实现,名为 PostgreSqlEventStorePositionStorage,它也依赖于 DBAL 与 PostgreSQL 进行通信。

use Morebec\Orkestra\PostgreSqlEventStore\PostgreSqlEventStorePositionStorage;
use Morebec\Orkestra\PostgreSqlEventStore\PostgreSqlEventStorePositionStorageConfiguration;

$connection = DriverManager::getConnection([
    'url' => '...'
], new Configuration()); 

$config = new PostgreSqlEventStorePositionStorageConfiguration();
$store = new PostgreSqlEventStorePositionStorage($connection, $config);

PostgreSqlEventProcessor

该组件还提供了一个带有对 PostgreSqlEventStore 支持的预置事件处理器实现。

use Morebec\Orkestra\PostgreSqlEventStore\PostgreSqlEventProcessor;
$processor = new PostgreSqlEventProcessor($publisher, $eventStore, $postgreSqlEventStore, $positionStorage);

// This call will loop and notify all event store subscribers as well as the event processor itself
// for event tracking.
$processor->start();

由于 EventStoreInterface 可以被装饰,处理器不能直接通过 EventStoreInterface 使用 PostgreSqlEventStore。此外,由于它需要使用 PostgreSqlEventStore 实现的特定方法来访问一些 PostgreSQL 特定功能,因此必须额外注入 PostgreSqlEventStore 的实际实例。这就是为什么两个都在构造函数中注入的原因。

测试

要运行测试,请执行以下命令

vendor/bin/phpunit tests/

需要有一个运行在无密码角色 postgres 和名为 postgres 的数据库上的 PostgreSQL 实例。为了轻松设置此环境并运行,项目根目录中提供了一个 docker-compose 配置文件。

要运行它,只需执行以下命令

docker-compose up -d