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";
// 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
);
$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);