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
2 Replies
koskimas
koskimas2w ago
You can't do that while having strict types. The types depend on the runtime value of tableName. You can do anything if you downcast your kysely instance to Kysely<any>. But then of course, the result types are just any
Billy Swedish
Billy SwedishOP2w ago
I thought not. We might try with Kysely<any> in later experiments. Ideally we would be able to make a generic select, upate or delete statement that we can use on multiple tables. For now we will live with the same statement for two tables. Will report back when we have tested with your suggestion
Want results from more Discord servers?
Add your server