diff --git a/Cargo.toml b/Cargo.toml index 87f7eed0..833d19f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ thiserror = { version = "1.0.44" } fnv = { version = "1.0.7" } [features] -default = [] +default = ["camel-case-field"] with-json = ["sea-orm/with-json"] with-chrono = ["sea-orm/with-chrono", "async-graphql/chrono"] with-time = ["sea-orm/with-time", "async-graphql/time"] @@ -34,3 +34,5 @@ with-bigdecimal = ["sea-orm/with-bigdecimal", "async-graphql/bigdecimal"] with-postgres-array = ["sea-orm/postgres-array"] # with-ipnetwork = ["sea-orm/with-ipnetwork"] # with-mac_address = ["sea-orm/with-mac_address"] +snake-case-field = [] +camel-case-field = [] diff --git a/src/enumerations/active_enum.rs b/src/enumerations/active_enum.rs index 163e3e14..70f5e386 100644 --- a/src/enumerations/active_enum.rs +++ b/src/enumerations/active_enum.rs @@ -1,5 +1,5 @@ use async_graphql::dynamic::Enum; -use heck::ToUpperCamelCase; +use heck::{ToSnakeCase, ToUpperCamelCase}; use sea_orm::{ActiveEnum, DynIden, Value}; use crate::BuilderContext; @@ -19,7 +19,11 @@ impl std::default::Default for ActiveEnumConfig { format!("{}Enum", name.to_upper_camel_case()) }), variant_name: Box::new(|_enum_name: &str, variant: &str| -> String { - variant.to_upper_camel_case().to_ascii_uppercase() + if cfg!(feature = "snake-case-field") { + variant.to_snake_case() + } else { + variant.to_upper_camel_case().to_ascii_uppercase() + } }), } } diff --git a/src/inputs/active_enum_filter_input.rs b/src/inputs/active_enum_filter_input.rs index 8c24fe49..afdfab2c 100644 --- a/src/inputs/active_enum_filter_input.rs +++ b/src/inputs/active_enum_filter_input.rs @@ -1,7 +1,7 @@ use std::collections::BTreeSet; use async_graphql::dynamic::ObjectAccessor; -use heck::ToUpperCamelCase; +use heck::{ToSnakeCase, ToUpperCamelCase}; use sea_orm::{ActiveEnum, ColumnTrait, ColumnType, Condition, DynIden, EntityTrait}; use crate::{ActiveEnumBuilder, BuilderContext, FilterInfo, FilterOperation, SeaResult}; @@ -88,10 +88,12 @@ where let extract_variant = move |input: &str| -> String { let variant = variants.iter().find(|variant| { - let variant = variant - .to_string() - .to_upper_camel_case() - .to_ascii_uppercase(); + let variant = variant.to_string(); + let variant = if cfg!(feature = "snake-case-field") { + variant.to_snake_case() + } else { + variant.to_upper_camel_case().to_ascii_uppercase() + }; variant.eq(input) }); variant.unwrap().to_string() diff --git a/src/mutation/entity_create_batch_mutation.rs b/src/mutation/entity_create_batch_mutation.rs index 08ab954d..08389a29 100644 --- a/src/mutation/entity_create_batch_mutation.rs +++ b/src/mutation/entity_create_batch_mutation.rs @@ -19,7 +19,14 @@ pub struct EntityCreateBatchMutationConfig { impl std::default::Default for EntityCreateBatchMutationConfig { fn default() -> Self { EntityCreateBatchMutationConfig { - mutation_suffix: "CreateBatch".into(), + mutation_suffix: { + if cfg!(feature = "snake-case-field") { + "_create_batch" + } else { + "CreateBatch" + } + .into() + }, data_field: "data".into(), } } diff --git a/src/mutation/entity_create_one_mutation.rs b/src/mutation/entity_create_one_mutation.rs index 110af657..22efb99b 100644 --- a/src/mutation/entity_create_one_mutation.rs +++ b/src/mutation/entity_create_one_mutation.rs @@ -19,7 +19,14 @@ pub struct EntityCreateOneMutationConfig { impl std::default::Default for EntityCreateOneMutationConfig { fn default() -> Self { EntityCreateOneMutationConfig { - mutation_suffix: "CreateOne".into(), + mutation_suffix: { + if cfg!(feature = "snake-case-field") { + "_create_one" + } else { + "CreateOne" + } + .into() + }, data_field: "data".into(), } } diff --git a/src/mutation/entity_delete_mutation.rs b/src/mutation/entity_delete_mutation.rs index fa1ed0cd..b2b0591b 100644 --- a/src/mutation/entity_delete_mutation.rs +++ b/src/mutation/entity_delete_mutation.rs @@ -20,7 +20,14 @@ pub struct EntityDeleteMutationConfig { impl std::default::Default for EntityDeleteMutationConfig { fn default() -> Self { Self { - mutation_suffix: "Delete".into(), + mutation_suffix: { + if cfg!(feature = "snake-case-field") { + "_delete" + } else { + "Delete" + } + .into() + }, filter_field: "filter".into(), } } diff --git a/src/mutation/entity_update_mutation.rs b/src/mutation/entity_update_mutation.rs index 28070c41..998944b1 100644 --- a/src/mutation/entity_update_mutation.rs +++ b/src/mutation/entity_update_mutation.rs @@ -24,7 +24,14 @@ pub struct EntityUpdateMutationConfig { impl std::default::Default for EntityUpdateMutationConfig { fn default() -> Self { Self { - mutation_suffix: "Update".into(), + mutation_suffix: { + if cfg!(feature = "snake-case-field") { + "_update" + } else { + "Update" + } + .into() + }, data_field: "data".into(), filter_field: "filter".into(), } diff --git a/src/outputs/connection_object.rs b/src/outputs/connection_object.rs index a01b16de..7314f82a 100644 --- a/src/outputs/connection_object.rs +++ b/src/outputs/connection_object.rs @@ -42,8 +42,22 @@ impl std::default::Default for ConnectionObjectConfig { type_name: Box::new(|object_name: &str| -> String { format!("{}Connection", object_name) }), - page_info: "pageInfo".into(), - pagination_info: "paginationInfo".into(), + page_info: { + if cfg!(feature = "snake-case-field") { + "page_info" + } else { + "pageInfo" + } + .into() + }, + pagination_info: { + if cfg!(feature = "snake-case-field") { + "pagination_info" + } else { + "paginationInfo" + } + .into() + }, edges: "edges".into(), nodes: "nodes".into(), } diff --git a/src/outputs/entity_object.rs b/src/outputs/entity_object.rs index 96509f25..321b5873 100644 --- a/src/outputs/entity_object.rs +++ b/src/outputs/entity_object.rs @@ -1,6 +1,6 @@ use async_graphql::dynamic::{Field, FieldFuture, Object}; use async_graphql::{Error, Value}; -use heck::{ToLowerCamelCase, ToUpperCamelCase}; +use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase}; use sea_orm::{ColumnTrait, ColumnType, EntityName, EntityTrait, IdenStatic, Iterable, ModelTrait}; /// The configuration structure for EntityObjectBuilder @@ -20,7 +20,11 @@ impl std::default::Default for EntityObjectConfig { entity_name.to_upper_camel_case() }), column_name: Box::new(|_entity_name: &str, column_name: &str| -> String { - column_name.to_lower_camel_case() + if cfg!(feature = "snake-case-field") { + column_name.to_snake_case() + } else { + column_name.to_lower_camel_case() + } }), basic_type_suffix: "Basic".into(), } diff --git a/src/outputs/page_info_object.rs b/src/outputs/page_info_object.rs index 02386c9b..5209dafd 100644 --- a/src/outputs/page_info_object.rs +++ b/src/outputs/page_info_object.rs @@ -30,10 +30,38 @@ impl std::default::Default for PageInfoObjectConfig { fn default() -> Self { PageInfoObjectConfig { type_name: "PageInfo".into(), - has_previous_page: "hasPreviousPage".into(), - has_next_page: "hasNextPage".into(), - start_cursor: "startCursor".into(), - end_cursor: "endCursor".into(), + has_previous_page: { + if cfg!(feature = "snake-case-field") { + "has_previous_page" + } else { + "hasPreviousPage" + } + .into() + }, + has_next_page: { + if cfg!(feature = "snake-case-field") { + "has_next_page" + } else { + "hasNextPage" + } + .into() + }, + start_cursor: { + if cfg!(feature = "snake-case-field") { + "start_cursor" + } else { + "startCursor" + } + .into() + }, + end_cursor: { + if cfg!(feature = "snake-case-field") { + "end_cursor" + } else { + "endCursor" + } + .into() + }, } } } diff --git a/src/query/entity_object_relation.rs b/src/query/entity_object_relation.rs index a813c1da..8d1ddf43 100644 --- a/src/query/entity_object_relation.rs +++ b/src/query/entity_object_relation.rs @@ -3,7 +3,7 @@ use async_graphql::{ dynamic::{Field, FieldFuture, FieldValue, InputValue, TypeRef}, Error, }; -use heck::ToSnakeCase; +use heck::{ToLowerCamelCase, ToSnakeCase}; use sea_orm::{EntityTrait, Iden, ModelTrait, RelationDef}; use crate::{ @@ -30,6 +30,11 @@ impl EntityObjectRelationBuilder { ::Model: Sync, <::Column as std::str::FromStr>::Err: core::fmt::Debug, { + let name = if cfg!(feature = "snake-case-field") { + name.to_snake_case() + } else { + name.to_lower_camel_case() + }; let context: &'static BuilderContext = self.context; let entity_object_builder = EntityObjectBuilder { context }; let connection_object_builder = ConnectionObjectBuilder { context }; diff --git a/src/query/entity_object_via_relation.rs b/src/query/entity_object_via_relation.rs index db84ad23..624e8c20 100644 --- a/src/query/entity_object_via_relation.rs +++ b/src/query/entity_object_via_relation.rs @@ -3,7 +3,7 @@ use async_graphql::{ dynamic::{Field, FieldFuture, FieldValue, InputValue, TypeRef}, Error, }; -use heck::ToSnakeCase; +use heck::{ToLowerCamelCase, ToSnakeCase}; use sea_orm::{ ColumnTrait, Condition, DatabaseConnection, EntityTrait, Iden, ModelTrait, QueryFilter, Related, }; @@ -33,6 +33,11 @@ impl EntityObjectViaRelationBuilder { <::Column as std::str::FromStr>::Err: core::fmt::Debug, <::Column as std::str::FromStr>::Err: core::fmt::Debug, { + let name = if cfg!(feature = "snake-case-field") { + name.to_snake_case() + } else { + name.to_lower_camel_case() + }; let context: &'static BuilderContext = self.context; let to_relation_definition = >::to(); let (via_relation_definition, is_via_relation) = match >::via() { diff --git a/src/query/entity_query_field.rs b/src/query/entity_query_field.rs index 168b9da7..0d2ceddb 100644 --- a/src/query/entity_query_field.rs +++ b/src/query/entity_query_field.rs @@ -2,7 +2,7 @@ use async_graphql::{ dynamic::{Field, FieldFuture, FieldValue, InputValue, TypeRef}, Error, }; -use heck::ToLowerCamelCase; +use heck::{ToLowerCamelCase, ToSnakeCase}; use sea_orm::{DatabaseConnection, EntityTrait, QueryFilter}; use crate::{ @@ -27,10 +27,21 @@ impl std::default::Default for EntityQueryFieldConfig { fn default() -> Self { EntityQueryFieldConfig { type_name: Box::new(|object_name: &str| -> String { - object_name.to_lower_camel_case() + if cfg!(feature = "snake-case-field") { + object_name.to_snake_case() + } else { + object_name.to_lower_camel_case() + } }), filters: "filters".into(), - order_by: "orderBy".into(), + order_by: { + if cfg!(feature = "snake-case-field") { + "order_by" + } else { + "orderBy" + } + .into() + }, pagination: "pagination".into(), } }