-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand SeaORM entity generator with Seaography related data (#1599)
* Add DeriveRelatedEntity macro * Add generation for related enum and seaography * Add seaography cli param * update codegen tests * Fix DeriveRelatedEntity macro doc and includes * Fix all RelatedEntity variants for RelationBuilder * Add tests for code * Cargo format * Fix clippy code * Fix format * Fix unit tests * Fix unit tests * Provide default for seaography::RelationBuilder * Update changelog * Update tests * Modify code to match feedback * Bring old Related Impl trait generation * Modify DeriveRelatedEntity to gen impl seaography::RelationBuilder * Generate RelatedEntity enum when seaography flag is enabled * Update documentation * Update Changelog * Fix format errors * Fix code generation * relations with suffix are definition based * Rev => Reverse easier to read * snake_case to cameCase for name generation * Fix unit tests * Update lib.rs * derive `seaography::RelationBuilder` only when `seaography` feature is enabled * Try constructing async-graphql root for "related entity" and "entity" without relation * Update demo * CHANGELOG * Update Cargo.toml Co-authored-by: Chris Tsang <[email protected]> * Revert "Update Cargo.toml" This reverts commit 6b16698. --------- Co-authored-by: Billy Chan <[email protected]> Co-authored-by: Chris Tsang <[email protected]>
- Loading branch information
1 parent
fd6c303
commit 3300336
Showing
27 changed files
with
1,060 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[workspace] | ||
members = ["entity", "graphql"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "entity" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
name = "entity" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
sea-orm = { path = "../../../" } | ||
seaography = { path = "../../../../seaography", optional = true } | ||
async-graphql = { version = "5", optional = true } | ||
|
||
[features] | ||
seaography = ["dep:seaography", "async-graphql", "sea-orm/seaography"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "cake")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
#[sea_orm(column_name = "name", enum_name = "Name")] | ||
pub name: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm(has_many = "super::fruit::Entity")] | ||
Fruit, | ||
#[sea_orm( | ||
has_many = "super::fruit::Entity", | ||
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"# | ||
)] | ||
TropicalFruit, | ||
#[sea_orm( | ||
has_many = "super::fruit::Entity", | ||
condition_type = "any", | ||
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"# | ||
)] | ||
OrTropicalFruit, | ||
} | ||
|
||
impl Related<super::fruit::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Fruit.def() | ||
} | ||
} | ||
|
||
impl Related<super::filling::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
super::cake_filling::Relation::Filling.def() | ||
} | ||
|
||
fn via() -> Option<RelationDef> { | ||
Some(super::cake_filling::Relation::Cake.def().rev()) | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)] | ||
pub enum RelatedEntity { | ||
#[sea_orm(entity = "super::fruit::Entity")] | ||
Fruit, | ||
#[sea_orm(entity = "super::filling::Entity")] | ||
Filling, | ||
#[sea_orm(entity = "super::fruit::Entity", def = "Relation::TropicalFruit.def()")] | ||
TropicalFruit, | ||
#[sea_orm( | ||
entity = "super::fruit::Entity", | ||
def = "Relation::OrTropicalFruit.def()" | ||
)] | ||
OrTropicalFruit, | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] | ||
pub struct Entity; | ||
|
||
impl EntityName for Entity { | ||
fn table_name(&self) -> &str { | ||
"cake_filling" | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] | ||
pub struct Model { | ||
pub cake_id: i32, | ||
pub filling_id: i32, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] | ||
pub enum Column { | ||
CakeId, | ||
FillingId, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] | ||
pub enum PrimaryKey { | ||
CakeId, | ||
FillingId, | ||
} | ||
|
||
impl PrimaryKeyTrait for PrimaryKey { | ||
type ValueType = (i32, i32); | ||
|
||
fn auto_increment() -> bool { | ||
false | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter)] | ||
pub enum Relation { | ||
Cake, | ||
Filling, | ||
} | ||
|
||
impl ColumnTrait for Column { | ||
type EntityName = Entity; | ||
|
||
fn def(&self) -> ColumnDef { | ||
match self { | ||
Self::CakeId => ColumnType::Integer.def(), | ||
Self::FillingId => ColumnType::Integer.def(), | ||
} | ||
} | ||
} | ||
|
||
impl RelationTrait for Relation { | ||
fn def(&self) -> RelationDef { | ||
match self { | ||
Self::Cake => Entity::belongs_to(super::cake::Entity) | ||
.from(Column::CakeId) | ||
.to(super::cake::Column::Id) | ||
.into(), | ||
Self::Filling => Entity::belongs_to(super::filling::Entity) | ||
.from(Column::FillingId) | ||
.to(super::filling::Column::Id) | ||
.into(), | ||
} | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] | ||
#[sea_orm(table_name = "filling")] | ||
pub struct Entity; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] | ||
pub struct Model { | ||
pub id: i32, | ||
pub name: String, | ||
pub vendor_id: Option<i32>, | ||
#[sea_orm(ignore)] | ||
pub ignored_attr: i32, | ||
} | ||
|
||
// If your column names are not in snake-case, derive `DeriveCustomColumn` here. | ||
#[derive(Copy, Clone, Debug, EnumIter, DeriveCustomColumn)] | ||
pub enum Column { | ||
Id, | ||
Name, | ||
VendorId, | ||
} | ||
|
||
// Then, customize each column names here. | ||
impl IdenStatic for Column { | ||
fn as_str(&self) -> &str { | ||
match self { | ||
// Override column names | ||
Self::Id => "id", | ||
// Leave all other columns using default snake-case values | ||
_ => self.default_as_str(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] | ||
pub enum PrimaryKey { | ||
Id, | ||
} | ||
|
||
impl PrimaryKeyTrait for PrimaryKey { | ||
type ValueType = i32; | ||
|
||
fn auto_increment() -> bool { | ||
true | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter)] | ||
pub enum Relation {} | ||
|
||
impl ColumnTrait for Column { | ||
type EntityName = Entity; | ||
|
||
fn def(&self) -> ColumnDef { | ||
match self { | ||
Self::Id => ColumnType::Integer.def(), | ||
Self::Name => ColumnType::String(None).def(), | ||
Self::VendorId => ColumnType::Integer.def().nullable(), | ||
} | ||
} | ||
} | ||
|
||
impl RelationTrait for Relation { | ||
fn def(&self) -> RelationDef { | ||
panic!("No RelationDef") | ||
} | ||
} | ||
|
||
impl Related<super::cake::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
super::cake_filling::Relation::Cake.def() | ||
} | ||
|
||
fn via() -> Option<RelationDef> { | ||
Some(super::cake_filling::Relation::Filling.def().rev()) | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "fruit")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
#[cfg_attr(feature = "with-json", serde(skip_deserializing))] | ||
pub id: i32, | ||
pub name: String, | ||
pub cake_id: Option<i32>, | ||
} | ||
|
||
#[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<super::cake::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Cake.def() | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod cake; | ||
pub mod cake_filling; | ||
pub mod filling; | ||
pub mod fruit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "graphql" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
poem = { version = "1.3.55" } | ||
async-graphql-poem = { version = "5.0.6" } | ||
async-graphql = { version = "5.0.6", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } | ||
async-trait = { version = "0.1.64" } | ||
dotenv = "0.15.0" | ||
tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread"] } | ||
tracing = { version = "0.1.37" } | ||
tracing-subscriber = { version = "0.3.16" } | ||
lazy_static = { version = "1.4.0" } | ||
|
||
sea-orm = { path = "../../../" } | ||
entity = { path = "../entity", features = ["seaography"] } | ||
seaography = { path = "../../../../seaography" } |
Oops, something went wrong.