Switon's database client layer for direct table operations, routed reads, pooled connections, transaction-safe connections, and SQL observability.
- 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.
composer require switon/dbuse 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
MIT.