Direct SQL client with named binds, transient transactions, and optional read or write routing for Switon Framework

Maintainers

Package info

github.com/switon-php/db

Documentation

pkg:composer/switon/db

Statistics

Installs: 8

Dependents: 4

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-06 13:43 UTC

This package is auto-updated.

Last update: 2026-06-07 03:57:16 UTC


README

CI PHP 8.3+

Switon's database client layer for direct table operations, routed reads, pooled connections, transaction-safe connections, and SQL observability.

Highlights

  • Table operations: ClientInterface offers insert, update, upsert, and delete alongside raw SQL.
  • Read/write routing: reads and writes can be split across the right pool automatically.
  • Connection pooling: read and write calls reuse pooled connections, with transient clients available for transactions.
  • Transient transaction clients: local transactions can use a client pinned to one connection.
  • Metadata and SQL helpers: table metadata and SQL builders are available from the same client.
  • Observable queries: DB events can feed collectors such as SqlCollector.

Installation

composer require switon/db

Quick Start

use Switon\Core\Attribute\Autowired;
use Switon\Db\ClientInterface;

final class UserRepository
{
    #[Autowired] protected ClientInterface $db;

    public function find(int $id): ?array
    {
        $rows = $this->db->fetchAll(
            'SELECT * FROM [users] WHERE id = :id LIMIT 1',
            ['id' => $id],
        );

        return $rows[0] ?? null;
    }

    public function rename(int $id, string $name): void
    {
        $db = $this->db->getTransient('default');
        $db->begin();

        try {
            $db->executeUpdate(
                'UPDATE [users] SET name = :name WHERE id = :id',
                ['id' => $id, 'name' => $name],
            );
            $db->commit();
        } catch (\Throwable $e) {
            $db->rollback();
            throw $e;
        }
    }
}

Docs: https://docs.switon.dev/latest/db

License

MIT.