1. Go to this page and download the library: Download tamarackdb/tamarackdb-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/ */
tamarackdb / tamarackdb-php example snippets
use TamarackDB\Client;
$client = Client::http('http://127.0.0.1:8085');
$client = Client::unixSocket('/run/tamarackdb/tamarackdb.sock');
// With authTokenFile set on the server, and your own limits:
$client = Client::http('http://127.0.0.1:8085', token: 'secret', timeout: 120.0);
use TamarackDB\Event\NewEvent;
use TamarackDB\Projection\TxProjectionWrites;
use TamarackDB\Query\Identifier;
use TamarackDB\Query\Query;
$tx = $client->beginTransaction();
try {
// One decision: one read, then one write.
foreach ($tx->readEvents(new Query(Identifier::is('userId', $userId))) as $event) {
// Build your decision model from $event.
}
$result = $tx->appendEvents([
new NewEvent('user-renamed', ['userId' => $userId], ['tenantId' => 'acme'], json_encode(['name' => $name])),
]);
// Give $result->time to the events before your event handlers react to them.
// Projections, once the events are written. A replace needs a read.
$tx->getProjection('user-profile', $userId);
$tx->writeProjections(
new TxProjectionWrites()->replace('user-profile', $userId, json_encode(['name' => $name])),
);
$tx->commit();
} catch (\Throwable $e) {
$tx->rollback();
throw $e;
}
foreach ($tx->readEvents($query) as $event) {
$event->time; // DateTimeImmutable, UTC
$event->type; // string
$event->identifiers; // ['userId' => '123', 'courseId' => ['a', 'b']]
$event->metadata; // ['tenantId' => 'acme']
$event->payload; // string, exactly as written
if ($event instanceof Event) {
$event->sequence; // int; a PendingEvent gets its own at commit
}
}
use TamarackDB\Query\EventType;
$tx = $client->beginTransaction();
$last = $tx->readEvents(new Query(EventType::in('invoice-created')), backwards: true, limit: 1);
$number = $last === [] ? 1 : (int) $last[0]->identifiers['invoice'] + 1;
$tx->appendEvents([new NewEvent('invoice-created', ['invoice' => (string) $number])]);
$tx->commit(); // ConcurrencyException when another invoice was created since the read
use TamarackDB\Query\AllEvents;
$events = $client->readEvents(new AllEvents(), afterSequence: $last, pageSize: 500);
foreach ($events as $event) {
// Every event is an Event, with its sequence.
$last = $event->sequence;
}
use TamarackDB\Query\EventType;
use TamarackDB\Query\Identifier;
use TamarackDB\Query\Metadata;
use TamarackDB\Query\Query;
new Query(
EventType::in('user-created', 'user-updated'),
Identifier::is('userId', '123'),
)->or(
EventType::in('some-other-event'),
Metadata::is('tenantId', 'acme'),
);
use TamarackDB\Projection\TxProjectionWrites;
$profile = $tx->getProjection('user-profile', '123'); // null when missing
$profile?->payload;
$tx->getProjection('user-list-entry', '456');
$tx->writeProjections(
new TxProjectionWrites()
->create('user-list-entry', '789', '{"name":"Grace"}')
->replace('user-profile', '123', '{"name":"Ada Lovelace"}')
->delete('user-list-entry', '456'),
);
use TamarackDB\Projection\ProjectionWrites;
$profile = $client->getProjection('user-profile', '123'); // with $profile->version
$result = $client->writeProjections(
new ProjectionWrites()
->create('user-list-entry', '789', '{"name":"Grace"}')
->replace('user-profile', '123', $profile->version, '{"name":"Ada Lovelace"}')
->delete('user-list-entry', '456', $entryVersion),
);
$result->createVersions; // new versions, in order
$result->replaceVersions;
$client->deleteProjectionsByType('user-profile'); // or deleteAllProjections()
foreach ($client->readEvents(new AllEvents()) as $event) {
// Run your projectors, and every so often:
// $client->writeProjections($writes);
}
$point = $client->pause(); // returns once the pause is in place
$point->lastSequence; // int
$client->resume();
$client->health(); // Health { status, paused, version }
$client->stats(); // array: the counters of GET /stats
$client->optimize(); // refreshes the statistics SQLite plans queries with
$client->deleteAllEvents(); // Sequence Positions go on: the next event doesn't get 1
$client->deleteAllProjections();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.