Skip to content

Commit

Permalink
Shield SeaORM: Add support for entity model
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Dec 11, 2024
1 parent 8deeaef commit ac60014
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/storage/shield-seaorm/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
```
sea-orm-cli migrate fresh -u "sqlite:///tmp/shield-seaorm.sqlite?mode=rwc" -d ./examples/seaorm
sea-orm-cli generate entity -u "sqlite:///tmp/shield-seaorm.sqlite?mode=rwc" -o packages/storage/shield-seaorm/src/entities --with-serde both
sea-orm-cli generate entity -u "sqlite:///tmp/shield-seaorm.sqlite?mode=rwc" -o packages/storage/shield-seaorm/src/entities_template --with-serde both
```
6 changes: 6 additions & 0 deletions packages/storage/shield-seaorm/src/entities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod prelude;

pub mod email_address;
#[cfg(feature = "entity")]
pub mod entity;
pub mod user;
21 changes: 21 additions & 0 deletions packages/storage/shield-seaorm/src/entities/email_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@ pub struct Model {
pub verification_token: Option<String>,
pub verification_token_expires_at: Option<DateTimeUtc>,
pub verified_at: Option<DateTimeUtc>,
#[cfg(feature = "entity")]
pub entity_id: Uuid,
#[cfg(not(feature = "entity"))]
pub user_id: Uuid,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[cfg(feature = "entity")]
#[sea_orm(
belongs_to = "super::entity::Entity",
from = "Column::EntityId",
to = "super::entity::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Entity,
#[cfg(not(feature = "entity"))]
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
Expand All @@ -32,6 +45,14 @@ pub enum Relation {
User,
}

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

#[cfg(not(feature = "entity"))]
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
Expand Down
37 changes: 37 additions & 0 deletions packages/storage/shield-seaorm/src/entities/entity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "entity")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
#[sea_orm(column_type = "Text")]
pub name: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::email_address::Entity")]
EmailAddress,
#[sea_orm(has_many = "super::user::Entity")]
User,
}

impl Related<super::email_address::Entity> for Entity {
fn to() -> RelationDef {
Relation::EmailAddress.def()
}
}

impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
6 changes: 0 additions & 6 deletions packages/storage/shield-seaorm/src/entities/mod.rs

This file was deleted.

2 changes: 2 additions & 0 deletions packages/storage/shield-seaorm/src/entities/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
pub use super::email_address::Entity as EmailAddress;
#[cfg(feature = "entity")]
pub use super::entity::Entity;
pub use super::user::Entity as User;
21 changes: 21 additions & 0 deletions packages/storage/shield-seaorm/src/entities/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,37 @@ pub struct Model {
pub id: Uuid,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
#[cfg(not(feature = "entity"))]
#[sea_orm(column_type = "Text")]
pub name: String,
#[cfg(feature = "entity")]
pub entity_id: Uuid,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[cfg(feature = "entity")]
#[sea_orm(
belongs_to = "super::entity::Entity",
from = "Column::EntityId",
to = "super::entity::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Entity,
#[cfg(not(feature = "entity"))]
#[sea_orm(has_many = "super::email_address::Entity")]
EmailAddress,
}

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

#[cfg(not(feature = "entity"))]
impl Related<super::email_address::Entity> for Entity {
fn to() -> RelationDef {
Relation::EmailAddress.def()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl MigrationTrait for Migration {
{
manager
.create_table(
BaseTable::create(Entity::Table)
BaseTable::create(Entity::Table, manager)
.col(ColumnDef::new(Entity::Name).text().not_null())
.to_owned(),
)
Expand Down

0 comments on commit ac60014

Please sign in to comment.