PHP code example of wwwision / umadb-php

1. Go to this page and download the library: Download wwwision/umadb-php 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/ */

    

wwwision / umadb-php example snippets




use UmaDB\Client;
use UmaDB\Event;

// Connect to UmaDB server
$client = new Client('http://localhost:50051');

// Create an event
$event = new Event(
    event_type: 'UserCreated',
    data: json_encode(['userId' => '12345', 'name' => 'Alice']),
    tags: ['user:12345']
);

// Append event
$position = $client->append([$event]);
echo "Event appended at position: {$position}\n";

// Read all events
$events = $client->read();
foreach ($events as $seqEvent) {
    echo "[{$seqEvent->position}] {$seqEvent->event->event_type}\n";
}

// Get current head
$head = $client->head();
echo "Current head: {$head}\n";

new Client(
    string $url,
    ?string $ca_path = null,
    ?int $batch_size = null
    ?string $api_key = null
)

// Basic connection
$client = new Client('http://localhost:50051');

// TLS with self-signed certificate
$client = new Client('https://localhost:50051', ca_path: '/path/to/ca.pem');

// Custom batch size (must provide all parameters explicitly)
$client = new Client('http://localhost:50051', ca_path: null, batch_size: 100);

// API key (must provide all parameters explicitly)
$client = new Client('http://localhost:50051', ca_path: null, batch_size: null, api_key: 'secret-key');

public function read(
    ?Query $query = null,
    ?int $start = null,
    ?bool $backwards = false,
    ?int $limit = null,
    ?bool $subscribe = false
): array

// Read all events
$events = $client->read();

// Read with query
$query = new Query([
    new QueryItem(types: ['OrderCreated'], tags: ['order:1234'])
]);
$events = $client->read(query: $query);

// Read backwards with limit
$events = $client->read(start: 1000, backwards: true, limit: 10);

// Subscribe to new events
$events = $client->read(subscribe: true);

public function append(
    array $events,
    ?AppendCondition $condition = null
): int

// Simple append
$event = new Event('UserCreated', $data, ['user:1234']);
$position = $client->append([$event]);

// Append with condition
$condition = new AppendCondition($query, after: $head);
$position = $client->append([$event], $condition);

// Append multiple events
$position = $client->append([
    new Event('OrderCreated', $data1, ['order:1234']),
    new Event('OrderPaid', $data2, ['order:1234', 'payment:4321']),
]);

public function head(): ?int

$head = $client->head();
if ($head === null) {
    echo "Store is empty\n";
} else {
    echo "Last event at position: {$head}\n";
}

new Event(
    string $event_type,
    string $data,
    ?array $tags = null,
    ?string $uuid = null
)

$event = new Event(
    event_type: 'UserRegistered',
    data: json_encode(['userId' => '123', 'email' => '[email protected]']),
    tags: ['user:123', 'email:' . sha1('[email protected]')],
    uuid: '550e8400-e29b-41d4-a716-446655440000'
);

new Query(?array $items = null)

$query = new Query([
    new QueryItem(types: ['UserCreated'], tags: ['user:1234']),
    new QueryItem(types: ['UserUpdated'], tags: ['user:1235']),
]);

new QueryItem(
    ?array $types = null,
    ?array $tags = null
)

// Match all "OrderCreated" events
$item = new QueryItem(types: ['OrderCreated']);

// Match events with specific tags
$item = new QueryItem(tags: ['order', 'order:12345']);

// Match specific types with specific tags
$item = new QueryItem(
    types: ['OrderPaid', 'OrderShipped'],
    tags: ['order:12345']
);

// Match all events (no filter)
$item = new QueryItem();

new AppendCondition(
    Query $fail_if_events_match,
    ?int $after = null
)

// Prevent duplicate events
$condition = new AppendCondition(
    fail_if_events_match: new Query([
        new QueryItem(types: ['UserRegistered'], tags: ['user:1234'])
    ])
);

// Optimistic concurrency (position-based)
$head = $client->head();
$condition = new AppendCondition(
    fail_if_events_match: new Query([]),
    after: $head
);

// Combined: business rule + position check
$condition = new AppendCondition(
    fail_if_events_match: $boundaryQuery,
    after: $lastKnownPosition
);

use UmaDB\Exception\IntegrityException;

try {
    $client->append([$event], $condition);
} catch (IntegrityException $e) {
    echo "Append condition failed: {$e->getMessage()}\n";
}

$uuid = '550e8400-e29b-41d4-a716-446655440000';
$event = new Event('OrderCreated', $data, ['order:1234'], $uuid);

// First append
$position1 = $client->append([$event]);  // Returns position 100

// Retry (e.g., after network failure) - same UUID
$position2 = $client->append([$event]);  // Returns position 100 (same!)

assert($position1 === $position2);  // true

$email = '[email protected]';
$emailHash = sha1($email);

// Define consistency boundary
$boundaryQuery = new Query([
    new QueryItem(types: ['UserRegistered'], tags: ["email:{$emailHash}"])
]);

// Read current state
$head = $client->head();

// Create append condition
$condition = new AppendCondition($boundaryQuery, $head);

// Try to register
$event = new Event(
    'UserRegistered',
    json_encode(['email' => $email, 'name' => 'Alice'}),
    ["email:$emailHash"]
);

try {
    $position = $client->append([$event], $condition);
    echo "User registered successfully\n";
} catch (IntegrityException $e) {
    echo "Email already registered\n";
}

$workflowId = 'workflow-123';

// Step 1: Start workflow
$event1 = new Event(
    'WorkflowStarted',
    json_encode(['workflowId' => $workflowId]),
    ["workflow:{$workflowId}", 'step:1']
);
$client->append([$event1]);

// Step 2: Prevent duplicate execution
$step2Boundary = new Query([
    new QueryItem(types: ['WorkflowStep2Completed'], tags: ["workflow:{$workflowId}"])
]);

$head = $client->head();
$condition = new AppendCondition($step2Boundary, $head);

$event2 = new Event(
    'WorkflowStep2Completed',
    json_encode(['workflowId' => $workflowId, 'result' => 'success']),
    ["workflow:{$workflowId}", 'step:2']
);

$client->append([$event2], $condition);  // Only succeeds once

// Query by event type
$query = new Query([
    new QueryItem(types: ['OrderCreated', 'OrderUpdated'])
]);
$orderEvents = $client->read(query: $query);

// Query by tags
$query = new Query([
    new QueryItem(tags: ['order:12345'])
]);
$specificOrderEvents = $client->read(query: $query);

// Complex query (OR logic between items)
$query = new Query([
    // Match user events
    new QueryItem(types: ['UserCreated'], tags: ['user:1234']),
    // OR payment events
    new QueryItem(types: ['PaymentProcessed'], tags: ['payment:4321']),
]);
$events = $client->read(query: $query);
bash
wget https://github.com/bwaidelich/umadb-php/releases/latest/download/umadb-macos-x86_64.dylib
sudo cp umadb-macos-x86_64.dylib $(php-config --extension-dir)/umadb.so
bash
wget https://github.com/bwaidelich/umadb-php/releases/latest/download/umadb-macos-arm64.dylib
sudo cp umadb-macos-arm64.dylib $(php-config --extension-dir)/umadb.so
bash
php -d extension=umadb.so your-script.php
bash
php examples/basic.php
php examples/query.php
php examples/consistency.php