mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-10 22:44:17 +00:00
Clean up the example a touch
This commit is contained in:
parent
017ee38725
commit
a23bfb60eb
6 changed files with 45 additions and 19 deletions
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "sqlx-example-realworld"
|
||||
name = "sqlx-example-realworld-postgres"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
workspace = "../.."
|
||||
|
|
27
examples/realworld-postgres/README.md
Normal file
27
examples/realworld-postgres/README.md
Normal file
|
@ -0,0 +1,27 @@
|
|||
# Real World SQLx
|
||||
|
||||
## Usage
|
||||
|
||||
Declare the database URL.
|
||||
|
||||
```
|
||||
export DATABASE_URL="postgres://postgres@localhost/realworld"
|
||||
```
|
||||
|
||||
Create the database.
|
||||
|
||||
```
|
||||
createdb -U postgres realworld
|
||||
```
|
||||
|
||||
Load the database schema.
|
||||
|
||||
```
|
||||
psql -d "$DATABASE_URL" -f ./schema.sql
|
||||
```
|
||||
|
||||
Run.
|
||||
|
||||
```
|
||||
cargo run
|
||||
```
|
|
@ -1,7 +0,0 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# Get current directory (of this script)
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
||||
|
||||
# Run schema file
|
||||
psql -d "$DATABASE_URL" -f schema.sql
|
|
@ -1,18 +1,22 @@
|
|||
use sqlx::{Pool, Postgres};
|
||||
use sqlx::PgPool;
|
||||
use std::env;
|
||||
use tide::{Request, Response};
|
||||
|
||||
// NOTE: Tide 0.5.x does not handle errors so any fallible methods just [.unwrap] for the moment.
|
||||
// To be clear, that is not recommended and this should be fixed as soon as Tide fixes its
|
||||
// error handling.
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
dotenv::dotenv()?;
|
||||
|
||||
let pool = Pool::<Postgres>::new(&env::var("DATABASE_URL")?).await?;
|
||||
let pool = PgPool::new(&env::var("DATABASE_URL")?).await?;
|
||||
|
||||
let mut server = tide::with_state(pool);
|
||||
|
||||
server.at("/api/users").post(register);
|
||||
|
||||
server.listen(("localhost", 8080)).await?;
|
||||
server.listen(("0.0.0.0", 8080)).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -21,21 +25,24 @@ async fn main() -> anyhow::Result<()> {
|
|||
// https://github.com/gothinkster/realworld/tree/master/api#registration
|
||||
|
||||
// #[post("/api/users")]
|
||||
async fn register(mut req: Request<Pool<Postgres>>) -> Response {
|
||||
async fn register(mut req: Request<PgPool>) -> Response {
|
||||
#[derive(serde::Deserialize)]
|
||||
struct RegisterRequestBody {
|
||||
username: String,
|
||||
email: String,
|
||||
// TODO: password: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
// TODO: Handle the unwrap
|
||||
let body: RegisterRequestBody = req.body_json().await.unwrap();
|
||||
|
||||
let mut pool = req.state();
|
||||
|
||||
// TODO: Handle the unwrap
|
||||
let (user_id,): (i64,) = sqlx::query!(
|
||||
"INSERT INTO users (username, email) VALUES ($1, $2) RETURNING id",
|
||||
r#"
|
||||
INSERT INTO users ( username, email )
|
||||
VALUES ( $1, $2 )
|
||||
RETURNING id
|
||||
"#,
|
||||
&*body.username,
|
||||
&*body.email
|
||||
)
|
||||
|
@ -48,7 +55,6 @@ async fn register(mut req: Request<Pool<Postgres>>) -> Response {
|
|||
id: i64,
|
||||
}
|
||||
|
||||
// TODO: Handle the unwrap
|
||||
Response::new(200)
|
||||
.body_json(&RegisterResponseBody { id: user_id })
|
||||
.unwrap()
|
||||
|
|
|
@ -201,5 +201,5 @@ macro_rules! impl_fmt_error {
|
|||
f.pad(self.message())
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ pub use sqlx_core::query_as_mapped;
|
|||
pub use sqlx_core::mysql::{self, MySql, MySqlConnection, MySqlPool};
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
pub use sqlx_core::postgres::{self, Postgres, PgConnection, PgPool};
|
||||
pub use sqlx_core::postgres::{self, PgConnection, PgPool, Postgres};
|
||||
|
||||
#[cfg(feature = "macros")]
|
||||
#[doc(hidden)]
|
||||
|
|
Loading…
Reference in a new issue