Skip to content

dbrucknr/rust-axum-pg-docker-crud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Running the API:

  • You will need to add a .env file in the root directory with the following structure:
POSTGRES_PORT=5432
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="postgres"
POSTGRES_DB="postgres"
DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres-database:${POSTGRES_PORT}/${POSTGRES_DB}"
  • Docker Compose

    • docker compose build
    • docker compose up
  • API Base URL: http://localhost:3000/api

  • Database Viewer: http://localhost:8081/

  • If not using Docker Compose: watchexec --restart --watch src --exts rs -- cargo run

    • Note: Database Service is Required for app to launch. Make sure to either manually launch + configure the database, or comment out all database relevant code.
    • Note: this command requires the installation of watchexec-cli
    • Use this command to add it: cargo install --locked watchexec-cli

Installed Dependencies + Method

cargo new axum-pg-docker-crud cargo add axum cargo add tokio --features=full cargo add serde --features=derive cargo add chrono cargo add diesel --features=chrono,postgres cargo add diesel-async --features bb8,postgres

Diesel ORM

NOTE:

  • Diesel has its own CLI tool that is a standalone binary. That means it's not a dependency of the project, and doesn't impact the project's code at all.
  • I opted to install it by following the directions here: Installing Diesel CLI

Diesel CLI Setup Steps

  1. diesel setup --database-url='postgresql://postgres:postgres@localhost/postgres'
    • This creates a root level /migrations folder
    • Note, that Database URL required @localhost instead of the compose.yaml defined service. This will require an adjustment (perhaps setting the database URL as a computed value on a struct?)
  2. I editted the autogenerated diesel.toml file and set the schema.rs location to be within the /src/database directory.
  3. diesel migration generate create_identities
    • This migration was named create_identities because I had already set identities as an API namespace.
    • You need to write the SQL for these Migrations by hand in the /migrations directory.
  4. Apply the written SQL Migrations: diesel migration run
  5. I went to http://localhost:8081/, refreshed the page, and checked the tables.
    • I verified the identities table was made and queryable from the Web GUI.
  6. In following the Diesel tutorial, I ran diesel migration redo to verify the down.sql file correctly rolled back migrations.
  7. I also verified my config changes to diesel.toml were reflected in the /src/database/schema.rs file
  8. Next, I added /src/database/schema.rs into /src/database/mod.rs
  9. I created a models.rs file in /src/database

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published