Download the PHP package tamarackdb/tamarackdb-php without Composer
On this page you can find all versions of the php package tamarackdb/tamarackdb-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tamarackdb/tamarackdb-php
More information about tamarackdb/tamarackdb-php
Files in tamarackdb/tamarackdb-php
Package tamarackdb-php
Short Description PHP client for TamarackDB, an event store compliant with the DCB specification.
License MIT
Homepage https://tamarackdb.github.io/
Informations about the package tamarackdb-php
tamarackdb-php
PHP client for TamarackDB, an event store compliant with the DCB specification.
It covers the whole HTTP API: transactions, reading and writing events, projections, projection rebuilds, and pauses. It is a low-level client, meant to be used by an event sourcing framework or directly by an application. It is tested against TamarackDB v0.46.0.
Requirements
- PHP 8.5 or later, with the
curlandjsonextensions. - A running TamarackDB server, over TCP or its unix socket.
Installation
Connecting
timeout is how long the client waits for a response (60 seconds by
default). It includes the time a request waits for its turn in the server's
queue: a commit, writeProjections(), the bulk deletes, pause(),
resume(), and optimize(). Past it, the client throws a
TimeoutException.
Handling a command
A command runs in one transaction, which lives in the server. Each decision reads events, then writes its events, or none. Your event handlers react, your projections are read and written, and the commit writes everything at once, or nothing. How it works is in Transactions.
beginTransaction()returns the transaction.$client->getTransaction()returns it too, while it's active, and$client->inTransaction()tells whether there is one. A client holds at most one active transaction.- After a
ConcurrencyExceptionor aTransactionNotFoundException, run the whole command again, in a new transaction. - Any server error ends the transaction, except a missing projection and a
TransactionBusyException.$tx->isActive()then returns false, and every call butrollback()throws aNoActiveTransactionException. The same goes once the transaction is committed or rolled back. - A transport failure leaves the transaction active on the client, since the
call may not have reached the server. Call
rollback(). rollback()never throws, and does nothing on a transaction that is already over: it's safe in any error handler.- If a commit's response is lost, the commit can't be sent again. See A lost response.
Reading events
In a transaction
$tx->readEvents() returns a list: every committed event that matches
(Event), then every event written earlier in the transaction that matches
(PendingEvent). The whole response is read before it returns.
The next call after a read must be appendEvents(), with the events of the
decision or an empty list. A decision that rests on no event reads
new NoEvents() first. A response cut short, or one the client can't read,
abandons the transaction and throws: run the command again.
backwards: true reads the newest events first: the pending events, then
the committed ones. limit is the most events returned, pending and
committed together. The read's condition still covers every event that
matches the query. To give an invoice the next number:
Outside a transaction
$client->readEvents() reads committed events, for a projector that
catches up on its own or a projection rebuild. It returns Events, which
you iterate once. Pages are fetched as you iterate, and a page cut short is
resumed after the last event received, so no event is skipped or repeated.
To follow new events, keep the last Sequence Position you read, and pass it to the next read.
backwards: true reads the newest events first, in a single page of at
most pageSize events. It takes no afterSequence.
Queries
Both reads take a Query, new AllEvents(), or new NoEvents().
The filters given together form one item, and an event must match all of
them. or() adds another item, and an event matching any item matches the
query. Within EventType::in(), any of the types matches. Give
Identifier::is() or Metadata::is() twice with the same name to require
both values. To add a filter to every item of a query, use map() and
with():
$query->map(fn (QueryItem $item) => $item->with(Metadata::is('tenantId', 'acme'))).
In identifiers and metadata, a name with one value maps to a string,
a name with several values to a list. NewEvent and QueryItem expose
them the same way.
Writing events
- The write closes the read before it.
appendEvents([])is the decision to write nothing, and the commit still checks it. $result->timeis the time every event of the write carries, and keeps once committed. The events get their Sequence Position at commit.- The payload is an opaque string: encode it as you like (JSON, XML, ...).
Projections
A projection is an opaque payload identified by type and id. In a transaction, it's written with the events it's computed from (see Projections).
getProjection()sees the changes the transaction made. A missing projection doesn't end the transaction. Itsversionis always null: the server keeps the version read.- No write carries a version. A
createneeds no read: your code knows the projection doesn't exist yet. Areplaceor adeleteneeds a read of the projection in the transaction, or acreateearlier in it. - The server refuses reads and writes of projections while a read of events waits for its write, so handle projections once the events are written.
- Empty writes send nothing.
- At commit, a projection changed by another write since it was read, or a
created one that already exists, gets a
ConcurrencyException.
Projection rebuilds
Outside a transaction, the client reads committed projections with their
version, and writes them with writeProjections(), all or nothing:
replace and delete carry the version read. When it no longer matches,
or a created projection already exists, the call throws a
ConcurrencyException.
A rebuild is your application's job. The client gives you the calls it needs:
writeProjections() and the bulk deletes wait for their turn in the
server's queue. See rebuilds
for how to run one.
Pause
While transactions are still open, pause() asks the server again every
$retryAfter milliseconds (1000 by default): $client->pause(retryAfter: 200).
See Pause.
Errors
Every exception implements TamarackDB\Exception\TamarackDBException.
| Exception | When |
|---|---|
ConcurrencyException |
409: a commit whose reads or projections changed since, or a projection version that doesn't match |
TransactionBusyException |
409: another call still runs on the transaction, for example one that timed out. The transaction goes on |
InvalidRequestException |
400: the server rejected the request, or a call that breaks a rule of transactions |
UnauthorizedException |
401: missing or wrong token |
TransactionNotFoundException |
404: the transaction expired, or an error ended it |
PayloadTooLargeException |
413: an event, a projection, or the request is too large |
InternalErrorException |
500 |
StorageFullException |
507: the server's disk is full. Nothing was written; don't retry until the operator frees space |
WriteQueueFullException |
503: too many requests are waiting in the server's queue |
ShuttingDownException |
503: the server is shutting down |
PausedException |
503: beginTransaction() while a pause is requested or in place |
TooManyTransactionsException |
503: beginTransaction() while the server has as many open transactions as it allows |
UnavailableException |
503: health() only, storage is unreachable |
TransactionAlreadyActiveException |
beginTransaction() while the client has an active transaction |
NoActiveTransactionException |
a call on a transaction that is over, or getTransaction() without one |
TransportException |
no full response: server unreachable, connection dropped |
TimeoutException |
a TransportException: the client stopped waiting |
ProtocolException |
a response this client can't make sense of |
InvalidArgumentException |
a value that breaks an API rule, caught before sending |
Every server error extends ServerException, with $statusCode,
$errorCode (such as "ConcurrencyException"), and $detail.
Server state
stats() returns the counters as the server sends them. Their format
follows the server version. See
Counters
for what they mean.
In development mode, a test suite empties the store with:
Development
The integration tests start their own tamarackdb-server processes, with
devMode on and a new database, on a free port and on a unix socket. They
are skipped unless TAMARACKDB_SERVER_BIN points to a server binary, with
tamarackdb-init in the same directory. Build both from a clone of the
server repo, at the version stated above, and check it with -version:
License
MIT, see LICENSE.
All versions of tamarackdb-php with dependencies
ext-curl Version *
ext-json Version *