PHP code example of genesisdb / neos-genesisdb

1. Go to this page and download the library: Download genesisdb/neos-genesisdb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

genesisdb / neos-genesisdb example snippets


use GenesisDB\Neos\GenesisDB\Service\EventStoreService;
use Neos\Flow\Annotations as Flow;

#[Flow\Inject]
protected EventStoreService $genesisDb;

use GenesisDB\NewEvent;

// The batch is committed atomically. The server returns the stored events with ID, time and hashes.
$stored = $this->genesisDb->commit([
    new NewEvent('io.example.shop', '/customer/42', 'io.example.shop.customer-registered', [
        'firstName' => 'Bruce',
        'lastName' => 'Wayne',
    ]),
    new NewEvent('io.example.shop', '/order/1', 'io.example.shop.order-placed', ['total' => 99.5]),
]);

$stored[0]->id;

use GenesisDB\FetchOptions;

// Every event of the subject and its descendants, in commit order
$events = $this->genesisDb->fetch('/customer');

// Bounded by event IDs
$events = $this->genesisDb->fetch('/customer', new FetchOptions(
    lowerBound: '2d6d4141-6107-4fb2-905f-445730f4f2a9',
    

// The history first, then new events as they are committed. Break out of the loop to stop.
foreach ($this->genesisDb->subscribe('/customer', interval: 0.5) as $event) {
    echo $event->type, ' ', json_encode($event->data), PHP_EOL;
}

$rows = $this->genesisDb->query(
    "STREAM e FROM events WHERE e.type == 'io.example.shop.customer-registered' ORDER BY e.time DESC LIMIT 20 MAP { subject: e.subject, firstName: e.data.firstName }"
);

[$count] = $this->genesisDb->query("STREAM e FROM events WHERE e.subject UNDER '/order' MAP COUNT()");

use GenesisDB\Precondition;
use GenesisDB\PreconditionFailedException;

try {
    $this->genesisDb->commit(
        [new NewEvent('io.example.shop', '/user/456', 'io.example.shop.user-created', ['email' => '[email protected]'])],
        [
            Precondition::isSubjectNew('/user/456'),
            Precondition::isQueryResultTrue("STREAM e FROM events WHERE e.data.email == '[email protected]' MAP COUNT() == 0"),
        ],
    );
} catch (PreconditionFailedException $e) {
    $e->preconditionType; // for example "isSubjectNew"
    $e->reason;           // the server's explanation
}

// Store the payload as reference data, so it can be erased later without breaking the hash chain
$this->genesisDb->commit([
    (new NewEvent('io.example.shop', '/user/456', 'io.example.shop.user-created', [
        'email' => '[email protected]',
    ]))->asReference(),
]);

// Replace every reference payload below the subject with {}. Administrator token.
$this->genesisDb->erase('/user/456');

$this->genesisDb->registerSchema('io.example.shop.user-created', [
    'type' => 'object',
    's();

$this->genesisDb->subjects();   // top level subjects
$this->genesisDb->types();      // event types
$this->genesisDb->ping();       // true when the server answers
$this->genesisDb->status();     // version, event counts, storage format, readiness
$this->genesisDb->audit();      // verifies the hash chain, administrator token