From bfca88623638f0ba6985520b7c27af9e3be15826 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 2 Dec 2024 17:14:06 +0800 Subject: [PATCH] codegen: register seaography entity module (#2403) * codegen: register seaography entity module * codegen: register seaography active enum * Update seaography examples * fmt --- .../loco_seaography/src/graphql/query_root.rs | 6 +-- .../src/models/_entities/mod.rs | 2 + .../backend/src/graphql/query_root.rs | 8 +-- .../backend/src/models/_entities/mod.rs | 2 + .../graphql/src/entities/mod.rs | 2 + .../graphql/src/query_root.rs | 4 +- sea-orm-codegen/src/entity/writer.rs | 52 ++++++++++++++++++- 7 files changed, 61 insertions(+), 15 deletions(-) diff --git a/examples/loco_seaography/src/graphql/query_root.rs b/examples/loco_seaography/src/graphql/query_root.rs index ae0528364..7230603e9 100644 --- a/examples/loco_seaography/src/graphql/query_root.rs +++ b/examples/loco_seaography/src/graphql/query_root.rs @@ -14,11 +14,7 @@ pub fn schema( // Builder of Seaography query root let mut builder = Builder::new(&CONTEXT, database.clone()); // Register SeaORM entities - seaography::register_entities!( - builder, - // List all models we want to include in the GraphQL endpoint here - [files, notes, users] - ); + let builder = crate::models::_entities::register_entity_modules(builder); // Configure async GraphQL limits let schema = builder .schema_builder() diff --git a/examples/loco_seaography/src/models/_entities/mod.rs b/examples/loco_seaography/src/models/_entities/mod.rs index c60d0956d..23ec9cc0b 100644 --- a/examples/loco_seaography/src/models/_entities/mod.rs +++ b/examples/loco_seaography/src/models/_entities/mod.rs @@ -5,3 +5,5 @@ pub mod prelude; pub mod files; pub mod notes; pub mod users; + +seaography::register_entity_modules!([files, notes, users]); diff --git a/examples/react_admin/backend/src/graphql/query_root.rs b/examples/react_admin/backend/src/graphql/query_root.rs index ae0528364..74dbe1b86 100644 --- a/examples/react_admin/backend/src/graphql/query_root.rs +++ b/examples/react_admin/backend/src/graphql/query_root.rs @@ -12,13 +12,9 @@ pub fn schema( complexity: usize, ) -> Result { // Builder of Seaography query root - let mut builder = Builder::new(&CONTEXT, database.clone()); + let builder = Builder::new(&CONTEXT, database.clone()); // Register SeaORM entities - seaography::register_entities!( - builder, - // List all models we want to include in the GraphQL endpoint here - [files, notes, users] - ); + let builder = crate::models::_entities::register_entity_modules(builder); // Configure async GraphQL limits let schema = builder .schema_builder() diff --git a/examples/react_admin/backend/src/models/_entities/mod.rs b/examples/react_admin/backend/src/models/_entities/mod.rs index c60d0956d..23ec9cc0b 100644 --- a/examples/react_admin/backend/src/models/_entities/mod.rs +++ b/examples/react_admin/backend/src/models/_entities/mod.rs @@ -5,3 +5,5 @@ pub mod prelude; pub mod files; pub mod notes; pub mod users; + +seaography::register_entity_modules!([files, notes, users]); diff --git a/examples/seaography_example/graphql/src/entities/mod.rs b/examples/seaography_example/graphql/src/entities/mod.rs index dc59aec47..fe886c6f5 100644 --- a/examples/seaography_example/graphql/src/entities/mod.rs +++ b/examples/seaography_example/graphql/src/entities/mod.rs @@ -6,3 +6,5 @@ pub mod baker; pub mod bakery; pub mod cake; pub mod cake_baker; + +seaography::register_entity_modules!([baker, bakery, cake, cake_baker]); diff --git a/examples/seaography_example/graphql/src/query_root.rs b/examples/seaography_example/graphql/src/query_root.rs index d9248ff29..76972459c 100644 --- a/examples/seaography_example/graphql/src/query_root.rs +++ b/examples/seaography_example/graphql/src/query_root.rs @@ -11,8 +11,8 @@ pub fn schema( depth: Option, complexity: Option, ) -> Result { - let mut builder = Builder::new(&CONTEXT, database.clone()); - seaography::register_entities!(builder, [baker, bakery, cake, cake_baker,]); + let builder = Builder::new(&CONTEXT, database.clone()); + let builder = crate::entities::register_entity_modules(builder); let schema = builder.schema_builder(); let schema = if let Some(depth) = depth { schema.limit_depth(depth) diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 1660c2221..f9b26bdd6 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -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( @@ -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 = self.entities.iter().map(Self::gen_mod).collect(); @@ -266,6 +266,12 @@ impl EntityWriter { ); } + if seaography { + lines.push("".to_owned()); + let ts = Self::gen_seaography_entity_mod(&self.entities, &self.enums); + Self::write(&mut lines, vec![ts]); + } + let file_name = match lib { true => "lib.rs".to_owned(), false => "mod.rs".to_owned(), @@ -704,6 +710,48 @@ impl EntityWriter { } } + pub fn gen_seaography_entity_mod( + entities: &[Entity], + enums: &BTreeMap, + ) -> 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, + } + } + ts = quote! { + seaography::register_entity_modules!([ + #ts + ]); + }; + + let mut enum_ts = TokenStream::new(); + for active_enum in enums.values() { + let enum_name = &active_enum.enum_name.to_string(); + let enum_iden = format_ident!("{}", enum_name.to_upper_camel_case()); + enum_ts = quote! { + #enum_ts + sea_orm_active_enums::#enum_iden + } + } + if !enum_ts.is_empty() { + ts = quote! { + #ts + + seaography::register_active_enums!([ + #enum_ts + ]); + }; + } + 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();