https://github.com/johnbasrai/rust-sqlx
Example of rust-sqlx/postgres usage with database enum types
https://github.com/johnbasrai/rust-sqlx
postgresql sqlx-postgres tokio
Last synced: 5 months ago
JSON representation
Example of rust-sqlx/postgres usage with database enum types
- Host: GitHub
- URL: https://github.com/johnbasrai/rust-sqlx
- Owner: JohnBasrai
- License: mit
- Created: 2023-10-08T11:53:38.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-10T13:29:08.000Z (about 2 years ago)
- Last Synced: 2025-01-11T21:22:34.177Z (12 months ago)
- Topics: postgresql, sqlx-postgres, tokio
- Language: Rust
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rust-sqlx
Async PostgreSQL database access in Rust using SQLx and Tokio. This project demonstrates
how to insert and update PostgreSQL enum columns using Rust enums and `sqlx`.
At the time of writing, clear examples for updating enum fields were hard to find. This
demo pulls together the correct patterns in a working example. Hopefully it saves others
some time and frustration.
***
## Running
Before running this demo with PostgreSQL (the primary target database), you'll need to
start a local PostgreSQL instance.
The quickest way is to use the official Docker container image:
```bash
docker run --rm --network=host --name=postgres \
-e POSTGRES_PASSWORD=welcome \
-e POSTGRES_USER=postgres \
-p 5432:5432 \
postgres
```
Then, from another shell window, exec into the database container and initialize the schema:
```bash
docker exec -i postgres psql --username=postgres < db-init.sql
```
Back in the first shell window, set the `DATABASE_URL` environment variable and run the application:
```bash
export DATABASE_URL=postgres://postgres@localhost:5432/postgres
cargo run
```
You’ll see records being added. Once the table exceeds 10 rows, the program will begin
deleting one row for each new insert.
### Sample Output
```text
----- 1
----- 2
----- 3: Record { id: 1 }
----- 5
== SELECT users with sqlx::query_as!:
User { id: 1, name: "Attila the long dead Hun:1", role: User }
----- 6
=== SELECT users with query.map..:
[User { id: 1, name: "Attila the long dead Hun:1", role: User }]
----- 7
=== SELECT users with query.map..:
[User { id: 1, name: "Attila the long dead Hun:1", role: User }]
```
If you run again you will see more rows added to the table. When the row count is greater
than 10 it will delete 1 row for each row added.