How do I create a helper to be used on a select array of fields?

I want to create a toBool helper to convert from tinyint, however I'm lost on the type I need to pass to get autocomplete of the possible fields in the select
import { sql } from 'kysely'

export const toBool = (
field: string // what type should I add here?
) => {
return sql<boolean>`IF(${field} > 0, true, false)`
}
import { sql } from 'kysely'

export const toBool = (
field: string // what type should I add here?
) => {
return sql<boolean>`IF(${field} > 0, true, false)`
}
Solution:
I think checking equality to 0 would make this safer (and swapping true/false order), as tinyint can be negative.
Jump to solution
2 Replies
thelinuxlich
thelinuxlichOP15mo ago
I ended up with this implementation, am I correct?
import { sql, type ExpressionWrapper } from 'kysely'

export const toBool = <DB, TB extends keyof DB, T>(
field: ExpressionWrapper<DB, TB, T>
) => {
return sql<boolean>`IF(${field} > 0, true, false)`
}


// usage

select(eb => [toBool(eb.ref('field')).as('alias')])
import { sql, type ExpressionWrapper } from 'kysely'

export const toBool = <DB, TB extends keyof DB, T>(
field: ExpressionWrapper<DB, TB, T>
) => {
return sql<boolean>`IF(${field} > 0, true, false)`
}


// usage

select(eb => [toBool(eb.ref('field')).as('alias')])
Solution
Igal
Igal14mo ago
I think checking equality to 0 would make this safer (and swapping true/false order), as tinyint can be negative.
Want results from more Discord servers?
Add your server