Skip to content

Commit

Permalink
derive seaography::RelationBuilder only when seaography feature i…
Browse files Browse the repository at this point in the history
…s enabled
  • Loading branch information
billy1624 committed May 17, 2023
1 parent 433f7c4 commit 0833478
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@ runtime-tokio-rustls = [
"runtime-tokio",
]
tests-cfg = ["serde/derive"]
seaography = ["sea-orm-macros/seaography"]
24 changes: 24 additions & 0 deletions issues/1599/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[workspace]
# A separate workspace

[package]
name = "sea-orm-issues-1599"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
anyhow = "1"
serde = "1"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
seaography = { path = "../../../seaography", optional = true }
async-graphql = { version = "5", optional = true }

[dependencies.sea-orm]
path = "../../"
default-features = false
features = ["macros"]

[features]
default = ["seaography"]
seaography = ["dep:seaography", "async-graphql", "sea-orm/seaography"]
60 changes: 60 additions & 0 deletions issues/1599/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/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/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/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 {}
11 changes: 11 additions & 0 deletions issues/1599/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use anyhow::Result;

pub mod cake;
pub mod cake_filling;
pub mod filling;
pub mod fruit;

#[tokio::main]
async fn main() -> Result<()> {
Ok(())
}
1 change: 1 addition & 0 deletions sea-orm-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ default = ["derive"]
postgres-array = []
derive = ["bae"]
strum = []
seaography = []
10 changes: 7 additions & 3 deletions sea-orm-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,13 @@ pub fn derive_relation(input: TokenStream) -> TokenStream {
#[proc_macro_derive(DeriveRelatedEntity, attributes(sea_orm))]
pub fn derive_related_entity(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
derives::expand_derive_related_entity(input)
.unwrap_or_else(Error::into_compile_error)
.into()
if cfg!(feature = "seaography") {
derives::expand_derive_related_entity(input)
.unwrap_or_else(Error::into_compile_error)
.into()
} else {
TokenStream::new()
}
}

/// The DeriveMigrationName derive macro will implement `sea_orm_migration::MigrationName` for a migration.
Expand Down
64 changes: 64 additions & 0 deletions src/tests_cfg/cake_seaography.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate as sea_orm;
use crate::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 {}

0 comments on commit 0833478

Please sign in to comment.