kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
9.9k stars 249 forks source link

ColumnType for where clause #930

Closed ysshir closed 3 months ago

ysshir commented 3 months ago

Hi, I am trying to set column type, which is used only for where clause. not for select.

Initially, I wrote it like this. but I encountered a type error on where clause.

export interface SomeTable {
  id: Generated<number>
  date: Date
  // Other properties...
}

const result = await db.selectFrom('SomeTable')
                         .selectAll()
                         .where('date', '=', '2024-01-01') // type error
                         .limit(1)
                         .executeTakeFirst();

So I changed date: Date to date: ColumnType<Date | string>. It works on where clause, but the result type is Date | string. Consequently, result.date.getTime() doesn't working this time.

In my timezone "Asia/Tokyo", new Date('2024-01-01') will be converted to 2024-01-01 09:00:00. Therefore, I would like to search in string, but get the result in Date.

Do you have any ideas for this? thanks.

koskimas commented 3 months ago

It's best to just make the db return strings. JavaScript dates suck.

https://kysely.dev/docs/recipes/data-types