The official PHP client SDK for EventSourcingDB – a purpose-built database for event sourcing.
EventSourcingDB enables you to build and operate event-driven applications with native support for writing, reading, and observing events. This client SDK provides convenient access to its capabilities in PHP.
For more information on EventSourcingDB, see its official documentation.
This client SDK includes support for Testcontainers to spin up EventSourcingDB instances in integration tests. For details, see Using Testcontainers.
Install the client SDK:
composer require thenativeweb/eventsourcingdb
Import the Client
class and create an instance by providing the URL of your EventSourcingDB instance and the API token to use:
require __DIR__ . '/vendor/autoload.php';
use Thenativeweb\Eventsourcingdb\Client;
$client = new Client('http://localhost:3000', 'secret');
Then call the ping
function to check whether the instance is reachable. If it is not, the function will throw an exception:
$client->ping();
Note that ping
does not require authentication, so the call may succeed even if the API token is invalid.
If you want to verify the API token, call verifyApiToken
. If the token is invalid, the function will throw an exception:
$client->verifyApiToken();
Call the writeEvents
function and hand over an array with one or more events. You do not have to provide all event fields – some are automatically added by the server.
Specify source
, subject
, type
, and data
according to the CloudEvents format.
The function returns the written events, including the fields added by the server:
use Thenativeweb\Eventsourcingdb\EventCandidate;
$writtenEvents = $client->writeEvents([
new EventCandidate(
'https://library.eventsourcingdb.io',
'/books/42',
'io.eventsourcingdb.library.book-acquired',
[
'title' => '2001 – A Space Odyssey',
'author' => 'Arthur C. Clarke',
'isbn' => '978-0756906788',
],
),
]);
If you only want to write events in case a subject (such as /books/42
) does not yet have any events, import the IsSubjectPristine
class, use it to create a precondition, and pass it in an array as the second argument:
use Thenativeweb\Eventsourcingdb\IsSubjectPristine;
$writtenEvents = $client->writeEvents([
// ...
], [
new IsSubjectPristine('/books/42'),
]);
If you only want to write events in case the last event of a subject (such as /books/42
) has a specific ID (e.g., 0
), import the IsSubjectOnEventId
class, use it to create a precondition, and pass it in an array as the second argument:
use Thenativeweb\Eventsourcingdb\IsSubjectOnEventId;
$writtenEvents = $client->writeEvents([
// ...
], [
new IsSubjectOnEventId('/books/42', '0'),
]);
Note that according to the CloudEvents standard, event IDs must be of type string.
To read all events of a subject, call the readEvents
function with the subject and an options object. Set the recursive
option to false
. This ensures that only events of the given subject are returned, not events of nested subjects.
The function returns an iterator, which you can use in a foreach
loop:
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: false,
),
);
foreach ($events as $event) {
// ...
}
If you want to read not only all the events of a subject, but also the events of all nested subjects, set the recursive
option to true
:
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: true,
),
);
foreach ($events as $event) {
// ...
}
This also allows you to read all events ever written. To do so, provide /
as the subject and set recursive
to true
, since all subjects are nested under the root subject.
By default, events are read in chronological order. To read in anti-chronological order, provide the order
option and set it to Order::ANTICHRONOLOGICAL
:
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: false,
order: Order::ANTICHRONOLOGICAL,
),
);
foreach ($events as $event) {
// ...
}
Note that you can also use Order::CHRONOLOGICAL
to explicitly enforce the default order.
Sometimes you do not want to read all events, but only a range of events. For that, you can specify the lowerBound
and upperBound
options – either one of them or even both at the same time.
Specify the ID and whether to include or exclude it, for both the lower and upper bound:
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: false,
lowerBound: new Bound('100', BoundType::INCLUSIVE),
upperBound: new Bound('200', BoundType::EXCLUSIVE),
),
);
foreach ($events as $event) {
// ...
}
To read starting from the latest event of a given type, provide the fromLatestEvent
option and specify the subject, the type, and how to proceed if no such event exists.
Possible options are ReadIfEventIsMissing::READ_NOTHING
, which skips reading entirely, or ReadIfEventIsMissing::READ_EVERYTHING
, which effectively behaves as if fromLatestEvent
was not specified:
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: false,
fromLatestEvent: new ReadFromLatestEvent(
subject: '/books/42',
type: 'io.eventsourcingdb.library.book-borrowed',
ifEventIsMissing: ReadIfEventIsMissing::READ_EVERYTHING,
),
),
);
foreach ($events as $event) {
// ...
}
Note that fromLatestEvent
and lowerBound
can not be provided at the same time.
To run an EventQL query, call the runEventQlQuery
function and provide the query as a string. The function returns an iterator, which you can use in a foreach
loop:
$rows = $client->runEventQlQuery(
'FROM e IN events PROJECT INTO e',
);
foreach ($rows as $row) {
// ...
}
Note that each row returned by the iterator is an associative array and matches the projection specified in your query.
Import the Container
class, call the start
function to run a test container, get a client, run your test code, and finally call the stop
function to stop the test container:
require __DIR__ . '/vendor/autoload.php';
use Thenativeweb\Eventsourcingdb\Container;
$container = new Container();
$container->start();
$client = $container->getClient();
// ...
$container->stop();
To check if the test container is running, call the isRunning
function:
$isRunning = $container->isRunning();
By default, Container
uses the latest
tag of the official EventSourcingDB Docker image. To change that, call the withImageTag
function:
$container = new Container()
->withImageTag('1.0.0');
Similarly, you can configure the port to use and the API token. Call the withPort
or the withApiToken
function respectively:
$container = new Container()
->withPort(4000)
->withApiToken('secret');
In case you need to set up the client yourself, use the following functions to get details on the container:
getHost()
returns the host namegetMappedPort()
returns the portgetBaseUrl()
returns the full URL of the containergetApiToken()
returns the API token