sea-query
sea-query copied to clipboard
Allow convenience macros that impl Iden to support pluralization for table names.
#[enum_def]
pub struct User {
...
}
The above produced a UserIden
enum whose UserIden::Table
is "user"
when converted to a string. Would be great if we had the option to pluralize the table when using a derive or attr macro to stick with SQL table naming conventions.
@solidiquis, hello, do you mean that table name should be users
?
I want to create derive-v2 macros where I merge sea-query-derive and sea-query-attr.
I think it's better to allow overriding the string representation of UserIden::Table
. That'd enable user to rename the Table
variant including pluralizing it.
I want to create derive-v2 macros where I merge sea-query-derive and sea-query-attr.
I'm thinking we can deprecate sea-query-attr
and make sea-query-derive
to support struct.
In our example, we have... an Enum implemented Iden
and a struct to hold the data
https://github.com/SeaQL/sea-query/blob/c14705ac413ae0c00d6c90e9f22beadd92757c1e/examples/sqlx_mysql/src/main.rs#L264-L288
Ideally, we want to keep the name of Iden
Enum short, let say Character
. And the struct to be named differently maybe with a suffix, say CharacterData
.
#[derive(Iden)]
#[iden = "character"] // Override string representation of the table identifier, i.e. `Character::Table`
#[iden(enum_name = "Character")] // Specify name of `Iden` Enum, e.g. `Character::Table`, `Character::Id`
struct CharacterData {
id: i32,
#[iden = "uuid"] // Override string representation of a column identifier
uuid: Uuid,
character: String,
font_size: i32,
meta: Json,
decimal: Decimal,
big_decimal: BigDecimal,
created: NaiveDateTime,
}
Thoughts? Input wanted! @tyt2y3
#[derive(Iden)] #[iden = "character"] // Override string representation of the table identifier, i.e. `Character::Table` #[iden(enum_name = "Character")] // Specify name of `Iden` Enum, e.g. `Character::Table`, `Character::Id` struct CharacterData { id: i32, #[iden = "uuid"] // Override string representation of a column identifier uuid: Uuid, character: String, font_size: i32, meta: Json, decimal: Decimal, big_decimal: BigDecimal, created: NaiveDateTime, }
Looks good!
Might be better if the parameter style is more consistent:
#[derive(Iden)]
#[iden(table_name = "character", enum_name = "Character"]
...
#[iden(column_name = "uuid")]
Before the release of 0.29
is there a way to use a struct and the derive macro and change the name of the table?
Before the release of
0.29
is there a way to use a struct and the derive macro and change the name of the table?
Currently using a workaround for now:
#[derive(Iden)]
enum Table {
Tenants,
}
#[enum_def(suffix = "T")]
#[derive(Debug, PartialEq, sqlx::FromRow)]
struct Tenant { /*... */ }
Query::select().columns([TenantT::Code, TenantT::Title]).from(Table::Tenants)