-
-
Notifications
You must be signed in to change notification settings - Fork 0
Comandos
Define comandos orientados al negocio en lugar de CRUD genérico. Los comandos traen el lenguaje del dominio a tu API y habilitan el patrón Command Query Responsibility Segregation (CQRS).
#[derive(Entity)] #[entity(table = "users", commands)] #[command(Register)] #[command(UpdateEmail: email)] #[command(Deactivate, requires_id)] pub struct User { #[id] pub id: Uuid, #[field(create, update, response)] pub email: String, #[field(create, response)] pub name: String, #[field(response)] pub active: bool, }
/// Payload de comando para operación Register en User. #[derive(Debug, Clone)] pub struct RegisterUser { pub email: String, pub name: String, } /// Payload de comando para operación UpdateEmail en User. #[derive(Debug, Clone)] pub struct UpdateEmailUser { pub id: Uuid, pub email: String, } /// Payload de comando para operación Deactivate en User. #[derive(Debug, Clone)] pub struct DeactivateUser { pub id: Uuid, }
/// Enum de comando para entidad User. #[derive(Debug, Clone)] pub enum UserCommand { Register(RegisterUser), UpdateEmail(UpdateEmailUser), Deactivate(DeactivateUser), } impl EntityCommand for UserCommand { fn kind(&self) -> CommandKind { match self { UserCommand::Register(_) => CommandKind::Create, UserCommand::UpdateEmail(_) => CommandKind::Update, UserCommand::Deactivate(_) => CommandKind::Custom, } } fn name(&self) -> &'static str { match self { UserCommand::Register(_) => "Register", UserCommand::UpdateEmail(_) => "UpdateEmail", UserCommand::Deactivate(_) => "Deactivate", } } }
/// Enum de resultado para ejecución de comando User. #[derive(Debug, Clone)] pub enum UserCommandResult { Register(User), UpdateEmail(User), Deactivate, }
/// Trait async para manejar comandos User. #[async_trait] pub trait UserCommandHandler: Send + Sync { type Error: std::error::Error + Send + Sync; type Context: Send + Sync; /// Despachar comando al handler apropiado. async fn handle(&self, cmd: UserCommand, ctx: &Self::Context) -> Result<UserCommandResult, Self::Error>; /// Manejar comando Register. async fn handle_register(&self, cmd: RegisterUser, ctx: &Self::Context) -> Result<User, Self::Error>; /// Manejar comando UpdateEmail. async fn handle_update_email(&self, cmd: UpdateEmailUser, ctx: &Self::Context) -> Result<User, Self::Error>; /// Manejar comando Deactivate. async fn handle_deactivate(&self, cmd: DeactivateUser, ctx: &Self::Context) -> Result<(), Self::Error>; }
Usa todos los campos #[field(create)]:
#[command(Register)] // Generado: RegisterUser { email, name }
Usa solo campos especificados (añade requires_id automáticamente):
#[command(UpdateEmail: email)] // Generado: UpdateEmailUser { id, email } #[command(UpdateProfile: name, bio, avatar)] // Generado: UpdateProfileUser { id, name, bio, avatar }
Un comando con sets(...) escribe columnas nombradas directamente, sin que tengan que ser #[field(update)], de modo que no aparecen ni en el DTO de parche público ni en la lista SET del upsert:
#[command(VerifyPassport, payload(passport_provider), sets( passport_verified = "true", passport_verified_at = "NOW()" ))] // Genera: VerifyPassportUser { id, passport_provider } // pool.verify_passport(command) -> User
Un solo UPDATE escribe las expresiones fijas más las columnas del payload y nada más. Las expresiones llegan a la sentencia tal cual, como en #[column(default = "...")]; los nombres de columna se verifican contra la entidad en compilación.
Con transactions activo, la misma operación está en el adaptador y se confirma junto con las demás escrituras; allí una fila ausente devuelve Ok(None), como en los otros métodos del adaptador.
let mut tx = pool.begin().await?; let verified = UserTransactionRepo::new(&mut tx) .verify_passport(VerifyPassportUser { id, passport_provider: Some("gov".into()) }) .await?; tx.commit().await?;
Añade solo el campo ID:
#[command(Deactivate, requires_id)] // Generado: DeactivateUser { id } #[command(Delete, requires_id, kind = "delete")] // Generado: DeleteUser { id }, devuelve ()
Usa una estructura externa:
pub struct TransferPayload { pub from_account: Uuid, pub to_account: Uuid, pub amount: i64, } #[command(Transfer, payload = "TransferPayload")] // Usa TransferPayload directamente
Usa un tipo de resultado personalizado:
pub struct TransferResult { pub transaction_id: Uuid, pub success: bool, } #[command(Transfer, payload = "TransferPayload", result = "TransferResult")] // Devuelve TransferResult en lugar de entidad
Controla qué campos se usan:
#[command(Create, source = "create")] // Usa campos #[field(create)] (defecto) #[command(Modify, source = "update")] // Usa campos #[field(update)] (opcional) #[command(Ping, source = "none")] // Sin campos de payload
Afectan la inferencia del tipo de resultado:
#[command(Create, kind = "create")] // Devuelve entidad (defecto) #[command(Update, kind = "update")] // Devuelve entidad #[command(Remove, kind = "delete")] // Devuelve () #[command(Process, kind = "custom")] // Inferido de source
use async_trait::async_trait; struct UserHandler { pool: PgPool, email_service: EmailService, } struct RequestContext { user_id: Option<Uuid>, correlation_id: Uuid, } #[async_trait] impl UserCommandHandler for UserHandler { type Error = AppError; type Context = RequestContext; async fn handle(&self, cmd: UserCommand, ctx: &Self::Context) -> Result<UserCommandResult, Self::Error> { match cmd { UserCommand::Register(c) => { let user = self.handle_register(c, ctx).await?; Ok(UserCommandResult::Register(user)) } UserCommand::UpdateEmail(c) => { let user = self.handle_update_email(c, ctx).await?; Ok(UserCommandResult::UpdateEmail(user)) } UserCommand::Deactivate(c) => { self.handle_deactivate(c, ctx).await?; Ok(UserCommandResult::Deactivate) } } } async fn handle_register(&self, cmd: RegisterUser, ctx: &Self::Context) -> Result<User, Self::Error> { // Validar if cmd.email.is_empty() { return Err(AppError::Validation("Email requerido".into())); } // Crear usuario let user = User { id: Uuid::now_v7(), email: cmd.email.to_lowercase(), name: cmd.name, active: true, }; // Persistir sqlx::query( "INSERT INTO users (id, email, name, active) VALUES (1,ドル 2,ドル 3,ドル 4ドル)" ) .bind(user.id) .bind(&user.email) .bind(&user.name) .bind(user.active) .execute(&self.pool) .await?; // Efectos secundarios self.email_service.send_welcome(&user.email).await?; Ok(user) } async fn handle_update_email(&self, cmd: UpdateEmailUser, ctx: &Self::Context) -> Result<User, Self::Error> { // Verificar autorización if ctx.user_id != Some(cmd.id) { return Err(AppError::Forbidden("No puede actualizar email de otro usuario".into())); } // Actualizar let user: User = sqlx::query_as( "UPDATE users SET email = 1ドル WHERE id = 2ドル RETURNING *" ) .bind(&cmd.email.to_lowercase()) .bind(cmd.id) .fetch_one(&self.pool) .await?; // Enviar verificación self.email_service.send_verification(&user.email).await?; Ok(user) } async fn handle_deactivate(&self, cmd: DeactivateUser, ctx: &Self::Context) -> Result<(), Self::Error> { sqlx::query("UPDATE users SET active = false WHERE id = 1ドル") .bind(cmd.id) .execute(&self.pool) .await?; Ok(()) } }
async fn register_user( handler: &impl UserCommandHandler, email: String, name: String, ) -> Result<User, AppError> { let cmd = RegisterUser { email, name }; let ctx = RequestContext { user_id: None, correlation_id: Uuid::new_v4(), }; match handler.handle(UserCommand::Register(cmd), &ctx).await? { UserCommandResult::Register(user) => Ok(user), _ => unreachable!(), } } // O llamar handler específico directamente async fn update_email( handler: &impl UserCommandHandler, user_id: Uuid, new_email: String, ctx: &RequestContext, ) -> Result<User, AppError> { let cmd = UpdateEmailUser { id: user_id, email: new_email, }; handler.handle_update_email(cmd, ctx).await }
Todos los enums de comando implementan el trait EntityCommand:
use entity_derive::{EntityCommand, CommandKind}; let cmd = UserCommand::Register(register_data); // Obtener metadatos del comando assert_eq!(cmd.name(), "Register"); assert!(matches!(cmd.kind(), CommandKind::Create)); // Pattern matching match cmd.kind() { CommandKind::Create => println!("Creando entidad"), CommandKind::Update => println!("Actualizando entidad"), CommandKind::Delete => println!("Eliminando entidad"), CommandKind::Custom => println!("Operación personalizada"), }
Cuando commands y hooks están habilitados:
#[derive(Entity)] #[entity(table = "orders", commands, hooks)] #[command(Place)] #[command(Cancel, requires_id)] pub struct Order { /* ... */ }
Hooks generados:
#[async_trait] pub trait OrderHooks: Send + Sync { type Error: std::error::Error + Send + Sync; // Hooks CRUD estándar... // Hooks específicos de comandos async fn before_command(&self, cmd: &OrderCommand) -> Result<(), Self::Error>; async fn after_command(&self, cmd: &OrderCommand, result: &OrderCommandResult) -> Result<(), Self::Error>; }
-
Lenguaje de dominio — Usa términos de negocio:
RegisterUsernoCreateUser - Responsabilidad única — Un comando = una operación de negocio
- Intención explícita — Los nombres de comandos deben describir la acción
- Validación en handlers — Mantén la lógica de validación en los handlers
- Idempotente cuando sea posible — Diseña comandos para reintentos seguros
- Usa contexto — Pasa metadatos de request (usuario, ID de correlación) vía contexto
Los comandos son una mitad de CQRS. Combina con proyecciones para el lado de consultas:
#[derive(Entity)] #[entity(table = "orders", commands)] #[projection(Summary: id, status, total_cents, created_at)] #[projection(Details: id, status, items, shipping_address, total_cents)] #[command(Place)] #[command(Ship, requires_id)] #[command(Cancel, requires_id)] pub struct Order { /* ... */ } // Comandos (lado de escritura) let result = handler.handle(OrderCommand::Place(place_order), &ctx).await?; // Consultas (lado de lectura) let summary = repo.find_by_id_summary(order_id).await?; let details = repo.find_by_id_details(order_id).await?;
🇬🇧 English | 🇷🇺 Русский | 🇰🇷 한국어 | 🇪🇸 Español | 🇨🇳 中文
Getting Started
Features
Advanced
Начало работы
Возможности
Продвинутое
시작하기
기능
고급
Comenzando
Características
Avanzado
入门
功能
高级