id_effect_sql
Driver-agnostic SQL client capability for id_effect, aligned with Effect.ts @effect/sql, on the 0.7 kernel.
Application code reads the SqlClientService capability (SqlClientEnv) from its environment; production wires the PostgreSQL driver behind the pg feature, tests use the scriptable in-memory TestSqlClient. The client and transactions are scoped layers: their resources are released when the layer's scope closes, however the program ends.
Quick start
use Exit;
use ;
use ;
/// Domain code depends only on the capability.
/// Runs inside a transaction: committed on success, rolled back on failure or interrupt.
Parameters are typed SqlValues (params![id, title] encodes Rust values; None binds NULL), and rows are typed: row.get::<i64>("id") / row.get::<String>(1) decode a column (uuid, jiff dates and times, rust_decimal, serde_json included) and return a SqlError naming the column instead of panicking.
Brackets are available too: with_transaction(client, |tx| ..) and transaction(|tx| ..) (which reads the client from the environment) commit on success and roll back on failure, defect or interrupt; a failed COMMIT is a typed error there.
With the pg feature, swap sql_client_layer(test) for id_effect_sql::pg::PgSqlClient::layer(PgPoolConfig::from_url(url)), which opens a pool when built and closes it afterwards, and provides the PgPool too. Its effects run on tokio (id_effect_tokio::TokioRuntime).
Modules
| Module | Description |
|---|---|
error |
SqlError |
client |
SqlClient, SqlClientService, sql_client_layer, provide_sql_client, query, execute, TestSqlClient (SqlParam, deprecated) |
value |
SqlValue, SqlType, ToSqlValue, FromSqlValue, ValueError, Json<T>, params! |
row |
SqlRow (get, value, from_pairs), SqlColumn, ColumnIndex |
transaction |
SqlTransaction, SqlTransactionService, transaction_layer, with_transaction (alias transaction_scope), transaction |
pg (feature pg) |
PgSqlClient (+ PgSqlClient::layer), PgPool, PgPoolConfig, pg_pool_from_config, provide_pg_sql_client, provide_pg_pool |
Design
See ADR adr-sql-driver-choice.md and the spec phase-c-sql.md.
Testing
Live PostgreSQL tests (tests/pg.rs, live_postgres_*) read DATABASE_URL. Without a database they print
SKIP … to stderr; with ID_EFFECT_REQUIRE_PG=1 (set by CI whenever it provides a Postgres service) a
missing or unreachable database fails the run instead.