amphp/sql
Async SQL library for PHP built on Amp. Provides non-blocking database connections, query execution, and result handling with a consistent API, enabling high-concurrency apps without blocking I/O. Supports common drivers and integrates cleanly with event-loop workflows.
amphp/sql aligns well with Laravel’s growing adoption of async/await (via Symfony’s Fiber or Swoole) and event-driven architectures (e.g., queues, real-time APIs). It abstracts SQL operations into a non-blocking, reactive model, which is critical for high-concurrency workloads (e.g., WebSockets, CLI jobs, or serverless functions).Illuminate\Database layer relies on PDO/MySQLi, which are blocking. This package enables a dual-stack approach: async drivers (e.g., amphp/mysql) alongside synchronous ones, reducing lock contention in mixed workloads.StatementInterface and ConnectionInterface allow swapping drivers (e.g., MySQL → PostgreSQL) without rewriting business logic, improving vendor lock-in resistance.App\Contracts\Database\AsyncConnection) and resolved via make() or resolve(). Middleware/bindings can route requests to either sync or async layers.Query Builder would need a parallel implementation (e.g., AsyncQueryBuilder) to leverage amphp/sql interfaces. This is feasible but requires:
Builder to support await/yield for queries.where(), join()) into async-compatible calls.AsyncEloquent) would need to:
await for relationships.await/yield. Laravel’s sync-first ecosystem may resist this shift.beginTransaction()/commit() with proper error handling.tinker, migrations, and debugbar assume sync DB access. Async operations would require custom wrappers or CLI tools.amphp-compatible drivers for the target DB (e.g., amphp/mysql, amphp/pgsql)?Amp\Sql\Pool) compare to Laravel’s pdo_mysql pooling?amphp/byte-stream (lightweight).Fiber (experimental in Laravel 10+) for async-friendly sync code.amphp/mysql or amphp/pgsql as the async layer, with Laravel’s mysql/pgsql as sync fallback.Amp\Sql\Pool for async connections, alongside Laravel’s DatabaseManager.async: true tag) and route to async DB layer.Route::async('GET', '/realtime-data', [DataController::class, 'fetchAsync']);
Builder with an AsyncBuilder trait:
class AsyncBuilder extends Builder {
public function async(): AsyncBuilder { ... }
public function await(): mixed { ... }
}
amphp/sql directly.use Amp\Sql\Connection;
use Amp\Sql\Statement;
$conn = $this->container->make(Connection::class);
$stmt = $conn->prepare('SELECT * FROM users WHERE id = ?');
$result = await $stmt->execute([1]);
class AsyncUser extends Model {
protected $asyncConnection = true;
public function asyncFind(int $id): User { ... }
}
$db = $this->app->make('db.async'); // Async connection
$db = $this->app->make('db.sync'); // Sync fallback
Illuminate\Database bindings with async interfaces.await wrappers).doctrine/dbal or illuminate/database by:
App\Async\Database).amp/loop with pestphp/pest for async tests.swoole/extension or amphp/amp).config/database.php.AppServiceProvider:
$this->app->bind(ConnectionInterface::class, function () {
return new Amp\Sql\Connection('mysql:host=...');
});
Amp\Sql\Exception).Amp\Sql\Logger to debug async query performance.amphp/sql is low-level; driver updates (e.g., amphp/mysql) may require re-testing.composer.json to avoid breaking changes.await for queries in async routes").## Async Database Usage
```php
// Async route handler
public async function index(): JsonResponse {
$users = await User::async()->where('active', true)->get();
return response()->json($users);
}
@deprecated in favor of async alternatives.await calls). Use:
Amp\Sql\Logger for query logging.Xdebug with async-aware configurations.Amp\Sql\Connection::setTimeout().Amp\Sql\Pool to manage connections.How can I help you explore Laravel packages today?