sea-query icon indicating copy to clipboard operation
sea-query copied to clipboard

Allow convenience macros that impl Iden to support pluralization for table names.

Open solidiquis opened this issue 2 years ago • 7 comments

#[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 avatar Nov 27 '22 18:11 solidiquis

@solidiquis, hello, do you mean that table name should be users?

ikrivosheev avatar Nov 28 '22 10:11 ikrivosheev

I want to create derive-v2 macros where I merge sea-query-derive and sea-query-attr.

ikrivosheev avatar Dec 06 '22 11:12 ikrivosheev

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.

billy1624 avatar Dec 08 '22 03:12 billy1624

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

billy1624 avatar Dec 08 '22 04:12 billy1624

#[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")]

tyt2y3 avatar Dec 08 '22 06:12 tyt2y3

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?

samueljoli avatar Dec 24 '23 22:12 samueljoli

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)

jvliwanag avatar Jan 13 '24 02:01 jvliwanag