K
Kysely12mo ago
djMax

Generic function to stream a table

I am trying to write a generic function that will take a kysely connection, a table name, a column name and a value for that column and return a streaming select. I can't figure out the "foward type declarations" to make it all work, and I think it's because of some of the fancy bits that Kysely does for selectFrom("table as alias") that I can't untangle. Playground link here: https://kyse.link/?p=s&i=n4OdlKPOjCkNULp6niLU . As you can see, it does WORK, but the red squigglies mean TS thinks it's wrong. Any idea how to declare this properly? Thanks!
Solution:
You can't really get something like this to work without getting rid of types inside the function. For example like this https://kyse.link/?p=s&i=pMUz4CfVVRHR6Sc2wgcN The issue is that, inside the function, the types Schema, T etc. are really wide (in this case completely unbounded). There's no information for Kysely to work with. The types don't work unless you give it an actual bounded schema. You should avoid generic functions like this when using Kysely. Super strict and generic just don't work well together....
Jump to solution
2 Replies
Solution
koskimas
koskimas12mo ago
You can't really get something like this to work without getting rid of types inside the function. For example like this https://kyse.link/?p=s&i=pMUz4CfVVRHR6Sc2wgcN The issue is that, inside the function, the types Schema, T etc. are really wide (in this case completely unbounded). There's no information for Kysely to work with. The types don't work unless you give it an actual bounded schema. You should avoid generic functions like this when using Kysely. Super strict and generic just don't work well together.
djMax
djMaxOP11mo ago
This is where I ended up: https://github.com/sesamecare/to-clickhouse/blob/main/src/dbs/kysely.ts#L21 The most important goal was to get autocomplete on the parameters, and that seems to be working, but as you say there have to be some non-type-safe things inside the function to maintain sanity.
GitHub
to-clickhouse/src/dbs/kysely.ts at main · sesamecare/to-clickhouse
Synchronize data between a relational store such as Postgres and a Clickhouse database - sesamecare/to-clickhouse
Want results from more Discord servers?
Add your server