cargo loco generate model --link bundles_services bundle:references service:references optional:bool

This commit is contained in:
Douze Bé 2024-10-21 14:44:07 +02:00
parent 6cbd41b1f9
commit ee1a27bb8d
11 changed files with 202 additions and 3 deletions

View file

@ -8,6 +8,7 @@ mod m20231103_114510_notes;
mod m20241016_181828_servers; mod m20241016_181828_servers;
mod m20241021_121449_bundles; mod m20241021_121449_bundles;
mod m20241021_121806_services; mod m20241021_121806_services;
mod m20241021_124238_bundles_services;
pub struct Migrator; pub struct Migrator;
#[async_trait::async_trait] #[async_trait::async_trait]
@ -19,6 +20,7 @@ impl MigratorTrait for Migrator {
Box::new(m20241016_181828_servers::Migration), Box::new(m20241016_181828_servers::Migration),
Box::new(m20241021_121449_bundles::Migration), Box::new(m20241021_121449_bundles::Migration),
Box::new(m20241021_121806_services::Migration), Box::new(m20241021_121806_services::Migration),
Box::new(m20241021_124238_bundles_services::Migration),
] ]
} }
} }

View file

@ -0,0 +1,71 @@
use loco_rs::schema::table_auto_tz;
use sea_orm_migration::{prelude::*, schema::*};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
table_auto_tz(BundlesServices::Table)
.primary_key(
Index::create()
.name("idx-bundles_services-refs-pk")
.table(BundlesServices::Table)
.col(BundlesServices::BundleId)
.col(BundlesServices::ServiceId)
,
)
.col(integer(BundlesServices::BundleId))
.col(integer(BundlesServices::ServiceId))
.col(boolean_null(BundlesServices::Optional))
.foreign_key(
ForeignKey::create()
.name("fk-bundles_services-bundles")
.from(BundlesServices::Table, BundlesServices::BundleId)
.to(Bundles::Table, Bundles::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk-bundles_services-services")
.from(BundlesServices::Table, BundlesServices::ServiceId)
.to(Services::Table, Services::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(BundlesServices::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum BundlesServices {
Table,
BundleId,
ServiceId,
Optional,
}
#[derive(DeriveIden)]
enum Bundles {
Table,
Id,
}
#[derive(DeriveIden)]
enum Services {
Table,
Id,
}

View file

@ -17,4 +17,22 @@ pub struct Model {
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {} pub enum Relation {
#[sea_orm(has_many = "super::bundles_services::Entity")]
BundlesServices,
}
impl Related<super::bundles_services::Entity> for Entity {
fn to() -> RelationDef {
Relation::BundlesServices.def()
}
}
impl Related<super::services::Entity> for Entity {
fn to() -> RelationDef {
super::bundles_services::Relation::Services.def()
}
fn via() -> Option<RelationDef> {
Some(super::bundles_services::Relation::Bundles.def().rev())
}
}

View file

@ -0,0 +1,48 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "bundles_services")]
pub struct Model {
pub created_at: DateTimeWithTimeZone,
pub updated_at: DateTimeWithTimeZone,
#[sea_orm(primary_key, auto_increment = false)]
pub bundle_id: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub service_id: i32,
pub optional: Option<bool>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::bundles::Entity",
from = "Column::BundleId",
to = "super::bundles::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Bundles,
#[sea_orm(
belongs_to = "super::services::Entity",
from = "Column::ServiceId",
to = "super::services::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Services,
}
impl Related<super::bundles::Entity> for Entity {
fn to() -> RelationDef {
Relation::Bundles.def()
}
}
impl Related<super::services::Entity> for Entity {
fn to() -> RelationDef {
Relation::Services.def()
}
}

View file

@ -3,6 +3,7 @@
pub mod prelude; pub mod prelude;
pub mod bundles; pub mod bundles;
pub mod bundles_services;
pub mod notes; pub mod notes;
pub mod servers; pub mod servers;
pub mod services; pub mod services;

View file

@ -1,6 +1,7 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0 //! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
pub use super::bundles::Entity as Bundles; pub use super::bundles::Entity as Bundles;
pub use super::bundles_services::Entity as BundlesServices;
pub use super::notes::Entity as Notes; pub use super::notes::Entity as Notes;
pub use super::servers::Entity as Servers; pub use super::servers::Entity as Servers;
pub use super::services::Entity as Services; pub use super::services::Entity as Services;

View file

@ -19,4 +19,22 @@ pub struct Model {
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {} pub enum Relation {
#[sea_orm(has_many = "super::bundles_services::Entity")]
BundlesServices,
}
impl Related<super::bundles_services::Entity> for Entity {
fn to() -> RelationDef {
Relation::BundlesServices.def()
}
}
impl Related<super::bundles::Entity> for Entity {
fn to() -> RelationDef {
super::bundles_services::Relation::Bundles.def()
}
fn via() -> Option<RelationDef> {
Some(super::bundles_services::Relation::Services.def().rev())
}
}

View file

@ -0,0 +1,7 @@
use sea_orm::entity::prelude::*;
use super::_entities::bundles_services::{ActiveModel, Entity};
pub type BundlesServices = Entity;
impl ActiveModelBehavior for ActiveModel {
// extend activemodel below (keep comment for generators)
}

View file

@ -4,3 +4,4 @@ pub mod users;
pub mod servers; pub mod servers;
pub mod bundles; pub mod bundles;
pub mod services; pub mod services;
pub mod bundles_services;

View file

@ -0,0 +1,31 @@
use nixin_farm_ssr::app::App;
use loco_rs::testing;
use serial_test::serial;
macro_rules! configure_insta {
($($expr:expr),*) => {
let mut settings = insta::Settings::clone_current();
settings.set_prepend_module_to_snapshot(false);
let _guard = settings.bind_to_scope();
};
}
#[tokio::test]
#[serial]
async fn test_model() {
configure_insta!();
let boot = testing::boot_test::<App>().await.unwrap();
testing::seed::<App>(&boot.app_context.db).await.unwrap();
// query your model, e.g.:
//
// let item = models::posts::Model::find_by_pid(
// &boot.app_context.db,
// "11111111-1111-1111-1111-111111111111",
// )
// .await;
// snapshot the result:
// assert_debug_snapshot!(item);
}

View file

@ -3,3 +3,4 @@ mod users;
mod servers; mod servers;
mod bundles; mod bundles;
mod services; mod services;
mod bundles_services;