| Crates.io | sqlx-d1-core |
| lib.rs | sqlx-d1-core |
| version | 0.2.1 |
| created_at | 2025-03-01 02:14:35.766739+00 |
| updated_at | 2025-09-12 10:15:41.495151+00 |
| description | core implementation for sqlx-d1 - SQLx for Cloudflare D1 |
| homepage | https://crates.io/crates/sqlx-d1 |
| repository | https://github.com/ohkami-rs/sqlx-d1 |
| max_upload_size | |
| id | 1573423 |
| size | 118,181 |
SQLx-D1 realizes "SQLx for Cloudflare D1" with compile-time SQL verification in Rust Cloudflare development !
worker version: 0.6
Miniflare's local D1 emulator is, essentially, just a .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 them by loading sqlx-sqlite only in macro context and just providing a conversion layer between D1 and SQLx in library context.
sqlx is not needed in dependenciessqlx-sqlite ).sqlx directory ( offline mode; cargo sqlx prepare ).env file is needed
.wrangler/state/v3/d1/miniflare-D1DatabaseObject.sqlx directory existssqlx::Pool internally requires Rust async runtime (tokio / asycn-std) and time implemetation of WASM runtime which is not done on Cloudflare Workers )
&sqlx_d1::D1Connection implements Executor, not only &mut one.Type, Encode, Decode
sqlx to dependencies and use its ones# Cargo.toml
[dependencies]
sqlx-d1 = { version = "0.2", features = ["macros"] }
worker = { version = "0.6", features = ["d1"] }
serde = { version = "1.0", features = ["derive"] }
wrangler d1 create <DATABASE_NAME> # prints <DATABASE_ID>
# wrangler.toml
[[d1_databases]]
binding = "DB"
database_name = "<DATABASE_NAME>"
database_id = "<DATABASE_ID>"
wrangler d1 migrations create DB 'schema'
-- migrations/0001_schema.sql
CREATE TABLE users (
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
);
wrangler d1 migrations apply DB --local
// src/lib.rs
#[worker::event(fetch)]
async fn main(
mut req: worker::Request,
env: worker::Env,
_ctx: worker::Context,
) -> worker::Result<worker::Response> {
let d1 = env.d1("DB")?;
let conn = sqlx_d1::D1Connection::new(d1);
#[derive(serde::Deserialize)]
struct CreateUser {
name: String,
age: Option<u8>,
}
let req = req.json::<CreateUser>().await?;
let id = sqlx_d1::query!(
"
INSERT INTO users (name, age) VALUES (?, ?)
RETURNING id
",
req.name,
req.age
)
.fetch_one(&conn)
.await
.map_err(|e| worker::Error::RustError(e.to_string()))?
.id;
worker::Response::ok(format!("Your id is {id}!"))
}
SQLx-D1 is licensed under MIT LICENSE ( LICENSE or https://opensource.org/licenses/MIT ) .