Examples
A worked example of a small blog application. The schema is in
examples/blog/schema.ruprizzle in the repository.
Project layout
my-blog/
Cargo.toml
.env
schema.ruprizzle
src/
main.rs
db/ # generated by `ruprizzle generate`
mod.rs
...
schema.ruprizzle
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
generator client {
output = "src/db"
module_name = "db"
}
enum Role {
USER
ADMIN
}
model User {
id Uuid @id @default(uuid7())
email String @unique
name String?
role Role @default(USER)
posts Post[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@index([email])
@@map("users")
}
model Post {
id Uuid @id @default(uuid7())
title String @db.VarChar(200)
body String?
published Boolean @default(false)
authorId Uuid @map("author_id")
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now()) @map("created_at")
@@index([authorId, published])
@@map("posts")
}
Cargo.toml
[package]
name = "my-blog"
version = "0.1.0"
edition = "2024"
[dependencies]
ruprizzle = "1.6"
dotenvy = "0.15"
tokio = { version = "1", features = ["full"] }
# ruprizzle re-exports sqlx and serde, but the generated client uses sqlx
# derives directly, so list them in your own Cargo.toml.
sqlx = { version = "0.8.6", default-features = false, features = ["runtime-tokio-rustls", "postgres"] }
serde = "1.0.229"
If you are working from a local clone of the ruprizzle-orm repository, use
path = "../crates/runtime" for ruprizzle and add the project to the
workspace members list.
src/main.rs
mod db; #[tokio::main] async fn main() -> Result<(), ruprizzle::Error> { dotenvy::dotenv().ok(); let db_url = std::env::var("DATABASE_URL").map_err(|e| ruprizzle::Error::Message(e.to_string()))?; let db = db::Db::connect(&db_url).await?; // Create an author and a post. let author = db .user() .create(db::UserInsert { id: None, email: "alice@example.com".into(), name: Some("Alice".into()), role: Some(db::enums::Role::Admin), created_at: None, updated_at: None, }) .exec() .await?; let _post = db .post() .create(db::PostInsert { id: None, title: "Hello, ruprizzle".into(), body: Some("This is the first post.".into()), published: Some(true), author_id: author.id, created_at: None, }) .exec() .await?; // List published posts with their authors (one query per level). let posts = db .post() .find_many() .filter(db::post::PUBLISHED.eq(true)) .order_by(db::post::CREATED_AT.desc()) .include(db::post::author()) .limit(10) .exec() .await?; for post in &posts { let author_name = post .author .get() .as_ref() .and_then(|a| a.name.as_ref()) .map(|s| s.as_str()) .unwrap_or("unknown"); println!("{} by {}", post.title, author_name); } // Show the SQL for a query. let compiled = db .post() .find_many() .filter(db::post::PUBLISHED.eq(true)) .to_sql()?; println!("{}", compiled.sql); Ok(()) }
.env
DATABASE_URL="postgres://user:password@localhost:5432/my_blog?sslmode=disable"
Running the example
cd examples/blog
ruprizzle migrate dev --name init
cargo run
Variations
SQLite
Change provider to sqlite and set:
DATABASE_URL="sqlite://./blog.db"
MySQL / MariaDB
Change provider to mysql and set a MySQL URL:
DATABASE_URL="mysql://user:password@localhost:3306/my_blog"