- Install the dependencies:
cargo add sqlx --features=runtime-tokio-native-tls,sqlite,macros,offline
- use tokio since axum is based on it
- native TLS openssl but is the same as rust-ls
- sqlite as the database engine
- macros for type safe queries
- offline mode for CI and test
chrono
(optional) for dates and times
- Install
sqlx-cli
for migrations, through nix
or cargo install
- Sqlite connection option:
- see the series
fly.io - sqlite internals wall
journal_mode=wall
let options = SqliteConnectOptions::from_str(db_url)?
.journal_mode(SqliteJournalMode::Wal);
let pool = SqlitePool::connect_with(options).await?;
- add it to the state of the application, pool is a value wrapped in an
Arc
so it’s safe to clone if needed and pass between threads.
let app = app().with_state(State(pool));