d1-orm
Query builder and ORM for Cloudflare D1, written in Rust, targeting wasm32-unknown-unknown.
Works with workers-rs 0.4.
Install
[]
= "0.0.1"
Usage
Define a model
Implement D1Model on any Deserialize struct that maps to a D1 table:
use ;
use Deserialize;
use JsValue;
CRUD
use ;
let table = new;
// INSERT ... RETURNING *
let user = table.insert.await?;
// INSERT batch — single D1 batch() round trip
let users = table.insert_batch.await?;
// SELECT
let user = table.find_one.await?;
let users = table.find_all.await?;
// UPDATE ... RETURNING *
let updated = table.update.await?;
// DELETE
table.delete.await?;
// COUNT
let n = table.count.await?;
Query builder reference
| Method | SQL fragment |
|---|---|
.eq("col", val) |
col = ?N |
.ne("col", val) |
col != ?N |
.gt("col", val) |
col > ?N |
.gte("col", val) |
col >= ?N |
.lt("col", val) |
col < ?N |
.lte("col", val) |
col <= ?N |
.is_null("col") |
col IS NULL |
.is_not_null("col") |
col IS NOT NULL |
.filter_optional("col", opt) |
(?N IS NULL OR col = ?N) |
.filter_optional_gte("col", opt) |
(?N IS NULL OR col >= ?N) |
.filter_optional_lte("col", opt) |
(?N IS NULL OR col <= ?N) |
.order_by("col", Order::Desc) |
ORDER BY col DESC |
.limit(n) |
LIMIT n |
.offset(n) |
OFFSET n |
filter_optional* methods accept Option<T> — pass None to skip the filter (match all rows).
Set builder reference
| Method | Behavior |
|---|---|
.field("col", val) |
col = ?N with a non-null value |
.nullable_field("col", opt) |
col = ?N — binds NULL when opt is None |
Notes
- All writes use
RETURNING *— no second SELECT after insert/update. insert_batchuses D1'sbatch()API — all rows in one round trip.- JOINs are not supported by the query builder. Drop to raw D1 for those.
License
MIT