Typing casting from JSON_OBJECT()

Im using mysql / planetscale. When I select a field that is a boolean (tinyint in database) drizzle returns a boolean. However when selecting that field inside of a JSON_OBJECT, it will return as a tinyint. Is there any method I'm missing to cast this as a boolean inside the query, or will I just have to do this after the query execution?
4 Replies
Angelelz
Angelelz10mo ago
Can you show the query that you're trying to run?
wheresthegold_
wheresthegold_OP10mo ago
db.select({foobar: sql`JSON_ARRAYAGG(JSON_OBJECT('bool_field', ${t.bool_field}))`}).from(t)
db.select({foobar: sql`JSON_ARRAYAGG(JSON_OBJECT('bool_field', ${t.bool_field}))`}).from(t)
Psuedo code sorry. For this bool field it will return for bool_field "1" or "0" compared to
db.select({bool_field: t.bool_field}).from(t)
db.select({bool_field: t.bool_field}).from(t)
which returns a true / false It's not a major issue I guess.. can just cast later in my code - just wondering if there's a method for casting inside the query that I missed.
Angelelz
Angelelz10mo ago
You can use the mapWith method in the sql operator You pass a function that will get called for each row in that field
baronnoraz
baronnoraz10mo ago
FWIW - I wouldn't expect/want that to behave differently. Since it's JSON, there's no way to know if {xyz: 0} should mean xyz = false or if it should be 0.
Want results from more Discord servers?
Add your server