feat(examples): add initial postgres+async-std quickstart

This commit is contained in:
Ryan Leckey 2021-03-20 00:16:52 -07:00
parent dbf13844d9
commit 9cc80af5e4
No known key found for this signature in database
GPG Key ID: F8AA68C235AB08C9
5 changed files with 95 additions and 1 deletions

2
Cargo.lock generated
View File

@ -1068,6 +1068,7 @@ dependencies = [
"futures-util",
"sqlx-core",
"sqlx-mysql",
"sqlx-postgres",
]
[[package]]
@ -1122,6 +1123,7 @@ version = "0.6.0-pre"
dependencies = [
"anyhow",
"atoi",
"base64",
"bitflags",
"bytes",
"bytestring",

40
examples/Cargo.lock generated
View File

@ -153,6 +153,15 @@ version = "4.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0"
[[package]]
name = "atoi"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5"
dependencies = [
"num-traits",
]
[[package]]
name = "atomic-waker"
version = "1.0.0"
@ -987,6 +996,7 @@ dependencies = [
"futures-util",
"sqlx-core",
"sqlx-mysql",
"sqlx-postgres",
]
[[package]]
@ -1028,6 +1038,25 @@ dependencies = [
"url",
]
[[package]]
name = "sqlx-postgres"
version = "0.6.0-pre"
dependencies = [
"atoi",
"base64",
"bitflags",
"bytes",
"bytestring",
"either",
"futures-io",
"futures-util",
"log",
"memchr",
"percent-encoding",
"sqlx-core",
"url",
]
[[package]]
name = "sqlx_example_quickstart_mysql_async-std"
version = "0.0.0"
@ -1060,6 +1089,17 @@ dependencies = [
"tokio",
]
[[package]]
name = "sqlx_example_quickstart_postgres_async-std"
version = "0.0.0"
dependencies = [
"anyhow",
"async-std",
"env_logger",
"log",
"sqlx",
]
[[package]]
name = "subtle"
version = "2.4.0"

View File

@ -2,5 +2,6 @@
members = [
"quickstart/mysql+blocking",
"quickstart/mysql+async-std",
"quickstart/mysql+tokio"
"quickstart/mysql+tokio",
"quickstart/postgres+async-std",
]

View File

@ -0,0 +1,15 @@
[package]
name = "sqlx_example_quickstart_postgres_async-std"
version = "0.0.0"
license = "MIT OR Apache-2.0"
edition = "2018"
authors = [
"LaunchBadge <contact@launchbadge.com>"
]
[dependencies]
anyhow = "1.0.36"
async-std = { version = "1.8.0", features = ["attributes"] }
log = "0.4"
env_logger = "0.8.2"
sqlx = { path = "../../../sqlx", features = [ "async-std", "postgres-async" ] }

View File

@ -0,0 +1,36 @@
use sqlx::postgres::{PgConnectOptions, PgConnection};
#[async_std::main]
async fn main() -> anyhow::Result<()> {
env_logger::try_init()?;
// start by parsing the connection URL (typically from an environment variable)
let mut conn: PgConnection = PgConnectOptions::parse("postgres://postgres@localhost")?
// set a password (perhaps from somewhere else than the rest of the URL)
.password("password")
// connect to the database (blocking)
.connect()
.await?;
// the following are equivalent to the above:
// let mut conn = PgConnection::<AsyncStd>::connect("mysql://root:password@localhost").await?;
// let mut conn = <PgConnection>::connect("mysql://root:password@localhost").await?;
// let mut conn = PgConnectOptions::<AsyncStd>::new().username("root").password("password").connect().await?;
// let mut conn = <PgConnectOptions>::new().username("root").password("password").connect().await?;
// the <...> syntax is an escape into the type syntax
// when writing a *type*, Rust allows default type parameters
// as opposed to writing a *path* where it does not (yet)
// ping, this makes sure the server is still there
// hopefully it is we did just connect to it
conn.ping().await?;
// close the connection explicitly
// this kindly informs the database server that we'll be terminating
// while not strictly required, the server will dispose of connection resources faster
conn.close().await?;
Ok(())
}