From 5ac0f5ec2da6300b8ff2b829c76e28c0eb2e2517 Mon Sep 17 00:00:00 2001 From: Esther <33323594+eminano@users.noreply.github.com> Date: Mon, 18 Mar 2024 10:27:21 +0100 Subject: [PATCH] Add rust sqlx connect example (#310) Link to the relevant section in the preview: https://xata-docs-pr-310.vercel.app/docs/rust-sqlx --------- Co-authored-by: Tudor Golubenco --- 020-Connect/070-Rust/010-sqlx.mdx | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 020-Connect/070-Rust/010-sqlx.mdx diff --git a/020-Connect/070-Rust/010-sqlx.mdx b/020-Connect/070-Rust/010-sqlx.mdx new file mode 100644 index 00000000..c7dd17fe --- /dev/null +++ b/020-Connect/070-Rust/010-sqlx.mdx @@ -0,0 +1,56 @@ +--- +title: Connect to Postgres with rust sqlx +navTitle: sqlx +keywords: ['rust', 'sqlx'] +description: Connect to Postgres with Rust sqlx +slug: rust-sqlx +published: true +--- + +To use Xata with Rust [sqlx](https://github.com/launchbadge/sqlx), you have the option to connect with the connection string (DSN). +Please replace the placeholders in the connection string, as indicated on the [connect to Postgres](/docs/postgres) page. For this example sqlx is used with tokio and rustls. + +```rust +use sqlx::FromRow; +use sqlx::{Connection, PgConnection}; + +#[tokio::main] +async fn main() -> Result<(), sqlx::Error> { + let conn_string = "postgresql://:@.sql.xata.sh:5432/:?sslmode=require"; + + let mut conn = PgConnection::connect(conn_string).await?; + + sqlx::query("CREATE TABLE IF NOT EXISTS test(id serial, name text);") + .execute(&mut conn) + .await?; + + let _row: (i32,) = sqlx::query_as("INSERT INTO test (name) VALUES($1) RETURNING id") + .bind("a") + .fetch_one(&mut conn) + .await?; + + #[derive(Debug, FromRow)] + struct TestRow { + id: i32, + name: String, + } + + let select_query = sqlx::query_as::<_, TestRow>("SELECT id, name FROM test"); + let rows: Vec = select_query.fetch_all(&mut conn).await?; + println!("\n=== select test rows: \n{:?}", rows); + + sqlx::query("DROP TABLE IF EXISTS test;") + .execute(&mut conn) + .await?; + + Ok(()) +} +``` + +### Cargo dependencies + +```toml +[dependencies] +tokio = { version = "1", features = ["full"] } +sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "postgres" ] } +```