2018-04-13 03:18:42 +02:00
# diesel
2022-02-06 09:25:38 +01:00
Basic integration of [Diesel ](https://diesel.rs/ ) using SQLite for Actix Web.
2018-04-13 03:18:42 +02:00
## Usage
2020-01-27 13:20:04 +01:00
### Install SQLite
2018-04-13 03:18:42 +02:00
2020-01-27 13:20:04 +01:00
```sh
# on OpenSUSE
sudo zypper install sqlite3-devel libsqlite3-0 sqlite3
# on Ubuntu
sudo apt-get install libsqlite3-dev sqlite3
# on Fedora
sudo dnf install libsqlite3x-devel sqlite3x
# on macOS (using homebrew)
brew install sqlite3
```
### Initialize SQLite Database
```sh
2022-02-18 03:27:07 +01:00
cd databases/diesel
2020-01-27 13:20:04 +01:00
cargo install diesel_cli --no-default-features --features sqlite
2019-06-12 23:03:20 +02:00
echo "DATABASE_URL=test.db" > .env
2022-11-04 23:11:36 +01:00
diesel setup
2018-04-13 03:18:42 +02:00
diesel migration run
```
2020-01-27 13:20:04 +01:00
There will now be a database file at `./test.db` .
2018-04-13 03:18:42 +02:00
2020-01-27 13:20:04 +01:00
### Running Server
```sh
2022-02-18 03:27:07 +01:00
cd databases/diesel
2022-02-18 03:32:44 +01:00
cargo run
2020-01-27 13:20:04 +01:00
2018-04-13 03:18:42 +02:00
# Started http server: 127.0.0.1:8080
```
2020-01-27 13:20:04 +01:00
### Available Routes
2018-04-13 03:18:42 +02:00
2020-01-27 13:20:04 +01:00
#### `POST /user`
2018-04-13 03:18:42 +02:00
2020-01-27 13:20:04 +01:00
Inserts a new user into the SQLite DB.
2018-04-13 03:18:42 +02:00
2020-01-27 13:20:04 +01:00
Provide a JSON payload with a name. Eg:
2022-03-06 01:43:10 +01:00
2020-01-27 13:20:04 +01:00
```json
{ "name": "bill" }
```
On success, a response like the following is returned:
2022-03-06 01:43:10 +01:00
2020-01-27 13:20:04 +01:00
```json
{
2022-03-06 01:43:10 +01:00
"id": "9e46baba-a001-4bb3-b4cf-4b3e5bab5e97",
"name": "bill"
2020-01-27 13:20:04 +01:00
}
```
< details >
< summary > Client Examples< / summary >
2022-03-06 01:43:10 +01:00
Using [HTTPie ](https://httpie.org/ ):
```sh
http POST localhost:8080/user name=bill
```
Using cURL:
```sh
curl -S -X POST --header "Content-Type: application/json" --data '{"name":"bill"}' http://localhost:8080/user
```
2020-01-27 13:20:04 +01:00
< / details >
#### `GET /user/{user_uid}`
Gets a user from the DB using its UID (returned from the insert request or taken from the DB directly). Returns a 404 when no user exists with that UID.
< details >
< summary > Client Examples< / summary >
2022-03-06 01:43:10 +01:00
Using [HTTPie ](https://httpie.org/ ):
```sh
http localhost:8080/user/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
```
Using cURL:
```sh
curl -S http://localhost:8080/user/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
```
2020-01-27 13:20:04 +01:00
< / details >
### Explore The SQLite DB
```sh
2018-04-13 03:18:42 +02:00
sqlite3 test.db
2020-01-27 13:20:04 +01:00
```
```
2018-04-13 03:18:42 +02:00
sqlite> .tables
2020-01-27 13:20:04 +01:00
sqlite> SELECT * FROM users;
2018-04-13 03:18:42 +02:00
```
2020-01-27 13:20:04 +01:00
## Using Other Databases
2018-04-13 03:18:42 +02:00
2020-01-27 13:20:04 +01:00
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 )