Kysely

K

Kysely

First came Kysely, the type-safe query builder for TypeScript, then came people asking questions, then came the Discord server.Join if you like Kysely or have questions, or both.

Join

help

query-showcase

announcements

Joining multiple separate CTE's

I am working on a complex query. I can get it to run efficiently using multiple CTE's(Atleast in RAW SQL) but I want to port all this over to Kysely due to the typesafety. What I want is something like this: const cteOne = kyselyDb...
Solution:
conditionally added CTEs are not supported - too much type complexity involved. you could union with a subquery conditionally tho, as the result shape is already established, and union can be added to existing builder instances, outside of chain (but also with $call)....

Is this a correct situation to use $castTo (jsonArrayFrom + Left Join)

```ts // snippet from main query: jsonArrayFrom( eb .selectFrom('address')...

Inconsistent Typing of NUMERIC Fields with jsonBuildObject

https://kyse.link/wxJiK I came across an issue on GitHub (link) where JSON fields in PostgreSQL aren't automatically typed correctly due to everything being returned as a string. This means that when using jsonObjectFrom, the returned type is string, rather than the expected data type like Date. What's puzzling is that this behavior isn't consistent across all data types. For instance, NUMERIC values at the root level are returned as strings (which is fine), but when those same values appear inside a nested JSON object, their type is inferred as number instead. I would prefer to have NUMERIC values inside JSON objects returned as string to keep it consistent with how they are handled at the root level....
Solution:
So, you either need those explicit casts, or allow the DB to cast the value to JSON number and fix your types.

AggregateFunctionBuilder inside a SUM

Hi there, I'm trying to build
SUM(CASE WHEN IsNoShow = 0 AND IsDNA = 0 THEN 1 ELSE 0 END)
SUM(CASE WHEN IsNoShow = 0 AND IsDNA = 0 THEN 1 ELSE 0 END)
...
Solution:
Here you go https://kyse.link/X1Vxc Our types are a bit too strict and give the type 100 instead of number for eb.lit(100)...

sql literal string in binary expression builder

Hi, I want the following: ```sql SELECT sum("ChargeAmount" * "1.0") AS "TotalChargeAmount" FROM...
Solution:
You definitely don't want "1.0" since that means you're referencing a column named 1.0

Query result type mismatch

For unknown to me reason kysely adds categoryId to nested object built with jsonObjectFrom or maybe it does not take related table, but copies selectFrom table. Why does it happen? https://kyse.link/0sjoX...
Solution:
Use selectAll('product_category') instead of selectAll() in the subquery
No description

Getting the underlying connection pool/client from a kysely wrapper

Hi guys, I have a migration script that takes a raw PgClient object. Was wondering if it would be possible to extract said object from the Kysely<any> that is passed into the migration functions (up down) thanks!...

Dynamic callbackURL on login

Hi guys, I wonder if it's possible to have a dynamic Dynamic callbackURL on login if the user is admin or not? Have a nice day....

Replicate ORM results

Hi guys, I wanted this result: `findPersonsWithPets: [...
Solution:
Hey 👋 Have you read https://kysely.dev/docs/recipes/relations ?...

postgres helper wrapping to_json referencing CTE?

is there a way to create a type safe postgres helper (recordToJson) that is similar to jsonObjectFrom, but that references an existing CTE. in essence i want to be able to do something like this ```typescript db .selectFrom('foo')...
Solution:
It's as simple as this https://kyse.link/t2h2a