Is it possible to change type of returned value with jsonBuildObject?
I struggle typing reponse schema in fastify cuz query returns
'wartosc'
as string
but actually it returns number
. Can't simply change zod reponse schema, cuz kysely types wartosc
in query is as string, and there is type mismatch anyway. (sorry for polish, ubiquitous language with polish domain masters)
is something like this possible (simmillar to type casting with raw sql sql<number>
? if not any tip how to handle this?
I would really like to put data in proper shape in sql to avoid extra mapping on backend codeSolution:Jump to solution
Data types | Kysely
When talking about data types in Kysely we need to make a distinction between the two kinds of types:
10 Replies
you can "lie" to kysely and use the
.$castTo
helper.
not that i advice doing that but if you have no other optionHey 👋
If at runtime you get a number, why is the type passed to kysely a string?
No idea why this mismatch happens. Db column has type
decimal
, it might be something with jsonBuildObject
helper which I guess treat/return values as string
Thanks, gonna check that tommorow👍
Can you share a kyse.link with
DB
interface and the given query?Sure, will post when I will be at desk later
https://kyse.link/GyVlE
response (without zod schema passed ofc)
Have you perhaps had time to take a look at it? I still struggle with it.
Solution
Data types | Kysely
When talking about data types in Kysely we need to make a distinction between the two kinds of types: