Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen: register seaography entity module #2403

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl EntityWriter {
pub fn generate(self, context: &EntityWriterContext) -> WriterOutput {
let mut files = Vec::new();
files.extend(self.write_entities(context));
files.push(self.write_index_file(context.lib));
files.push(self.write_index_file(context.lib, context.seaography));
files.push(self.write_prelude());
if !self.enums.is_empty() {
files.push(self.write_sea_orm_active_enums(
Expand Down Expand Up @@ -245,7 +245,7 @@ impl EntityWriter {
.collect()
}

pub fn write_index_file(&self, lib: bool) -> OutputFile {
pub fn write_index_file(&self, lib: bool, seaography: bool) -> OutputFile {
let mut lines = Vec::new();
Self::write_doc_comment(&mut lines);
let code_blocks: Vec<TokenStream> = self.entities.iter().map(Self::gen_mod).collect();
Expand All @@ -266,6 +266,12 @@ impl EntityWriter {
);
}

if seaography {
lines.push("".to_owned());
let ts = Self::gen_seaography_entity_mod(&self.entities);
Self::write(&mut lines, vec![ts]);
}

let file_name = match lib {
true => "lib.rs".to_owned(),
false => "mod.rs".to_owned(),
Expand Down Expand Up @@ -704,6 +710,25 @@ impl EntityWriter {
}
}

pub fn gen_seaography_entity_mod(entities: &[Entity]) -> TokenStream {
let mut ts = TokenStream::new();
for entity in entities {
let table_name_snake_case_ident = format_ident!(
"{}",
escape_rust_keyword(entity.get_table_name_snake_case_ident())
);
ts = quote! {
#ts
#table_name_snake_case_ident,
}
}
quote! {
seaography::register_entity_modules!([
#ts
]);
}
}

pub fn gen_prelude_use(entity: &Entity) -> TokenStream {
let table_name_snake_case_ident = entity.get_table_name_snake_case_ident();
let table_name_camel_case_ident = entity.get_table_name_camel_case_ident();
Expand Down