For the complete documentation index, see llms.txt. This page is also available as Markdown.

Database Connection (DBAL Module)

Database connection with DBAL module in Tempest

We can use Ecotone's Tempest integration to reuse the Connection that is already configured in your Application. This is what powers DBAL features such as the transactional outbox, dead letter, deduplication, document store, event sourcing and multi-tenant.

In a Tempest application the database is configured through a database.config.php file, for example:

use Tempest\Database\Config\PostgresConfig;

return new PostgresConfig(
    host: env('DB_HOST', 'localhost'),
    port: env('DB_PORT', '5432'),
    username: env('DB_USERNAME', 'app'),
    password: env('DB_PASSWORD', ''),
    database: env('DB_DATABASE', 'app'),
);

Using a ServiceContext we tell Ecotone to reuse this Connection as the default:

use Ecotone\Messaging\Attribute\ServiceContext;
use Ecotone\Tempest\Config\TempestConnectionReference;

final class EcotoneConfiguration
{
    #[ServiceContext]
    public function tempestConnection(): TempestConnectionReference
    {
        return TempestConnectionReference::defaultConnection();
    }
}

Multi-Tenant

For multi-tenant applications, register a tagged DatabaseConfig per tenant in your Tempest configuration, and map each tenant to a TempestConnectionReference by tag:

A command or query carrying the tenant header is then routed to the matching tenant database automatically.

The DBAL features require the ecotone/dbal package: composer require ecotone/dbal.

Last updated

Was this helpful?