diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index d5bb4ef28..5983e4b7b 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -187,6 +187,15 @@ pub enum GenerateSubcommands { help = "The datetime crate to use for generating entities." )] date_time_crate: DateTimeCrate, + + #[clap( + action, + long, + short = 'l', + default_value = "false", + help = "Generate index file as `lib.rs` instead of `mod.rs`." + )] + lib: bool, }, } diff --git a/sea-orm-cli/src/commands/generate.rs b/sea-orm-cli/src/commands/generate.rs index 0cea31e44..1686e878b 100644 --- a/sea-orm-cli/src/commands/generate.rs +++ b/sea-orm-cli/src/commands/generate.rs @@ -26,6 +26,7 @@ pub async fn run_generate_command( with_serde, with_copy_enums, date_time_crate, + lib, } => { if verbose { let _ = tracing_subscriber::fmt() @@ -171,6 +172,7 @@ pub async fn run_generate_command( with_copy_enums, date_time_crate.into(), schema_name, + lib, ); let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context); diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 8227fa482..398b50c22 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -42,6 +42,7 @@ pub struct EntityWriterContext { pub(crate) with_copy_enums: bool, pub(crate) date_time_crate: DateTimeCrate, pub(crate) schema_name: Option, + pub(crate) lib: bool, } impl WithSerde { @@ -101,6 +102,7 @@ impl EntityWriterContext { with_copy_enums: bool, date_time_crate: DateTimeCrate, schema_name: Option, + lib: bool, ) -> Self { Self { expanded_format, @@ -108,6 +110,7 @@ impl EntityWriterContext { with_copy_enums, date_time_crate, schema_name, + lib, } } } @@ -116,7 +119,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_mod()); + files.push(self.write_index_file(context.lib)); files.push(self.write_prelude()); if !self.enums.is_empty() { files.push( @@ -168,7 +171,7 @@ impl EntityWriter { .collect() } - pub fn write_mod(&self) -> OutputFile { + pub fn write_index_file(&self, lib: bool) -> OutputFile { let mut lines = Vec::new(); Self::write_doc_comment(&mut lines); let code_blocks: Vec = self.entities.iter().map(Self::gen_mod).collect(); @@ -188,8 +191,14 @@ impl EntityWriter { }], ); } + + let file_name = match lib { + true => "lib.rs".to_owned(), + false => "mod.rs".to_owned(), + }; + OutputFile { - name: "mod.rs".to_owned(), + name: file_name, content: lines.join("\n"), } }