Skip to content

Commit c5ce3c1

Browse files
authored
Merge pull request #992 from alexted/feature/diesel-async-example
Diesel-async usage example
2 parents e431c4a + f7f01a1 commit c5ce3c1

File tree

16 files changed

+547
-5
lines changed

16 files changed

+547
-5
lines changed

.github/workflows/ci-nightly.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@ jobs:
2121
with:
2222
toolchain: ${{ matrix.version }}
2323

24+
- name: Install system packages
25+
run: |
26+
sudo apt-get update
27+
sudo apt-get -y install sqlite3
28+
sudo apt-get -y install libpq-dev
29+
2430
- name: Install DB CLI tools
2531
run: |
2632
cargo install --force sqlx-cli --no-default-features --features=sqlite,rustls
27-
cargo install --force diesel_cli --no-default-features --features sqlite
33+
cargo install --force diesel_cli --no-default-features --features=sqlite,postgres
2834
2935
- name: Create Test DBs
3036
env:
3137
DATABASE_URL: sqlite://./todo.db
3238
run: |
33-
sudo apt-get update && sudo apt-get install sqlite3
3439
sqlx database create
3540
chmod a+rwx ./todo.db
3641
sqlx migrate run --source=./basics/todo/migrations

.github/workflows/ci.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@ jobs:
2626
with:
2727
toolchain: ${{ matrix.version }}
2828

29+
- name: Install system packages
30+
run: |
31+
sudo apt-get update
32+
sudo apt-get -y install sqlite3
33+
sudo apt-get -y install libpq-dev
34+
2935
- name: Install DB CLI tools
3036
run: |
3137
cargo install --force sqlx-cli --no-default-features --features=sqlite,rustls
32-
cargo install --force diesel_cli --no-default-features --features sqlite
38+
cargo install --force diesel_cli --no-default-features --features=sqlite,postgres
3339
3440
- name: Create Test DBs
3541
env:
3642
DATABASE_URL: sqlite://./todo.db
3743
run: |
38-
sudo apt-get update && sudo apt-get install sqlite3
3944
sqlx database create
4045
chmod a+rwx ./todo.db
4146
sqlx migrate run --source=./basics/todo/migrations

Cargo.lock

+74-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ members = [
1717
"cors/backend",
1818
"data-factory",
1919
"databases/diesel",
20+
"databases/diesel-async",
2021
"databases/mongodb",
2122
"databases/mysql",
2223
"databases/postgres",

databases/diesel-async/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "db-diesel-async"
3+
version = "1.0.0"
4+
edition = "2021"
5+
6+
[features]
7+
postgres_tests = []
8+
9+
[dependencies]
10+
actix-web.workspace = true
11+
diesel = { version = "2", default-features = false, features = ["uuid"] }
12+
diesel-async = { version = "0.5", features = ["postgres", "bb8", "async-connection-wrapper"] }
13+
serde.workspace = true
14+
uuid.workspace = true
15+
dotenvy.workspace = true
16+
env_logger.workspace = true
17+
log.workspace = true

databases/diesel-async/README.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# diesel
2+
3+
Basic integration of [Diesel-async](https://github.com/weiznich/diesel_async) using PostgreSQL for Actix Web.
4+
5+
## Usage
6+
7+
### Install PostgreSQL
8+
9+
```sh
10+
# on any OS
11+
docker run -d --restart unless-stopped --name postgresql -e POSTGRES_USER=test-user -e POSTGRES_PASSWORD=password -p 5432:5432 -v postgres_data:/var/lib/postgresql/data postgres:alpine
12+
```
13+
make sure it has successfully started up and is running
14+
```sh
15+
# on any OS
16+
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}"
17+
```
18+
19+
### Initialize PostgreSQL Database
20+
21+
```sh
22+
cd databases/diesel-async
23+
cargo install diesel_cli --no-default-features --features postgres
24+
25+
echo DATABASE_URL=postgres://test-user:password@localhost:5432/test_db > .env
26+
diesel setup
27+
diesel migration run
28+
```
29+
30+
The database will now be created in your PostgreSQL instance.
31+
```sh
32+
docker exec -i postgresql psql -U test-user -c "\l"
33+
```
34+
35+
### Running Server
36+
37+
```sh
38+
cd databases/diesel-async
39+
cargo run
40+
41+
# Started http server: 127.0.0.1:8080
42+
```
43+
44+
### Available Routes
45+
46+
#### `POST /items`
47+
48+
Inserts a new item into the PostgreSQL DB.
49+
50+
Provide a JSON payload with a name. Eg:
51+
52+
```json
53+
{ "name": "thingamajig" }
54+
```
55+
56+
On success, a response like the following is returned:
57+
58+
```json
59+
{
60+
"id": "01948982-67d0-7a55-b4b1-8b8b962d8c6b",
61+
"name": "thingamajig"
62+
}
63+
```
64+
65+
<details>
66+
<summary>Client Examples</summary>
67+
68+
Using [HTTPie]:
69+
70+
```sh
71+
http POST localhost:8080/items name=thingamajig
72+
```
73+
74+
Using cURL:
75+
76+
```sh
77+
curl -S -X POST --header "Content-Type: application/json" --data '{"name":"thingamajig"}' http://localhost:8080/items
78+
```
79+
80+
</details>
81+
82+
#### `GET /items/{item_uid}`
83+
84+
Gets an item from the DB using its UID (returned from the insert request or taken from the DB directly). Returns a 404 when no item exists with that UID.
85+
86+
<details>
87+
<summary>Client Examples</summary>
88+
89+
Using [HTTPie]:
90+
91+
```sh
92+
http localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
93+
```
94+
95+
Using cURL:
96+
97+
```sh
98+
curl -S http://localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
99+
```
100+
101+
</details>
102+
103+
### Explore The PostgreSQL DB
104+
105+
```sh
106+
docker exec -i postgresql psql -U test-user -d test_db -c "select * from public.items"
107+
```
108+
109+
## Using Other Databases
110+
111+
You can find a complete example of Diesel + PostgreSQL at: [https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix](https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix)
112+
113+
[httpie]: https://httpie.io/cli

databases/diesel-async/diesel.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# For documentation on how to configure this file,
2+
# see https://diesel.rs/guides/configuring-diesel-cli
3+
4+
[print_schema]
5+
file = "src/schema.rs"
6+
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
7+
8+
[migrations_directory]
9+
dir = "/home/alex/CLionProjects/actix-with-async-diesel/migrations"

databases/diesel-async/migrations/.keep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- This file was automatically created by Diesel to setup helper functions
2+
-- and other internal bookkeeping. This file is safe to edit, any future
3+
-- changes will be added to existing projects as new migrations.
4+
5+
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
6+
DROP FUNCTION IF EXISTS diesel_set_updated_at();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- This file was automatically created by Diesel to setup helper functions
2+
-- and other internal bookkeeping. This file is safe to edit, any future
3+
-- changes will be added to existing projects as new migrations.
4+
5+
6+
-- Sets up a trigger for the given table to automatically set a column called
7+
-- `updated_at` whenever the row is modified (unless `updated_at` was included
8+
-- in the modified columns)
9+
--
10+
-- # Example
11+
--
12+
-- ```sql
13+
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
14+
--
15+
-- SELECT diesel_manage_updated_at('users');
16+
-- ```
17+
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS
18+
$$
19+
BEGIN
20+
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
21+
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
22+
END;
23+
$$ LANGUAGE plpgsql;
24+
25+
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS
26+
$$
27+
BEGIN
28+
IF (
29+
NEW IS DISTINCT FROM OLD AND
30+
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
31+
) THEN
32+
NEW.updated_at := current_timestamp;
33+
END IF;
34+
RETURN NEW;
35+
END;
36+
$$ LANGUAGE plpgsql;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- This file should undo anything in `up.sql`
2+
DROP TABLE IF EXISTS items;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Your SQL goes here
2+
CREATE TABLE IF NOT EXISTS items
3+
(
4+
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
5+
name VARCHAR NOT NULL
6+
);

0 commit comments

Comments
 (0)