sqlx-cloudflare-d1
A Cloudflare D1 driver for sqlx: a real
sqlx::Databaseover the Workers D1 binding, so code written against sqlx's runtime API runs inside a Rust Worker.
[!NOTE] Pre-release. Not on crates.io yet, and the API may still move.
use ;
async
Install
[]
= "0.1"
= { = "0.9", = false, = ["derive"] }
= { = "0.8", = ["d1"] }
| sqlx-cloudflare-d1 | sqlx | worker | Rust |
|---|---|---|---|
| 0.1 | 0.9 | 0.8 | 1.94+ |
The crate runs on wasm32-unknown-unknown, inside a Worker.
Limitations
- No
query!/query_as!macros. sqlx's macros only know its built-in drivers. The runtime API (sqlx::query,query_as,FromRow) works. - No transactions. D1 cannot hold one open across calls, so
begin()fails. UseD1Connection::execute_batch, which runs statements atomically in one round trip. - Rows in a batch are keyed by name.
D1Connection::fetch_batchstreams each statement's rows and result -- the shape of sqlc's:batch*queries. D1'sbatch()returns rows as objects, so two columns with one name collapse to the last. Alias them apart inside a batch. - No
sqlx::Pool. Nothing to pool: build a connection per request from the binding. - Integers are limited to ±(2^53 − 1). D1 passes values as JavaScript
numbers, so a wider
i64is refused rather than silently rounded. Note that sqlx'sQueryBuilder::push_bindpanics on a refused value instead of returning the error. - No
sqlx::migrate!. Apply migrations withwrangler d1 migrations. - No derived TEXT enums.
#[derive(sqlx::Type)]works for#[sqlx(transparent)]newtypes and#[repr(i32)]enums, but sqlx only generates string-backed enums for its built-in drivers. - No statement logging.
log_statementsis accepted and ignored. - Results are not streamed. D1 returns a whole result set at once.