Drizzle RS
A type-safe SQL query builder for Rust inspired by Drizzle ORM.
Schema Setup
First, create a schema.rs file to define your database tables. All schema
items must be pub for proper destructuring:
use *;
use Uuid;
// Index definitions
;
;
UUID Storage Options
Choose between binary (BLOB) or string (TEXT) storage:
// Binary storage (16 bytes) - more efficient
pub id: Uuid,
// String storage (36 characters) - human readable
pub id: Uuid,
Indexes
Indexes are defined as separate structs and included in your schema. They
reference table columns using the Table::column syntax:
use *;
// Unique compound index on email and name
;
// Simple index on email column
;
The indexes are automatically created when you call db.create() and must be
included as fields in your schema struct.
Basic Usage
In your main.rs, use the schema without feature flags:
use *;
use Drizzle;
use Connection;
use Uuid;
use crate;
Insert Models
// Always use new() as it forces you at compile time to input required fields
new
.with_email // Optional fields or fields with defaults via .with_*
[!WARNING]
Avoid usingInsertUsers::default(), as it will fail at runtime if required fields are not provided.
The .values() method automatically batches inserts of the same type:
// Same insert model type - will batch
db.insert
.values
.execute?;
// compile time failure
db.insert
.values
.execute?;
Transactions
For multiple different operations or when you need ACID guarantees, use transactions:
use SQLiteTransactionType;
// sync drivers
db.transaction?;
// async drivers - api is wip as I think pinning here is gross.
db.transaction.await?;
For more details on transaction types, see the SQLite Transaction Documentation.
Table Attributes
// Basic table
// Custom table name
// SQLite STRICT mode
// WITHOUT ROWID table
// Combined
Field Attributes
// Column types
// INTEGER column
// TEXT column
// REAL column
// BLOB column
// Stored as INTEGER (0/1)
// Constraints
// Primary key
// Auto-incrementing primary key
// Unique constraint
// Text primary key
// Default values
// Compile-time default
// Compile-time default
// Runtime default function
// Special types
// Store enum as TEXT
// Store enum as INTEGER
// JSON serialized to TEXT
// JSON serialized to BLOB
// Foreign keys
// Foreign key reference
Nullability
Nullability is controlled by Rust's type system:
Enums
JSON Fields
FromRow Derive Macro
The FromRow derive macro automatically generates TryFrom<&Row>
implementations for converting database rows into your structs.
Basic Usage
use *;
// Query returns User structs directly
let users: = db.select.from.all?;
Column Mapping
Use the #[column] attribute to map struct fields to specific table columns:
// Use in SELECT queries with custom column mapping
// You can collect into your favorite container
let info: = db
.select
.from
.all?;
JOIN Queries
Perfect for handling JOIN query results:
let results: = db
.select
.from
.inner_join
.all?;
Tuple Structs
Also works with tuple structs for simple cases:
;
let names: = db
.select
.from
.all?;
The macro automatically handles:
- Type conversions between SQLite types and Rust types
- Optional fields (
Option<T>) - All supported column types (integers, text, blobs, JSON, enums)
SQLite PRAGMA Support
use ;
// Type-safe pragma statements
let pragma = foreign_keys;
// Execute with drizzle
db.execute?;