mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
feat(examples): add initial postgres+async-std quickstart
This commit is contained in:
parent
dbf13844d9
commit
9cc80af5e4
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -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
40
examples/Cargo.lock
generated
@ -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"
|
||||
|
||||
@ -2,5 +2,6 @@
|
||||
members = [
|
||||
"quickstart/mysql+blocking",
|
||||
"quickstart/mysql+async-std",
|
||||
"quickstart/mysql+tokio"
|
||||
"quickstart/mysql+tokio",
|
||||
"quickstart/postgres+async-std",
|
||||
]
|
||||
|
||||
15
examples/quickstart/postgres+async-std/Cargo.toml
Normal file
15
examples/quickstart/postgres+async-std/Cargo.toml
Normal 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" ] }
|
||||
36
examples/quickstart/postgres+async-std/src/main.rs
Normal file
36
examples/quickstart/postgres+async-std/src/main.rs
Normal 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(())
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user