Function sqlm_postgres::connect
source · pub async fn connect() -> Result<Client, Error>
Expand description
Establish a database connection.
This function is automatically called when awaiting queries created with sql!
. When first
called, a connection pool is created, which expects the env variable DATABASE_URL
to be set.
When having multiple sequential queries, it is recommended to manually establish a connection
and pass it to sql!
via Sql::run_with
.
§Examples
let conn = connect().await?;
let name: String = sql!("SELECT name FROM users WHERE id = {id}", id = 1i64)
.run_with(conn)
.await?;
let mut conn = connect().await?;
let tx = conn.transaction().await?;
let name: String = sql!("SELECT name FROM users WHERE id = {id}", id = 1i64)
.run_with(&tx)
.await?;
tx.commit().await?;