- Install
sqlx-client
cargo install sqlx-cli
Create .env
file in the project root. Define something like this:
POSTGRES_PASSWORD = "password"
DATABASE_URL=postgres://vs-academy-product-postgres:[email protected]/product
Start database container:
docker-compose -f docker-compose-db.yml up -d
Create Postgres database:
sqlx database create
sqlx migrate run
To execute project, using address 127.0.0.1:3000
:
cargo run
To execute defining different address:
cargo run --addr 127.0.0.1:8080
To enable CORS:
cargo run --cors-allow-origin http://127.0.0.1:3000
Build the container image:
./scripts/build.sh
Run docker-compose:
docker-compose up -d
Always when update the project (from git) should execute database modifications:
sqlx migrate run
- Table in singular name
- Primary key should be named
id
and has typeUUID
- All tables should have field
created_at
with typeTIMESTAMP
- SQL statements should be uppercase
- Tables and fields name should be lowercase and inside double quotes
- Date/time fields should be
TIMESTAMP
, without time zone - Date/time fields consider UTC time
- Endpoint in plural name
- Operations:
list GET api/products
create POST api/products/:id
read GET api/products/:id
update PATCH api/products/:id
delete DELETE api/products/:id
- Execute
sqlx migrate add <migration_name>
- Insert SQL statements in the .sql file
- Execute
sqlx migrate run
If you have some compiling problem defining function route it's possible declare this attribute on the function, to get better error:
#[debug_handler]