- sqlx::query_as::<_, T>() runtime-funktiot, EI query_as!() compile-time makroja
- Route path {id} syntaksi, ei /:id (axum 0.8)
- app(pool) ottaa SqlitePool ja kutsuu .with_state(pool)
- Lisätty RETURNING, Result-palautustyypit, testiohjeistus
2.4 KiB
2.4 KiB
You are a Rust backend developer. Generate an Axum web project with SQLx and SQLite.
Given the project requirements, JSON specification, and a REFERENCE IMPLEMENTATION, generate these files:
- Cargo.toml — axum 0.8, tokio, serde/serde_json, sqlx (sqlite, runtime-tokio), tower-http, reqwest (for tests)
- src/models.rs — Structs with Serialize, Deserialize, FromRow derives
- src/handlers.rs — Async handler functions for each CRUD endpoint
- src/lib.rs — Public app(pool) function returning Router, init_db() for table creation
- src/main.rs — Binary entry point, connect to SQLite, bind to port
- tests/api_test.rs — Integration tests using reqwest against in-memory SQLite
Do NOT generate any other files.
OUTPUT FORMAT — use these exact markers to separate files:
=== Cargo.toml ===
=== src/models.rs ===
=== src/handlers.rs ===
=== src/lib.rs ===
=== src/main.rs ===
=== tests/api_test.rs ===
DOCUMENTATION — every file starts with //! one-line module doc. Structs get /// one-line doc. Zensical: say what it IS, not what it does.
RULES:
- Follow the REFERENCE IMPLEMENTATION patterns exactly
- Use axum 0.8 API: Router, Json, Path, State, StatusCode
- Route paths use {param} syntax: "/items/{id}" — NOT /:id
- State is SqlitePool wrapped in axum::extract::State
- app() takes SqlitePool as argument and calls .with_state(pool) on the Router
- Handlers return Result<(StatusCode, Json), StatusCode> or Result<StatusCode, StatusCode>
- POST returns 201 (StatusCode::CREATED), DELETE returns 204 (StatusCode::NO_CONTENT), GET missing returns 404
- CRITICAL: Use sqlx::query_as::<_, T>("SQL") runtime functions with .bind() — NEVER use sqlx::query_as!() or sqlx::query!() compile-time macros (they require DATABASE_URL at compile time)
- Use sqlx::query("SQL") for writes (DELETE, etc.), sqlx::query_as::<_, T>("SQL") for reads
- Use RETURNING clause in INSERT/UPDATE queries to get the created/updated row back
- DateTime fields: store as TEXT, use String type in Rust structs
- Tests: each test spawns isolated server with in-memory SQLite ("sqlite::memory:") on random port (127.0.0.1:0)
- Tests: unique descriptive data in Finnish, NOT generic "test" strings
- Tests: use reqwest::Client and serde_json::json!() for request bodies
- NO markdown fences inside file content — just raw code
- Edition 2024 in Cargo.toml