Skip to main content

Challenge

Now it's time to apply what you've learned about database access with SQLx! In this module's challenge, you'll:

  1. Set up a PostgreSQL database using Docker:

    • Use the provided docker-compose.yml file to run a PostgreSQL instance
    • Connect to the database using the correct connection string
    export DATABASE_URL=postgresql://postgres:mysupersecretlocalpassword@localhost:5432/users
  2. Add SQLx to your project:

    • Update your Cargo.toml to include sqlx with the postgres and runtime-tokio features enabled
    • Add thiserror for better error handling
  3. Create a database implementation of the DataAccess trait:

    • Create a PostgresUsers struct that connects to your database
    • Implement the trait methods to query and store users in PostgreSQL
    • Add proper error handling for database operations
  4. Apply your database migrations

    • Install the SQLx CLI
    • The actual migration scripts are included in the repo
    • Run the migration to apply your schema
    cargo sqlx migrate run
  5. Update your API handlers to work with the new implementation:

    • Initialize the PostgresUsers data access in your main function
    • Make sure all API endpoints correctly handle database errors

The SQL Queries you will need

Get User

SELECT email_address, name, password
FROM users
WHERE email_address = $1

Insert User

INSERT INTO users ( email_address, name, password )
VALUES ( $1, $2, $3 )

The starter code for this challenge is available on GitHub.

If you're struggling, you can find a solution on GitHub. Try it on your own first, if you're finding it difficult that's good. It means you're learning.

To test your implementation, follow these steps:

# Start the PostgreSQL database
docker compose up -d

# Set the database URL
export DATABASE_URL=postgresql://postgres:mysupersecretlocalpassword@localhost:5432/users

# Run migrations and start the application
cargo sqlx migrate run
cargo run

Good luck, and remember that working with databases in Rust gives you the power of compile-time SQL checking, helping you catch errors before your code even runs!