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
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f03864e
Add DeriveRelatedEntity macro
karatakis Apr 14, 2023
d9c6579
Add generation for related enum and seaography
karatakis Apr 14, 2023
0ecc57c
Add seaography cli param
karatakis Apr 14, 2023
474f471
update codegen tests
karatakis Apr 14, 2023
ff01616
Fix DeriveRelatedEntity macro doc and includes
karatakis Apr 14, 2023
e9ff1c0
Fix all RelatedEntity variants for RelationBuilder
karatakis Apr 15, 2023
04bd604
Add tests for code
karatakis Apr 15, 2023
516272b
Cargo format
karatakis Apr 15, 2023
f60d9d6
Fix clippy code
karatakis Apr 15, 2023
0e188ca
Fix format
karatakis Apr 15, 2023
5b37895
Merge branch 'SeaQL:master' into seaography-generator
karatakis Apr 15, 2023
f23cd63
Fix unit tests
karatakis Apr 15, 2023
77e46dc
Fix unit tests
karatakis Apr 15, 2023
215bd04
Provide default for seaography::RelationBuilder
karatakis Apr 15, 2023
eedc835
Update changelog
karatakis Apr 15, 2023
6428de1
Update tests
karatakis Apr 17, 2023
2616fd4
Modify code to match feedback
karatakis Apr 17, 2023
499ce88
Update documentation
karatakis Apr 17, 2023
cbbbf10
Update Changelog
karatakis Apr 17, 2023
b49d416
Fix format errors
karatakis Apr 17, 2023
fc5232c
Fix code generation
karatakis Apr 18, 2023
fa86415
Fix unit tests
karatakis Apr 18, 2023
9ebf2bf
Merge branch 'master' into seaography-generator
karatakis Apr 27, 2023
433f7c4
Update lib.rs
karatakis Apr 27, 2023
0833478
derive `seaography::RelationBuilder` only when `seaography` feature i…
billy1624 May 17, 2023
c84294e
Try constructing async-graphql root for "related entity" and "entity"…
billy1624 May 17, 2023
494d527
Merge branch 'master' into pr/1599
billy1624 May 18, 2023
2ecfdb9
Update demo
billy1624 May 18, 2023
4117664
CHANGELOG
billy1624 May 18, 2023
6b16698
Update Cargo.toml
billy1624 May 19, 2023
2e1f4a5
Revert "Update Cargo.toml"
billy1624 May 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
billy1624 marked this conversation as resolved.
Show resolved Hide resolved
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