Skip to content

Commit

Permalink
Expand SeaORM entity generator with Seaography related data (#1599)
Browse files Browse the repository at this point in the history
* 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
3 people authored May 19, 2023
1 parent fd6c303 commit 3300336
Show file tree
Hide file tree
Showing 27 changed files with 1,060 additions and 4 deletions.
29 changes: 28 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,33 @@ assert_eq!(
```
* [sea-orm-cli] Added support for generating migration of space separated name, for example executing `sea-orm-cli migrate generate "create accounts table"` command will create `m20230503_000000_create_accounts_table.rs` for you https://github.com/SeaQL/sea-orm/pull/1570

* Add `seaography` flag to `sea-orm`, `sea-orm-orm-macros` and `sea-orm-cli` https://github.com/SeaQL/sea-orm/pull/1599
* Add generation of `seaography` related information to `sea-orm-codegen` https://github.com/SeaQL/sea-orm/pull/1599

The following information is added in entities files by `sea-orm-cli` when flag `seaography` is `true`
```rust
/// ... Entity File ...

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)]
pub enum RelatedEntity {
#[sea_orm(entity = "super::address::Entity")]
Address,
#[sea_orm(entity = "super::payment::Entity")]
Payment,
#[sea_orm(entity = "super::rental::Entity")]
Rental,
#[sea_orm(entity = "Entity", def = "Relation::SelfRef.def()")]
SelfRef,
#[sea_orm(entity = "super::store::Entity")]
Store,
#[sea_orm(entity = "Entity", def = "Relation::SelfRef.def().rev()")]
SelfRefRev,
}
```
* Add `DeriveEntityRelated` macro https://github.com/SeaQL/sea-orm/pull/1599

The DeriveRelatedEntity derive macro will implement `seaography::RelationBuilder` for `RelatedEntity` enumeration when the `seaography` feature is enabled

### Enhancements

* Added `Migration::name()` and `Migration::status()` getters for the name and status of `sea_orm_migration::Migration` https://github.com/SeaQL/sea-orm/pull/1519
Expand Down Expand Up @@ -457,7 +484,7 @@ impl ColumnTrait for Column {
### Breaking Changes

* [sea-orm-cli] Enable --universal-time by default https://github.com/SeaQL/sea-orm/pull/1420
* Added `RecordNotInserted` and `RecordNotUpdated` to `DbErr`
* Added `RecordNotInserted` and `RecordNotUpdated` to `DbErr`
* Added `ConnectionTrait::execute_unprepared` method https://github.com/SeaQL/sea-orm/pull/1327
* As part of https://github.com/SeaQL/sea-orm/pull/1311, the required method of `TryGetable` changed:
```rust
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ runtime-tokio-rustls = [
"runtime-tokio",
]
tests-cfg = ["serde/derive"]
seaography = ["sea-orm-macros/seaography"]
2 changes: 2 additions & 0 deletions issues/1599/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["entity", "graphql"]
17 changes: 17 additions & 0 deletions issues/1599/entity/Cargo.toml
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"]
60 changes: 60 additions & 0 deletions issues/1599/entity/src/cake.rs
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 {}
70 changes: 70 additions & 0 deletions issues/1599/entity/src/cake_filling.rs
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 {}
80 changes: 80 additions & 0 deletions issues/1599/entity/src/filling.rs
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 {}
29 changes: 29 additions & 0 deletions issues/1599/entity/src/fruit.rs
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 {}
4 changes: 4 additions & 0 deletions issues/1599/entity/src/lib.rs
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;
20 changes: 20 additions & 0 deletions issues/1599/graphql/Cargo.toml
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" }
Loading

0 comments on commit 3300336

Please sign in to comment.