From 33f0cfaa7728444f30fe2eca144a01f1ec7c579d Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Thu, 30 Sep 2021 18:27:05 +0800 Subject: [PATCH] Readme --- README.md | 25 ++++++++++++++++++++++++ src/lib.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/README.md b/README.md index 2285c77c..28b9747c 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,31 @@ async fn list( ## A quick taste of SeaORM +### Entity +```rust +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "cake")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub name: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::fruit::Entity")] + Fruit, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Fruit.def() + } +} +``` + ### Select ```rust // find all models diff --git a/src/lib.rs b/src/lib.rs index a1dc83f7..e4897d13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -203,6 +203,63 @@ //! //! ## A quick taste of SeaORM //! +//! ### Entity +//! ``` +//! # #[cfg(feature = "macros")] +//! # mod entities { +//! # mod fruit { +//! # use sea_orm::entity::prelude::*; +//! # #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +//! # #[sea_orm(table_name = "fruit")] +//! # pub struct Model { +//! # #[sea_orm(primary_key)] +//! # pub id: i32, +//! # pub name: String, +//! # pub cake_id: Option, +//! # } +//! # #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +//! # pub enum Relation { +//! # #[sea_orm( +//! # belongs_to = "super::cake::Entity", +//! # from = "Column::CakeId", +//! # to = "super::cake::Column::Id" +//! # )] +//! # Cake, +//! # } +//! # impl Related for Entity { +//! # fn to() -> RelationDef { +//! # Relation::Cake.def() +//! # } +//! # } +//! # impl ActiveModelBehavior for ActiveModel {} +//! # } +//! # mod cake { +//! use sea_orm::entity::prelude::*; +//! +//! #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +//! #[sea_orm(table_name = "cake")] +//! pub struct Model { +//! #[sea_orm(primary_key)] +//! pub id: i32, +//! pub name: String, +//! } +//! +//! #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +//! pub enum Relation { +//! #[sea_orm(has_many = "super::fruit::Entity")] +//! Fruit, +//! } +//! +//! impl Related for Entity { +//! fn to() -> RelationDef { +//! Relation::Fruit.def() +//! } +//! } +//! # impl ActiveModelBehavior for ActiveModel {} +//! # } +//! # } +//! ``` +//! //! ### Select //! ``` //! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};