SQLx-D1 realizes "SQLx for Cloudflare D1" with compile-time SQL verification in Rust Cloudflare development !
Background
Miniflare's local D1 emulator is, essentially, just an .sqlite file.
This fact has been brought a lot of Rustaceans trying sqlx with sqlite feature for D1, but it's impossible because sqlx-sqlite contains a native dependency of SQLite driver.
SQLx-D1 works around this by loading sqlx-sqlite only in macro context and just providing a conversion layer between D1 and SQLx in library context.
Features
- SQLx interface for Cloudflare D1
- Batteries included,
sqlxis not needed in dependencies - Compile-time SQL verification
- by
sqlx-sqliteand miniflare's local D1 emulator - by query caches in
.sqlxdirectory ( offline mode )
- by
- No environment variable or
.envfile is needed- D1 emulator's location is fixed to
.wrangler/state/v3/d1/miniflare-D1DatabaseObject - falling back to offline mode when it doesn't exist and
.sqlxdirectory exists
- D1 emulator's location is fixed to
Unsupported features
- Transaction
- Let's wait for Cloudflare's side to support transation on D1 !
- Connection pool (
sqlx::Poolinternally requires Rust async runtime (tokio / asycn-std) and time implemetation of WASM runtime which is not done on Cloudflare Workers )- alternatively,
&sqlx_d1::D1ConnectionimplementsExecutor, not only&mutone.
- alternatively,
- derive
Type,Encode,Decode- maybe added if requested
- workaround if needed: add
sqlxto dependencies and use its ones
Note
So, make sure to use sqlx_d1 where target is set to wasm32-unknown-unknown.
In non-wasm32-unknown-unknown environment, for example, D1Connection doesn't provide new or impl Executor for &_.
Typically, place follwing .cargo/config.toml at project/workspace root :
[]
= "wasm32-unknown-unknown"
Example
# .cargo/config.toml
[]
= "wasm32-unknown-unknown"
# Cargo.toml
[]
= { = "0.1", = ["macros"] }
= { = "0.5", = ["d1"] }
= { = "1.0", = ["derive"] }
# wrangler.toml
[[]]
= "DB"
= "..."
= "..."
// src/lib.rs
async
LICENSE
SQLx-D1 is licensed under MIT LICENSE ( LICENSE or https://opensource.org/licenses/MIT ) .