Skip to content

Navigation Menu

Sign in
Sign up
RAprogramm edited this page Jul 4, 2026 · 3 revisions

常见用例的实用示例。

用户管理

带身份验证字段的经典用户实体:

use entity_derive::Entity;
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[derive(Entity)]
#[entity(table = "users", schema = "auth")]
pub struct User {
 #[id]
 pub id: Uuid,
 #[field(create, update, response)]
 pub username: String,
 #[field(create, update, response)]
 pub email: String,
 #[field(create, update)] // 接受但从不返回
 pub password_hash: String,
 #[field(response)]
 pub email_verified: bool,
 #[field(update, response)]
 pub avatar_url: Option<String>,
 #[field(response)]
 pub role: String,
 #[field(skip)] // 内部审计字段
 pub last_login_ip: Option<String>,
 #[auto]
 #[field(response)]
 pub created_at: DateTime<Utc>,
 #[auto]
 #[field(response)]
 pub updated_at: DateTime<Utc>,
}

用法:

// 创建用户
let request = CreateUserRequest {
 username: "john_doe".into(),
 email: "john@example.com".into(),
 password_hash: hash_password("secret123"),
};
let user = pool.create(request).await?;
// 更新用户
let update = UpdateUserRequest {
 avatar_url: Some(Some("https://cdn.example.com/avatar.jpg".into())),
 ..Default::default()
};
let user = pool.update(user.id, update).await?;
// 响应是安全的 - 没有 password_hash,没有 last_login_ip
let response = UserResponse::from(&user);

博客系统

带作者关系的文章:

#[derive(Entity)]
#[entity(table = "posts", schema = "blog")]
pub struct Post {
 #[id]
 pub id: Uuid,
 #[field(create, update, response)]
 pub title: String,
 #[field(create, update, response)]
 pub slug: String,
 #[field(create, update, response)]
 pub content: String,
 #[field(create, update, response)]
 pub excerpt: Option<String>,
 #[field(create, response)] // 只设置一次,不能更改作者
 pub author_id: Uuid,
 #[field(update, response)]
 pub published: bool,
 #[field(update, response)]
 pub published_at: Option<DateTime<Utc>>,
 #[field(response)] // 只读,由触发器管理
 pub view_count: i64,
 #[field(skip)] // 内部审核
 pub moderation_status: String,
 #[auto]
 #[field(response)]
 pub created_at: DateTime<Utc>,
 #[auto]
 #[field(response)]
 pub updated_at: DateTime<Utc>,
}

多对多分类:

#[derive(Entity)]
#[entity(table = "categories", schema = "blog")]
pub struct Category {
 #[id]
 pub id: Uuid,
 #[field(create, update, response)]
 pub name: String,
 #[field(create, update, response)]
 pub slug: String,
 #[field(create, update, response)]
 pub description: Option<String>,
 #[field(response)]
 pub post_count: i64, // 计算字段
 #[auto]
 #[field(response)]
 pub created_at: DateTime<Utc>,
}

电商系统

产品目录:

#[derive(Entity)]
#[entity(table = "products", schema = "catalog")]
pub struct Product {
 #[id]
 pub id: Uuid,
 #[field(create, update, response)]
 pub name: String,
 #[field(create, update, response)]
 pub sku: String,
 #[field(create, update, response)]
 pub description: Option<String>,
 #[field(create, update, response)]
 pub price_cents: i64,
 #[field(create, update, response)]
 pub currency: String,
 #[field(update, response)]
 pub stock_quantity: i32,
 #[field(update, response)]
 pub is_active: bool,
 #[field(create, response)]
 pub category_id: Uuid,
 #[field(skip)] // 内部成本跟踪
 pub cost_cents: i64,
 #[field(skip)] // 供应商信息
 pub supplier_id: Option<Uuid>,
 #[auto]
 #[field(response)]
 pub created_at: DateTime<Utc>,
 #[auto]
 #[field(response)]
 pub updated_at: DateTime<Utc>,
}

带状态跟踪的订单:

#[derive(Entity)]
#[entity(table = "orders", schema = "sales")]
pub struct Order {
 #[id]
 pub id: Uuid,
 #[field(create, response)]
 pub customer_id: Uuid,
 #[field(response)]
 pub order_number: String, // 由数据库序列生成
 #[field(update, response)]
 pub status: String,
 #[field(create, response)]
 pub total_cents: i64,
 #[field(create, response)]
 pub currency: String,
 #[field(create, update, response)]
 pub shipping_address: String,
 #[field(update, response)]
 pub tracking_number: Option<String>,
 #[field(skip)] // 支付处理器数据
 pub payment_intent_id: Option<String>,
 #[field(skip)] // 内部备注
 pub admin_notes: Option<String>,
 #[auto]
 #[field(response)]
 pub created_at: DateTime<Utc>,
 #[auto]
 #[field(response)]
 pub updated_at: DateTime<Utc>,
}

多租户SaaS

组织范围内的实体:

#[derive(Entity)]
#[entity(table = "organizations", schema = "tenants")]
pub struct Organization {
 #[id]
 pub id: Uuid,
 #[field(create, update, response)]
 pub name: String,
 #[field(create, response)]
 pub slug: String, // 创建后不可变
 #[field(update, response)]
 pub plan: String,
 #[field(response)]
 pub member_count: i32,
 #[field(skip)] // 账单信息
 pub stripe_customer_id: Option<String>,
 #[auto]
 #[field(response)]
 pub created_at: DateTime<Utc>,
}
#[derive(Entity)]
#[entity(table = "projects", schema = "tenants")]
pub struct Project {
 #[id]
 pub id: Uuid,
 #[field(create, response)] // 只设置一次
 pub organization_id: Uuid,
 #[field(create, update, response)]
 pub name: String,
 #[field(create, update, response)]
 pub description: Option<String>,
 #[field(update, response)]
 pub archived: bool,
 #[auto]
 #[field(response)]
 pub created_at: DateTime<Utc>,
 #[auto]
 #[field(response)]
 pub updated_at: DateTime<Utc>,
}

仅DTO(无数据库)

用于没有持久化的API契约:

#[derive(Entity)]
#[entity(table = "webhooks", sql = "none")]
pub struct WebhookPayload {
 #[id]
 pub id: Uuid,
 #[field(create, response)]
 pub event_type: String,
 #[field(create, response)]
 pub payload: String,
 #[field(create, response)]
 pub timestamp: DateTime<Utc>,
 #[field(response)]
 pub signature: String,
}

这只生成:

  • CreateWebhookPayloadRequest
  • WebhookPayloadResponse
  • From 实现

没有repository,没有SQL,没有Row/Insertable结构体。

仅自定义查询

当标准CRUD不够用时:

#[derive(Entity)]
#[entity(table = "analytics_events", schema = "analytics", sql = "trait")]
pub struct AnalyticsEvent {
 #[id]
 pub id: Uuid,
 #[field(create, response)]
 pub event_name: String,
 #[field(create, response)]
 pub user_id: Option<Uuid>,
 #[field(create, response)]
 pub properties: serde_json::Value,
 #[auto]
 #[field(response)]
 pub created_at: DateTime<Utc>,
}
// 自己实现自定义查询:
impl AnalyticsEventRepository for PgPool {
 type Error = sqlx::Error;
 async fn create(&self, dto: CreateAnalyticsEventRequest) -> Result<AnalyticsEvent, Self::Error> {
 // 批量插入、分区表等
 }
 async fn find_by_id(&self, id: Uuid) -> Result<Option<AnalyticsEvent>, Self::Error> {
 // 带时间分区的查询
 }
 // 超出CRUD的自定义方法:
 async fn aggregate_by_event(&self, start: DateTime<Utc>, end: DateTime<Utc>)
 -> Result<Vec<EventAggregate>, Self::Error> {
 // 复杂聚合查询
 }
}

另见

批量操作

每个仓库都提供批量原语:find_by_ids(WHERE id = ANY(1ドル),单次往返)、原子的 create_many(单个事务,任一行失败则整批回滚)以及感知软删除的 delete_many(返回实际受影响的行数)。启用事件投递(streams 或 outbox)时,会在同一事务内按行发出事件。

let posts: Vec<Post> = pool.find_by_ids(ids).await?;
let created: Vec<Post> = pool.create_many(dtos).await?;
let removed: u64 = pool.delete_many(stale_ids).await?;

🌐 Language

🇬🇧 English | 🇷🇺 Русский | 🇰🇷 한국어 | 🇪🇸 Español | 🇨🇳 中文


🇬🇧 English

Home

Getting Started

Features

Advanced


🇷🇺 Русский

Главная

Начало работы

Возможности

Продвинутое


🇰🇷 한국어

시작하기

기능

고급


🇪🇸 Español

Inicio

Comenzando

Características

Avanzado


🇨🇳 中文

首页

入门

功能

高级


📚 Reference

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /