Kysely not calling 'init' for Driver (?)
Hi all, preface this by saying I am starting out with Kysely and I might be making a dumb mistake 😄
I am getting the following error (had to cut off the last 2 lines bc Discord msg limit)
I've followed the stack trace to the screenshotted code, I believe that for some reason the
init
function is not getting called, so this.#pool
remains undefined. This happens when I do normal API calls but also in the health hook at the bottom of the code snippet.
Now, the interesting thing is that I have migrations running when the app starts, these work fine. They connect to the database and execute their queries (used the log
prop in the Kysely constructor to verify this). I'm not sure why other queries are throwing the ugly error though :(
Solution:Jump to solution
Okay my bad, this was a really dumb mistake...
I was destroying the Kysely instance after the migrations...
7 Replies
Weird.
Could you create a reproduction for this somehow?
I can try but it's very likely not easily reproducible, this is in a monorepo running in containers.. lots of moving parts :(
I'm happy to try and debug more though, if you could give me some pointers to look at?
I have no idea 😅 I haven't seen this bug happen before.
Did you check that
init
is in fact never called?I put a debugger breakpoint in the
init
and the acquireConnection
functions
When the app starts and the migrations run, it triggers both those breakpoints and it works as expected.
Then I trigger the health hook and the breakpoint in acquireConnection triggers, which shows this.#pool being undefined.Okay so I dunno if this helps but I tried making a second db instance inside the health hook and this is working :O
Could it be some weird Typescript scoping issue? The parts where it doesn't work (health and the api calls) are coming from another package in the monorepo
(sorry for spam :p)
I tried making a
async function getDb()
function that returns a cached Kysely instance, but that had the same result as before :(Solution
Okay my bad, this was a really dumb mistake...
I was destroying the Kysely instance after the migrations