switon / db
Direct SQL client with named binds, transient transactions, and optional read or write routing for Switon Framework
v1.0.0
2026-06-06 13:43 UTC
Requires
- php: >=8.3
- ext-pdo: *
- switon/command: ^1.0
- switon/core: ^1.0
- switon/di: ^1.0
- switon/event: ^1.0
- switon/pool: ^1.0
Requires (Dev)
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.0
- switon/testing: ^1.0
README
Switon's database client layer for direct table operations, routed reads, pooled connections, transaction-safe connections, and SQL observability.
Highlights
- Table operations:
ClientInterfaceoffersinsert,update,upsert, anddeletealongside 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.