K
Kysely5mo ago
tzezar

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)
jsonBuildObject({
id: eb.ref('domyslnaStawkaVatSprzedazy.id'),
nazwa: eb.ref('domyslnaStawkaVatSprzedazy.nazwa'),
wartosc: eb.ref('domyslnaStawkaVatSprzedazy.wartosc'),
}).as('domyslnaStawkaVatSprzedazy'),
jsonBuildObject({
id: eb.ref('domyslnaStawkaVatSprzedazy.id'),
nazwa: eb.ref('domyslnaStawkaVatSprzedazy.nazwa'),
wartosc: eb.ref('domyslnaStawkaVatSprzedazy.wartosc'),
}).as('domyslnaStawkaVatSprzedazy'),
is something like this possible (simmillar to type casting with raw sql sql<number>? if not any tip how to handle this?
jsonBuildObject({
id: eb.ref('domyslnaStawkaVatSprzedazy.id'),
nazwa: eb.ref('domyslnaStawkaVatSprzedazy.nazwa'),
wartosc: eb.ref<number>('domyslnaStawkaVatSprzedazy.wartosc'),
}).as('domyslnaStawkaVatSprzedazy'),
jsonBuildObject({
id: eb.ref('domyslnaStawkaVatSprzedazy.id'),
nazwa: eb.ref('domyslnaStawkaVatSprzedazy.nazwa'),
wartosc: eb.ref<number>('domyslnaStawkaVatSprzedazy.wartosc'),
}).as('domyslnaStawkaVatSprzedazy'),
I would really like to put data in proper shape in sql to avoid extra mapping on backend code
Solution:
Data types | Kysely
When talking about data types in Kysely we need to make a distinction between the two kinds of types:
Jump to solution
10 Replies
decho
decho5mo ago
you can "lie" to kysely and use the .$castTo helper.
jsonBuildObject({
id: eb.ref('domyslnaStawkaVatSprzedazy.id'),
nazwa: eb.ref('domyslnaStawkaVatSprzedazy.nazwa'),
wartosc: eb.ref('domyslnaStawkaVatSprzedazy.wartosc').$castTo<number>(),
}).as('domyslnaStawkaVatSprzedazy'),
jsonBuildObject({
id: eb.ref('domyslnaStawkaVatSprzedazy.id'),
nazwa: eb.ref('domyslnaStawkaVatSprzedazy.nazwa'),
wartosc: eb.ref('domyslnaStawkaVatSprzedazy.wartosc').$castTo<number>(),
}).as('domyslnaStawkaVatSprzedazy'),
not that i advice doing that but if you have no other option
Igal
Igal5mo ago
Hey 👋 If at runtime you get a number, why is the type passed to kysely a string?
tzezar
tzezarOP5mo ago
No idea why this mismatch happens. Db column has typedecimal, it might be something with jsonBuildObject helper which I guess treat/return values as string Thanks, gonna check that tommorow
decho
decho5mo ago
👍
Igal
Igal5mo ago
Can you share a kyse.link with DB interface and the given query?
tzezar
tzezarOP5mo ago
Sure, will post when I will be at desk later https://kyse.link/GyVlE
tzezar
tzezarOP5mo ago
No description
tzezar
tzezarOP5mo ago
response (without zod schema passed ofc)
No description
tzezar
tzezarOP4mo ago
Have you perhaps had time to take a look at it? I still struggle with it.
Solution
koskimas
koskimas4mo ago
Data types | Kysely
When talking about data types in Kysely we need to make a distinction between the two kinds of types:
Want results from more Discord servers?
Add your server