K
Kysely•12mo ago
bombillazo

Ad hoc custom column type

Hello, we have a table that on the database level, the column is a string type, but on the typescript level, the type is a Type literal union. Is there any way we can ad hoc type a column to a type we know during select queries and insert statements?
Solution:
Just give that type for the column in the table interface.
Jump to solution
6 Replies
Igal
Igal•12mo ago
Hey 👋 Can you provide an example of what you mean?
bombillazo
bombillazoOP•12mo ago
Lest say I have this:
type X = 'foo' | 'bar' | 'baz';

await db
.selectFrom('my_table')
.select(['a','b','c')
.execute();
type X = 'foo' | 'bar' | 'baz';

await db
.selectFrom('my_table')
.select(['a','b','c')
.execute();
At the DB level, b is a string, but I want to type is as type X, how can I do that in my select query?
Solution
koskimas
koskimas•12mo ago
Just give that type for the column in the table interface.
bombillazo
bombillazoOP•12mo ago
We aare using kysely-codegen, so there is no way to do this like one could do with a helper function like selectFrom('my_table').select<MyType>(['b'])...?
koskimas
koskimas•12mo ago
Igal
Igal•12mo ago
Look for overrides in kysely-codegen, if its not supported, submit an issue if one doesn't exist. If you're on postgres, try kanel-kysely
Want results from more Discord servers?
Add your server