Skip to content

Conversation

@tyt2y3
Copy link
Member

@tyt2y3 tyt2y3 commented Nov 5, 2025

This is still WIP but I want everyone to see this earlier.

So basically after some research I figured we can have typed columns without the complexity of "one struct for every column", and this solution solves three things:

  1. Make column methods type safe
  2. No longer have to use CamelCase for column identifier
  3. Make the shorthand for cake::Entity more useful

We would generate the following structs, columns share the same type so it's fine if Entity has 100 Columns:

pub struct TypedColumn {
    pub id: NumericColumn<Entity>,
    pub name: StringColumn<Entity>,
    pub price: NumericColumn<Entity>,
}

impl Entity {
    pub const COLUMN: TypedColumn = TypedColumn {
        id: NumericColumn(Column::Id),
        name: StringColumn(Column::Name),
        price: NumericColumn(Column::Price),
    };
}
// before
let cakes = cake::Entity::find()
    .filter(cake::Column::Name.like("Ch%"))
    .all(db)
    .await?;

// after
let cakes = Cake::find()
    .filter(Cake::COLUMN.name.like("Ch%"))
    .all(db)
    .await?;

The following would not compile:

Cake::find()
    .filter(Cake::COLUMN.id.like("Ch")) // method cannot be called on `NumericColumn<Entity>`
    .filter(Cake::COLUMN.name.like(2))  // the trait `From<{integer}>` is not implemented for `String`
    .order_by_desc(Cake::COLUMN.name)
    .one(db)
    .await?

This also means that contains for String would not collide with array_contains.

@tyt2y3 tyt2y3 requested review from Expurple and Huliiiiii November 5, 2025 12:55
@tyt2y3 tyt2y3 force-pushed the typed-column branch 3 times, most recently from 305b721 to e654503 Compare November 6, 2025 01:52
@tyt2y3
Copy link
Member Author

tyt2y3 commented Nov 6, 2025

Everything will pass after I bump sea-query

Comment on lines +185 to +193
let found = Entity::find()
.filter(
Entity::COLUMN
.teas
.overlap(vec![Tea::BreakfastTea, Tea::AfternoonTea]),
)
.order_by_asc(Entity::COLUMN.id)
.all(db)
.await?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can finally have this

@tyt2y3 tyt2y3 marked this pull request as ready for review November 7, 2025 20:07
@tyt2y3 tyt2y3 merged commit c03f4ed into master Nov 8, 2025
7 of 26 checks passed
@tyt2y3 tyt2y3 deleted the typed-column branch November 8, 2025 00:58
@tyt2y3
Copy link
Member Author

tyt2y3 commented Nov 8, 2025

release early, release often

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants