Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand SeaORM entity generator with Seaography related data #1599

Merged
merged 31 commits into from
May 19, 2023

Conversation

karatakis
Copy link
Contributor

@karatakis karatakis commented Apr 15, 2023

PR Info

New Features

  • Add seaography flag to sea-orm-cli
  • Add generation of seaography related information to sea-orm-codegen
  • Add DeriveEntityRelated macro
  • Add generation of code that uses DeriveEntityRelated to sea-orm-codegen

@karatakis karatakis marked this pull request as ready for review April 15, 2023 11:49
Comment on lines 20 to 25
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", to = "Relation::Fruit.def()")]
Fruit,
#[sea_orm (entity = "super::filling::Entity", to = "super::cake_filling::Relation::Filling.def()", via = "Some(super::cake_filling::Relation::Cake.def().rev())")]
Filling
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep generating the plain old Related<T>. With that in mind, if user pass in a --seaography flag to the sea-orm-cli generate entity, then, an additional RelatedEntity enum will be generated.

I imagine the derive attribute of each variant in the RelatedEntity could be... to take advantage of the existing Related<T> impl and avoid saying the same thing twice.

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)]
pub enum RelatedEntity {
    #[sea_orm (entity = "super::filling::Entity")]
    Filling
}

Comment on lines 30 to 49
impl seaography::RelationBuilder for Relation {
fn get_relation(&self, context: & 'static seaography::BuilderContext) -> async_graphql::dynamic::Field {
let builder = seaography::EntityObjectRelationBuilder { context };
match self {
Self::Fruit => builder.get_relation:: <Entity, super::fruit::Entity>("fruit", Self::Fruit.def()),
_ => panic!("No relations for this entity")
}
}
}

impl seaography::RelationBuilder for RelatedEntity {
fn get_relation(&self, context: & 'static seaography::BuilderContext) -> async_graphql::dynamic::Field {
let builder = seaography::EntityObjectViaRelationBuilder { context };
match self {
Self::Fruit => builder.get_relation:: <Entity, super::fruit::Entity>("fruit"),
Self::Filling => builder.get_relation:: <Entity, super::filling::Entity>("filling"),
_ => panic!("No relations for this entity")
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These async-graphql constructors for relations can be moved into sea-orm-macros in which those methods will be generated when the seaography feature is enabled

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @karatakis, thanks for the huge PR! Great work! I just leave a few comments :)

* Bring old Related Impl trait generation
* Modify DeriveRelatedEntity to gen impl seaography::RelationBuilder
* Generate RelatedEntity enum when seaography flag is enabled
@billy1624
Copy link
Member

Hey @karatakis, thanks for the changes!! Please feel free to ping me once it's finished or you need help :P

@karatakis
Copy link
Contributor Author

I am done.

@billy1624 could you please take a look if the last changes resolve the issues you pointed out ?

* relations with suffix are definition based
* Rev => Reverse easier to read
* snake_case to cameCase for name generation
@karatakis karatakis mentioned this pull request Apr 18, 2023
3 tasks
@karatakis
Copy link
Contributor Author

I used the proposed changes in the following SeaQL/seaography#131
Its still WIP but you can see how I am planning to use it :)

@tyt2y3
Copy link
Member

tyt2y3 commented Apr 18, 2023

Great work. This is really significant. We will try to roll out SeaORM 0.12 sooner and I think this is really a 'killer app'

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @karatakis, look good!! Neat and clean!

Can we update the Cargo.toml in SeaQL/seaography#131 and make it depends on this PR. I want to compile, see it in action and play with it :)

Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can arrange a merge @billy1624 if there is no other outstanding PR to sea-orm-cli
We want to be extra careful not to breaking any existing features though.

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @karatakis, I just added two commits

  1. Derive impl seaography::RelationBuilder for RelatedEntity when seaography feature is enabled 0833478

  2. A demo for how to construct the async-graphql query root for entity with/without RelatedEntity c84294e

Please check :)

@billy1624 billy1624 requested review from tyt2y3 and billy1624 May 18, 2023 12:40
@tyt2y3
Copy link
Member

tyt2y3 commented May 19, 2023

The seaography feature in the root Cargo.toml needs a '?' for macros

Cargo.toml Show resolved Hide resolved
Co-authored-by: Chris Tsang <[email protected]>
@billy1624
Copy link
Member

Indeed! Thanks @tyt2y3 for catching that

This reverts commit 6b16698.
@billy1624
Copy link
Member

Emmmm... no. We're wrong. sea-orm-macros is not a optional dependency. It's always on, just that we pass-through sea-orm-macros's feature via sea-orm's feature.

Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No questions then!

@billy1624 billy1624 merged commit 3300336 into SeaQL:master May 19, 2023
darkmmon added a commit to darkmmon/seaql.github.io that referenced this pull request Jul 12, 2023
billy1624 added a commit to SeaQL/seaql.github.io that referenced this pull request Jul 19, 2023
* Optional Field SeaQL/sea-orm#1513

* .gitignore SeaQL/sea-orm#1334

* migration table custom name SeaQL/sea-orm#1511

* OR condition relation SeaQL/sea-orm#1433

* DerivePartialModel SeaQL/sea-orm#1597

* space for migration file naming SeaQL/sea-orm#1570

* composite key up to 12 SeaQL/sea-orm#1508

* seaography integration SeaQL/sea-orm#1599

* QuerySelect SimpleExpr SeaQL/sea-orm#1702

* sqlErr SeaQL/sea-orm#1707

* migration check SeaQL/sea-orm#1519

* postgres array SeaQL/sea-orm#1565

* param intoString SeaQL/sea-orm#1439

* **skipped** re-export SeaQL/sea-orm#1661

* ping SeaQL/sea-orm#1627

* on empty do nothing SeaQL/sea-orm#1708

* on conflict do nothing SeaQL/sea-orm#1712

* **skipped** upgrade versions

* active enum fail safe SeaQL/sea-orm#1374

* relation generation check SeaQL/sea-orm#1435

* entity generation bug SeaQL/sea-schema#105

* **skipped** bug fix that does not require edits

* EnumIter change SeaQL/sea-orm#1535

* completed and fixed a previous todo SeaQL/sea-orm#1570

* amended wordings and structures

* Edit

* Remove temp file

---------

Co-authored-by: Billy Chan <[email protected]>
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

🎉 Released In 0.12.1 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

@karatakis karatakis deleted the seaography-generator branch August 7, 2023 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants