K
Kysely•9mo ago
sirhype

Converting a more complex query to Kysely

I wrote this query for my database, and I've been trying to convert it to use Kysely but can't seem to. I'd like to figure it out (rather than just use string SQL) The query:
SELECT
*,
(
SELECT
COUNT(*)
FROM
zamdle_clips AS c2
WHERE
c2.id < c1.id
AND c2.shahz_response IS NULL
) + 1 AS position_in_queue
FROM
zamdle_clips AS c1
WHERE
twitch_id = 'some_input_id'
ORDER BY
id;
SELECT
*,
(
SELECT
COUNT(*)
FROM
zamdle_clips AS c2
WHERE
c2.id < c1.id
AND c2.shahz_response IS NULL
) + 1 AS position_in_queue
FROM
zamdle_clips AS c1
WHERE
twitch_id = 'some_input_id'
ORDER BY
id;
What I've got so far (WIP)
const result = await db
.selectFrom("zamdle_clips")
.selectAll()
.where("twitch_id", "=", twitch_id)
.orderBy("id")
.select(({ selectFrom }) => [
selectFrom("zamdle_clips").selectAll().as("c1"),
selectFrom("zamdle_clips")
.select(({ fn }) => fn.countAll().as("count"))
.where("shahz_response", "is", null)
.as("position_in_queue"),
])
.execute();
const result = await db
.selectFrom("zamdle_clips")
.selectAll()
.where("twitch_id", "=", twitch_id)
.orderBy("id")
.select(({ selectFrom }) => [
selectFrom("zamdle_clips").selectAll().as("c1"),
selectFrom("zamdle_clips")
.select(({ fn }) => fn.countAll().as("count"))
.where("shahz_response", "is", null)
.as("position_in_queue"),
])
.execute();
Any help is appreciated 🙂
15 Replies
koskimas
koskimas•9mo ago
If you are absolutely sure position_in_queue can't be null, you can use this https://kyse.link/?p=s&i=uQMeXqMnIDdWbul02jGS
sirhype
sirhype•9mo ago
Thank you SO much, my biggest problems were that I didn't realize I could put the alias in selectFrom . Also not using whereRef instead of where
koskimas
koskimas•9mo ago
You're welcome! whereRef is equal to where('foo', '=', eb.ref('bar')) so you can use whichever you prefer.
sirhype
sirhype•9mo ago
gotcha! When I use $narrowType is that supposed to make the output of position_in_queue a number?
koskimas
koskimas•9mo ago
Kysely can't always correctly determine if something can be null or not. By default you get number | null for position_in_queue in this query. But since the subquery is using count, it can't return null. $narrowType is used to cast away the null $narrowType is still very type-safe. You can't accidentally change the type of a column. You can only narrow it. You also can't accidentally add a column that doesn't exist.
sirhype
sirhype•9mo ago
Ahhh I understand, for some reason it's giving me a string at the moment for position_in_queue. That's why I was a bit confused
koskimas
koskimas•9mo ago
$narrowType is only a type-level helper. It doesn't affect the runtime You're probably using postgres. Postgres returns strings for numbers that could (in theory) be larger than the maximum integer the javascript number can represent (2^53-1)
sirhype
sirhype•9mo ago
I'm using Planetscale which I believe uses MySQL
koskimas
koskimas•9mo ago
You can configure postgres to return numbers, but you need to do that for the pg driver Well the same thing applies. You need to configure the driver. Kysely never touches the data. You need to modify the types to match the underlying driver.
sirhype
sirhype•9mo ago
ah I see, thank you!
koskimas
koskimas•9mo ago
Most drivers have some way to make type transformations for cases like this
sirhype
sirhype•9mo ago
regardless, output type isn't too important. Thank you again so much
Igal
Igal•9mo ago
Hey 👋 The underlying planetscale driver used in the community dialect you're using has data transformation support https://github.com/planetscale/database-js#custom-type-casting-function
GitHub
GitHub - planetscale/database-js: A Fetch API-compatible PlanetScal...
A Fetch API-compatible PlanetScale database driver - GitHub - planetscale/database-js: A Fetch API-compatible PlanetScale database driver
Igal
Igal•9mo ago
GitHub
GitHub - depot/kysely-planetscale: A Kysely dialect for PlanetScale...
A Kysely dialect for PlanetScale Serverless. Contribute to depot/kysely-planetscale development by creating an account on GitHub.
Want results from more Discord servers?
Add your server
More Posts