Billy Swedish
Billy Swedish
Explore posts from servers
KKysely
Created by Billy Swedish on 11/8/2024 in #help
Trouble with dynamically allocating table name via string
Hello! I am having some trouble trying to allocate a table name dynamically via a string. I used the sql.table command in my SelectFrom statement but no such luck. Passing in a direct string also does not work as hoped. I have read a few posts which indicate this is not possible, if not I can work around it, if it is however how might I achieve this?
const tableName: string = 'my_table_name'

await db
.selectFrom(sql.table(tableName))
.selectAll()
.execute();
const tableName: string = 'my_table_name'

await db
.selectFrom(sql.table(tableName))
.selectAll()
.execute();
// Ben
5 replies
KKysely
Created by Billy Swedish on 11/6/2024 in #help
Dynamic expression builder using raw SQL for WHERE column LIKE string% OR
Hello! We switched over to using Kysely in our project and it's going great so far! But I found a bit of a hitch with writing an old dynamic WHERE LIKE query. I need to get the results of file paths stored in our system that start with a directory path. Previously in pure SQL we would generate an expression like this:
SELECT * FROM tableName WHERE
file_path LIKE dirPath% OR
file_path LIKE otherDirPath% OR... etc.
SELECT * FROM tableName WHERE
file_path LIKE dirPath% OR
file_path LIKE otherDirPath% OR... etc.
I have tried with Kysely and it looks something like this which works just fine:
const result = await db
.selectFrom(tableName)
.selectAll()
.where((eb) => {
return eb.or(
dirPaths.map((dir) =>
sql<boolean>`file_path LIKE ${dir + '%'}`))
}).execute();
const result = await db
.selectFrom(tableName)
.selectAll()
.where((eb) => {
return eb.or(
dirPaths.map((dir) =>
sql<boolean>`file_path LIKE ${dir + '%'}`))
}).execute();
My question is though... originally I tried to generate an sql expression like the following:
const condition = dirPaths
.map((dir) => sql`${sql.identifier(['file_path'])} LIKE ${dir + '%'} COLLATE NOCASE`)
.reduce((acc, condition) => sql`${acc} OR ${condition}`);

db.selectFrom(tableName).selectAll().where(condition).execute();
const condition = dirPaths
.map((dir) => sql`${sql.identifier(['file_path'])} LIKE ${dir + '%'} COLLATE NOCASE`)
.reduce((acc, condition) => sql`${acc} OR ${condition}`);

db.selectFrom(tableName).selectAll().where(condition).execute();
I got an error of the following: Argument of type RawBuilder<unknown> is not assignable to parameter of type. Could you explain this? If I can get an sql template string set up it would make my life much easier moving forward knowing how to do this properly. Please let me know!
6 replies
TtRPC
Created by Billy Swedish on 8/4/2024 in #❓-help
tRPC and playing audio files on the frontend
Hello all! Reaching out to the community here to see if anyone can help. I am putting together an audio player component for my electron app and use tRPC for all backend communication. Specifically I am using the electron tRPC setup (https://github.com/jsonnull/electron-trpc) I was wondering if anyone has used something like the react h5 audio player and served audio files to it using tRPC yet or should I just go with an express server setup? Ideally I would like to avoid two comms systems but if needs must then I will go ahead. Cheers! // Ben
4 replies