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
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 1 addition & 5 deletions examples/loco_seaography/src/graphql/query_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions examples/loco_seaography/src/models/_entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ pub mod prelude;
pub mod files;
pub mod notes;
pub mod users;

seaography::register_entity_modules!([files, notes, users]);
8 changes: 2 additions & 6 deletions examples/react_admin/backend/src/graphql/query_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ pub fn schema(
complexity: usize,
) -> Result<Schema, SchemaError> {
// 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()
Expand Down
2 changes: 2 additions & 0 deletions examples/react_admin/backend/src/models/_entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ pub mod prelude;
pub mod files;
pub mod notes;
pub mod users;

seaography::register_entity_modules!([files, notes, users]);
2 changes: 2 additions & 0 deletions examples/seaography_example/graphql/src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
4 changes: 2 additions & 2 deletions examples/seaography_example/graphql/src/query_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub fn schema(
depth: Option<usize>,
complexity: Option<usize>,
) -> Result<Schema, SchemaError> {
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)
Expand Down
52 changes: 50 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.enums);
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,48 @@ impl EntityWriter {
}
}

pub fn gen_seaography_entity_mod(
entities: &[Entity],
enums: &BTreeMap<String, ActiveEnum>,
) -> 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();
Expand Down