kysely-org / kysely

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

How to select with functions? #374

Closed TomMettam closed 1 year ago

TomMettam commented 1 year ago

I'm trying to figure out how to accomplish something like

SELECT UNIX_TIMESTAMP(slotWeeks.startDate) as slotWeekStart

I tried using an sql template but it seems not to be accepted by .select()

What's the way to accomplish this in Kysely?

koskimas commented 1 year ago

You need to provide an alias for the selection using the as method.

.select([
  'foo',
  'bar',
  sql<number>`UNIX_TIMESTAMP(slotWeeks.startDate)`.as('slotWeekStart')
])

From the select method's documentation:

Screenshot 2023-03-21 at 12 46 29