Skip to content

feat(codgen): Allow hiding SeaORM specific traits behind features#2988

Draft
AaronDewes wants to merge 4 commits intoSeaQL:masterfrom
nirvati:codegen-feature-flags
Draft

feat(codgen): Allow hiding SeaORM specific traits behind features#2988
AaronDewes wants to merge 4 commits intoSeaQL:masterfrom
nirvati:codegen-feature-flags

Conversation

@AaronDewes
Copy link

This PR makes it possible to set a --sea-orm-feature flag that makes generated code not depend on sea-orm unless that feature is enabled.

This allows having just one set of generated files that can be used on frontend and backend for shared types. The --frontend-format flag previously required creating different files.


This PR currently lacks tests. Before creating tests, I wanted to open this draft to see if this is even a feature you want or if it's something you wouldn't merge anyway.

PR Info

New Features

  • In codegen, allow making any SeaORM-related code only available with a feature enabled.

Bug Fixes

None

Breaking Changes

None

Changes

None

@Huliiiiii Huliiiiii requested a review from tyt2y3 March 5, 2026 21:52
@AaronDewes AaronDewes force-pushed the codegen-feature-flags branch from a18d6a1 to 3a1d4fb Compare March 5, 2026 22:08
@AaronDewes AaronDewes force-pushed the codegen-feature-flags branch from 48c23ce to a311708 Compare March 5, 2026 22:36
@tyt2y3
Copy link
Member

tyt2y3 commented Mar 6, 2026

interesting, can you give an example what the generated entity looks like?

@AaronDewes
Copy link
Author

interesting, can you give an example what the generated entity looks like?

Here's an example from a project where I'm using this with Leptos (--sea-orm-feature ssr):

//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0

#[cfg(feature = "ssr")]
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "ssr", derive(DeriveEntityModel))]
#[cfg_attr(feature = "ssr", sea_orm(table_name = "teams"))]
pub struct Model {
    #[cfg_attr(feature = "ssr", sea_orm(primary_key))]
    pub id: i32,
    pub name: String,
    pub org_id: i32,
}

#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "ssr", derive(EnumIter, DeriveRelation))]
pub enum Relation {
    #[cfg_attr(feature = "ssr", sea_orm(has_many = "super::apps::Entity"))]
    Apps,
    #[cfg_attr(
        feature = "ssr",
        sea_orm(
            belongs_to = "super::organizations::Entity",
            from = "Column::OrgId",
            to = "super::organizations::Column::Id",
            on_update = "NoAction",
            on_delete = "Cascade",
        )
    )]
    Organizations,
    #[cfg_attr(feature = "ssr", sea_orm(has_many = "super::team_memberships::Entity"))]
    TeamMemberships,
}

#[cfg(feature = "ssr")]
impl Related<super::apps::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Apps.def()
    }
}

#[cfg(feature = "ssr")]
impl Related<super::organizations::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Organizations.def()
    }
}

#[cfg(feature = "ssr")]
impl Related<super::team_memberships::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::TeamMemberships.def()
    }
}

#[cfg(feature = "ssr")]
#[cfg(feature = "ssr")]
impl ActiveModelBehavior for ActiveModel {}

(This is the compact format, I will fix the duplicate cfg in the last block later)

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.

2 participants