sirhype
sirhype
Explore posts from servers
KKysely
Created by sirhype on 2/13/2024 in #help
case when column = another column
I have a semi-complex query, and I'm trying to use a case() that includes a when() where the two values are columns (aka the two columns have the same value). It doesn't seem to be supported, however in theory this should work. Any idea how I can make it work with Kysely? The .when("shahz_response", "=", "rank") is where I'm attempting this, but this isn't working
const result = await db
.selectFrom("zamdle_clips")
.select((eb) => [
eb.fn
.sum((e) =>
e
.case()
.when("shahz_response", "=", "rank")
.then(e.case().when("id", ">=", startZamdle).then(1).else(0).end())
.else(0)
.end()
)
.as("correctSeason"),
eb.fn
.sum((e) =>
e.case().when("shahz_response", "=", "rank").then(1).else(0).end()
)
.as("correctAllTime"),
eb.fn
.sum((e) =>
e
.case()
.when("shahz_response", "is not", null)
.then(e.case().when("id", ">=", startZamdle).then(1).else(0).end())
.else(0)
.end()
)
.as("totalSeason"),
eb.fn
.sum((e) =>
e.case().when("shahz_response", "is not", null).then(1).else(0).end()
)
.as("totalAllTime"),
])
.executeTakeFirstOrThrow();
const result = await db
.selectFrom("zamdle_clips")
.select((eb) => [
eb.fn
.sum((e) =>
e
.case()
.when("shahz_response", "=", "rank")
.then(e.case().when("id", ">=", startZamdle).then(1).else(0).end())
.else(0)
.end()
)
.as("correctSeason"),
eb.fn
.sum((e) =>
e.case().when("shahz_response", "=", "rank").then(1).else(0).end()
)
.as("correctAllTime"),
eb.fn
.sum((e) =>
e
.case()
.when("shahz_response", "is not", null)
.then(e.case().when("id", ">=", startZamdle).then(1).else(0).end())
.else(0)
.end()
)
.as("totalSeason"),
eb.fn
.sum((e) =>
e.case().when("shahz_response", "is not", null).then(1).else(0).end()
)
.as("totalAllTime"),
])
.executeTakeFirstOrThrow();
4 replies
CDCloudflare Developers
Created by sirhype on 12/31/2023 in #pages-help
Cannot find module with vite + react + typescript + yarn build
I'm going crazy trying to figure out the reasoning I'm getting
src/App.tsx(1,53): error TS2307: Cannot find module 'react-router-dom' or its corresponding type declarations.
src/App.tsx(1,53): error TS2307: Cannot find module 'react-router-dom' or its corresponding type declarations.
along with a billion other Cannot find module errors with my new Vite project. It's a vite + react + typescript project that's almost directly out of the box. When I've done this with other projects it worked, so I'm confused where I might be going wrong. Anyone seen this before? Build command is yarn build, dist is the output directory. Versions are [email protected], [email protected] tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
.eslint.cjs
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:react-hooks/recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime",
],
ignorePatterns: ["dist", ".eslintrc.cjs"],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
project: ["./tsconfig.json", "./tsconfig.node.json"],
tsconfigRootDir: __dirname,
},
plugins: ["react-refresh"],
rules: {
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
},
};
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:react-hooks/recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime",
],
ignorePatterns: ["dist", ".eslintrc.cjs"],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
project: ["./tsconfig.json", "./tsconfig.node.json"],
tsconfigRootDir: __dirname,
},
plugins: ["react-refresh"],
rules: {
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
},
};
2 replies
KKysely
Created by sirhype on 12/17/2023 in #help
case() return boolean (castTo doesn't work?)
Hiya! I'm trying to determine if what I'm doing is currently possible I have the following expression
const result = await db
.selectFrom("zamdle_submissions")
.selectAll()
.leftJoin(
"zamdle_clips",
"zamdle_clips.twitch_id",
"zamdle_submissions.twitch_id"
)
.select((eb) =>
eb
.case()
.when("zamdle_clips.twitch_id", "is not", null)
.then(true)
.else(false)
.end()
.as("has_matching_clip")
)
.execute();
const result = await db
.selectFrom("zamdle_submissions")
.selectAll()
.leftJoin(
"zamdle_clips",
"zamdle_clips.twitch_id",
"zamdle_submissions.twitch_id"
)
.select((eb) =>
eb
.case()
.when("zamdle_clips.twitch_id", "is not", null)
.then(true)
.else(false)
.end()
.as("has_matching_clip")
)
.execute();
Currently this will return:
"has_matching_clip": "1"
"has_matching_clip": "1"
which I can work with, however I'm trying to have it return as true/false, or at the very least a number. I've tried using castTo after .end() but it doesn't seem to work. Any advice?
2 replies
KKysely
Created by sirhype on 9/27/2023 in #help
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 🙂
21 replies
KKysely
Created by sirhype on 3/26/2023 in #help
Types for use with leftJoin
Hiya! I'm attempting to do a leftJoin and return the result:
const result = await db
.selectFrom("accounts")
.leftJoin("applications", "accounts.id", "applications.accountId")
.where("accounts.sessionId", "=", sessionId)
.executeTakeFirst();
const result = await db
.selectFrom("accounts")
.leftJoin("applications", "accounts.id", "applications.accountId")
.where("accounts.sessionId", "=", sessionId)
.executeTakeFirst();
I'd like to have the result be a type, like Selectable<Account & Application> but I'm not sure if this is supported or what the proper way to do this is. Any help would be appreciated, thank you!
3 replies
CDCloudflare Developers
Created by sirhype on 3/19/2023 in #workers-help
Worker with Slash-Up Discord Slash Commands Failing
I am currently using slash-create with Cloudflare Workers to integrate Discord Slash commands. I'm getting the below error when I attempt to add the integration on Discord's side.
A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
✘ [ERROR] Uncaught (in response) Error: The script will never generate a response.
A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
✘ [ERROR] Uncaught (in response) Error: The script will never generate a response.
This all works in local mode, but not on the worker itself (or outside of local mode). I've made some modifications to the boilerplate to get it to run properly. But I never thought to check outside of local mode when I was originally setting up. Happy to share my project if anyone wants to take a look 🙂 Source is here: https://github.com/TristanWiley/procity-slash-commands Relevant code is most likely in src/shim/servers/cfworker.ts and src/shim/creator.ts. This is where I've been looking. Unfortunately this is code I did not write and are a part of the library itself, however probably outdated a bit.
11 replies