mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-10 14:34:19 +00:00
Copy 'todos' example directory as starting point
This commit is contained in:
parent
ac0690822c
commit
da178405b9
4 changed files with 159 additions and 0 deletions
14
examples/postgres/mockable-todos/Cargo.toml
Normal file
14
examples/postgres/mockable-todos/Cargo.toml
Normal file
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "sqlx-example-postgres-todos"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
workspace = "../../../"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
async-std = { version = "1.4.0", features = [ "attributes" ] }
|
||||
futures = "0.3"
|
||||
paw = "1.0"
|
||||
sqlx = { path = "../../../", features = ["postgres", "offline"] }
|
||||
structopt = { version = "0.3", features = ["paw"] }
|
||||
dotenv = "0.15.0"
|
41
examples/postgres/mockable-todos/README.md
Normal file
41
examples/postgres/mockable-todos/README.md
Normal file
|
@ -0,0 +1,41 @@
|
|||
# TODOs Example
|
||||
|
||||
## Setup
|
||||
|
||||
1. Declare the database URL
|
||||
|
||||
```
|
||||
export DATABASE_URL="postgres://postgres:password@localhost/todos"
|
||||
```
|
||||
|
||||
2. Create the database.
|
||||
|
||||
```
|
||||
$ sqlx db create
|
||||
```
|
||||
|
||||
3. Run sql migrations
|
||||
|
||||
```
|
||||
$ sqlx migrate run
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Add a todo
|
||||
|
||||
```
|
||||
cargo run -- add "todo description"
|
||||
```
|
||||
|
||||
Complete a todo.
|
||||
|
||||
```
|
||||
cargo run -- done <todo id>
|
||||
```
|
||||
|
||||
List all todos
|
||||
|
||||
```
|
||||
cargo run
|
||||
```
|
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE IF NOT EXISTS todos
|
||||
(
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
description TEXT NOT NULL,
|
||||
done BOOLEAN NOT NULL DEFAULT FALSE
|
||||
);
|
98
examples/postgres/mockable-todos/src/main.rs
Normal file
98
examples/postgres/mockable-todos/src/main.rs
Normal file
|
@ -0,0 +1,98 @@
|
|||
use sqlx::postgres::PgPool;
|
||||
use sqlx::Done;
|
||||
use std::env;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(StructOpt)]
|
||||
struct Args {
|
||||
#[structopt(subcommand)]
|
||||
cmd: Option<Command>,
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
enum Command {
|
||||
Add { description: String },
|
||||
Done { id: i64 },
|
||||
}
|
||||
|
||||
#[async_std::main]
|
||||
#[paw::main]
|
||||
async fn main(args: Args) -> anyhow::Result<()> {
|
||||
let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?;
|
||||
|
||||
match args.cmd {
|
||||
Some(Command::Add { description }) => {
|
||||
println!("Adding new todo with description '{}'", &description);
|
||||
let todo_id = add_todo(&pool, description).await?;
|
||||
println!("Added new todo with id {}", todo_id);
|
||||
}
|
||||
Some(Command::Done { id }) => {
|
||||
println!("Marking todo {} as done", id);
|
||||
if complete_todo(&pool, id).await? {
|
||||
println!("Todo {} is marked as done", id);
|
||||
} else {
|
||||
println!("Invalid id {}", id);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
println!("Printing list of all todos");
|
||||
list_todos(&pool).await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_todo(pool: &PgPool, description: String) -> anyhow::Result<i64> {
|
||||
let rec = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO todos ( description )
|
||||
VALUES ( $1 )
|
||||
RETURNING id
|
||||
"#,
|
||||
description
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(rec.id)
|
||||
}
|
||||
|
||||
async fn complete_todo(pool: &PgPool, id: i64) -> anyhow::Result<bool> {
|
||||
let rows_affected = sqlx::query!(
|
||||
r#"
|
||||
UPDATE todos
|
||||
SET done = TRUE
|
||||
WHERE id = $1
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.execute(pool)
|
||||
.await?
|
||||
.rows_affected();
|
||||
|
||||
Ok(rows_affected > 0)
|
||||
}
|
||||
|
||||
async fn list_todos(pool: &PgPool) -> anyhow::Result<()> {
|
||||
let recs = sqlx::query!(
|
||||
r#"
|
||||
SELECT id, description, done
|
||||
FROM todos
|
||||
ORDER BY id
|
||||
"#
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
|
||||
for rec in recs {
|
||||
println!(
|
||||
"- [{}] {}: {}",
|
||||
if rec.done { "x" } else { " " },
|
||||
rec.id,
|
||||
&rec.description,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue