PHPackages                             genesisdb/client-sdk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. genesisdb/client-sdk

ActiveLibrary[Database &amp; ORM](/categories/database)

genesisdb/client-sdk
====================

PHP SDK for GenesisDB

1.0.6(2mo ago)031PHPPHP &gt;=8.1

Since Jun 11Pushed 2mo agoCompare

[ Source](https://github.com/genesisdb-io/genesisdb-io-client-php)[ Packagist](https://packagist.org/packages/genesisdb/client-sdk)[ RSS](/packages/genesisdb-client-sdk/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (12)Used By (1)

GenesisDB PHP SDK
=================

[](#genesisdb-php-sdk)

This is the official PHP SDK for GenesisDB, an awesome and production ready event store database system for building event-driven apps.

GenesisDB Advantages
--------------------

[](#genesisdb-advantages)

- Incredibly fast when reading, fast when writing 🚀
- Easy backup creation and recovery
- [CloudEvents](https://cloudevents.io/) compatible
- GDPR-ready
- Easily accessible via the HTTP interface
- Auditable. Guarantee database consistency
- Logging and metrics for Prometheus
- SQL like query language called GenesisDB Query Language (GDBQL)
- ...

Installation
------------

[](#installation)

```
composer require genesisdb/client-sdk

```

Configuration
-------------

[](#configuration)

### Basic Setup

[](#basic-setup)

```
use GenesisDB\GenesisDB\Client;

$client = new Client(
    apiUrl: 'http://localhost:8080',
    apiVersion: 'v1',
    authToken: ''
);
```

Streaming Events
----------------

[](#streaming-events)

### Basic Event Streaming

[](#basic-event-streaming)

```
// Stream all events for a subject
$events = $client->streamEvents('/customer');
```

### Stream Events from Lower Bound

[](#stream-events-from-lower-bound)

```
use GenesisDB\GenesisDB\StreamOptions;

$events = $client->streamEvents('/', new StreamOptions(
    lowerBound: '2d6d4141-6107-4fb2-905f-445730f4f2a9',
    includeLowerBoundEvent: true
));
```

### Stream Events with Upper Bound

[](#stream-events-with-upper-bound)

```
$events = $client->streamEvents('/', new StreamOptions(
    upperBound: '9f3e4141-7208-4fb2-905f-445730f4f3b1',
    includeUpperBoundEvent: false
));
```

### Stream Events with Both Lower and Upper Bounds

[](#stream-events-with-both-lower-and-upper-bounds)

```
$events = $client->streamEvents('/', new StreamOptions(
    lowerBound: '2d6d4141-6107-4fb2-905f-445730f4f2a9',
    includeLowerBoundEvent: true,
    upperBound: '9f3e4141-7208-4fb2-905f-445730f4f3b1',
    includeUpperBoundEvent: false
));
```

### Stream Latest Events by Event Type

[](#stream-latest-events-by-event-type)

```
$events = $client->streamEvents('/', new StreamOptions(
    latestByEventType: 'io.genesisdb.app.customer-updated'
));
```

This feature allows you to stream only the latest event of a specific type for each subject. Useful for getting the current state of entities.

Committing Events
-----------------

[](#committing-events)

### Basic Event Committing

[](#basic-event-committing)

```
use GenesisDB\GenesisDB\CommitEvent;

$client->commitEvents([
    new CommitEvent(
        source: 'io.genesisdb.app',
        subject: '/customer',
        type: 'io.genesisdb.app.customer-added',
        data: [
            'firstName' => 'Bruce',
            'lastName' => 'Wayne',
            'emailAddress' => 'bruce.wayne@enterprise.wayne'
        ]
    ),
    new CommitEvent(
        source: 'io.genesisdb.app',
        subject: '/customer',
        type: 'io.genesisdb.app.customer-added',
        data: [
            'firstName' => 'Alfred',
            'lastName' => 'Pennyworth',
            'emailAddress' => 'alfred.pennyworth@enterprise.wayne'
        ]
    ),
    new CommitEvent(
        source: 'io.genesisdb.store',
        subject: '/article',
        type: 'io.genesisdb.store.article-added',
        data: [
            'name' => 'Tumbler',
            'color' => 'black',
            'price' => 2990000.00
        ]
    ),
    new CommitEvent(
        source: 'io.genesisdb.app',
        subject: '/customer/fed2902d-0135-460d-8605-263a06308448',
        type: 'io.genesisdb.app.customer-personaldata-changed',
        data: [
            'firstName' => 'Angus',
            'lastName' => 'MacGyver',
            'emailAddress' => 'angus.macgyer@phoenix.foundation'
        ]
    )
]);
```

Preconditions
-------------

[](#preconditions)

Preconditions allow you to enforce certain checks on the server before committing events. GenesisDB supports multiple precondition types:

### isSubjectNew

[](#issubjectnew)

Ensures that a subject is new (has no existing events):

```
use GenesisDB\GenesisDB\Precondition;

$client->commitEvents([
    new CommitEvent(
        source: 'io.genesisdb.app',
        subject: '/user/456',
        type: 'io.genesisdb.app.user-created',
        data: [
            'firstName' => 'John',
            'lastName' => 'Doe',
            'email' => 'john.doe@example.com'
        ]
    )
], [
    Precondition::isSubjectNew('/user/456')
]);
```

### isSubjectExisting

[](#issubjectexisting)

Ensures that events exist for the specified subject:

```
$client->commitEvents([
    new CommitEvent(
        source: 'io.genesisdb.app',
        subject: '/user/456',
        type: 'io.genesisdb.app.user-updated',
        data: [
            'firstName' => 'John',
            'lastName' => 'Doe',
            'email' => 'john.doe@example.com'
        ]
    )
], [
    Precondition::isSubjectExisting('/user/456')
]);
```

### isQueryResultTrue

[](#isqueryresulttrue)

Evaluates a query and ensures the result is truthy. Supports the full GDBQL feature set including complex WHERE clauses, aggregations, and calculated fields.

**Basic uniqueness check:**

```
$client->commitEvents([
    new CommitEvent(
        source: 'io.genesisdb.app',
        subject: '/user/456',
        type: 'io.genesisdb.app.user-created',
        data: [
            'firstName' => 'John',
            'lastName' => 'Doe',
            'email' => 'john.doe@example.com'
        ]
    )
], [
    Precondition::isQueryResultTrue(
        "STREAM e FROM events WHERE e.data.email == 'john.doe@example.com' MAP COUNT() == 0"
    )
]);
```

**Business rule enforcement (transaction limits):**

```
$client->commitEvents([
    new CommitEvent(
        source: 'io.genesisdb.banking',
        subject: '/user/123/transactions',
        type: 'io.genesisdb.banking.transaction-processed',
        data: [
            'amount' => 500.00,
            'currency' => 'EUR'
        ]
    )
], [
    Precondition::isQueryResultTrue(
        "STREAM e FROM events WHERE e.subject UNDER '/user/123' AND e.type == 'transaction-processed' AND e.time >= '2024-01-01T00:00:00Z' MAP SUM(e.data.amount) + 500
