Skip to content

Commit

Permalink
[sea-orm-cli] generate entity with relation variant order by name of …
Browse files Browse the repository at this point in the history
…reference table (#1229)
  • Loading branch information
billy1624 committed Nov 24, 2022
1 parent 80c81ea commit a7c8970
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 13 additions & 7 deletions sea-orm-codegen/src/entity/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ use crate::{
PrimaryKey, Relation, RelationType,
};
use sea_query::{ColumnSpec, TableCreateStatement};
use std::collections::HashMap;
use std::collections::BTreeMap;

#[derive(Clone, Debug)]
pub struct EntityTransformer;

impl EntityTransformer {
pub fn transform(table_create_stmts: Vec<TableCreateStatement>) -> Result<EntityWriter, Error> {
let mut enums: HashMap<String, ActiveEnum> = HashMap::new();
let mut inverse_relations: HashMap<String, Vec<Relation>> = HashMap::new();
let mut conjunct_relations: HashMap<String, Vec<ConjunctRelation>> = HashMap::new();
let mut entities = HashMap::new();
let mut enums: BTreeMap<String, ActiveEnum> = BTreeMap::new();
let mut inverse_relations: BTreeMap<String, Vec<Relation>> = BTreeMap::new();
let mut conjunct_relations: BTreeMap<String, Vec<ConjunctRelation>> = BTreeMap::new();
let mut entities = BTreeMap::new();
for table_create in table_create_stmts.into_iter() {
let table_name = match table_create.get_table_name() {
Some(table_ref) => match table_ref {
Expand Down Expand Up @@ -71,7 +71,7 @@ impl EntityTransformer {
col
})
.collect();
let mut ref_table_counts: HashMap<String, usize> = HashMap::new();
let mut ref_table_counts: BTreeMap<String, usize> = BTreeMap::new();
let relations: Vec<Relation> = table_create
.get_foreign_key_create_stmts()
.iter()
Expand Down Expand Up @@ -202,7 +202,13 @@ impl EntityTransformer {
}
}
Ok(EntityWriter {
entities: entities.into_iter().map(|(_, v)| v).collect(),
entities: entities
.into_iter()
.map(|(_, mut v)| {
v.relations.sort_by(|a, b| a.ref_table.cmp(&b.ref_table));
v
})
.collect(),
enums,
})
}
Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::{util::escape_rust_keyword, ActiveEnum, Entity};
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::{collections::HashMap, str::FromStr};
use std::{collections::BTreeMap, str::FromStr};
use syn::{punctuated::Punctuated, token::Comma};
use tracing::info;

#[derive(Clone, Debug)]
pub struct EntityWriter {
pub(crate) entities: Vec<Entity>,
pub(crate) enums: HashMap<String, ActiveEnum>,
pub(crate) enums: BTreeMap<String, ActiveEnum>,
}

pub struct WriterOutput {
Expand Down

0 comments on commit a7c8970

Please sign in to comment.