K
Kysely•12mo ago
Catalysm

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)
Cannot read properties of undefined (reading 'connect') | {"stack":"TypeError: Cannot read properties of undefined (reading 'connect')\n at PostgresDriver.acquireConnection (file:///app/node_modules/kysely/dist/esm/dialect/postgres/postgres-driver.js:19:41)\n at RuntimeDriver.acquireConnection (file:///app/node_modules/kysely/dist/esm/driver/runtime-driver.js:29:47)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async DefaultConnectionProvider.provideConnection (file:///app/node_modules/kysely/dist/esm/driver/default-connection-provider.js:8:28)\n at async DefaultQueryExecutor.executeQuery (file:///app/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:34:16)\n
Cannot read properties of undefined (reading 'connect') | {"stack":"TypeError: Cannot read properties of undefined (reading 'connect')\n at PostgresDriver.acquireConnection (file:///app/node_modules/kysely/dist/esm/dialect/postgres/postgres-driver.js:19:41)\n at RuntimeDriver.acquireConnection (file:///app/node_modules/kysely/dist/esm/driver/runtime-driver.js:29:47)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async DefaultConnectionProvider.provideConnection (file:///app/node_modules/kysely/dist/esm/driver/default-connection-provider.js:8:28)\n at async DefaultQueryExecutor.executeQuery (file:///app/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:34:16)\n
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 :(
const dialect = new PostgresDialect({
pool: new pg.Pool({
database: config.get('database.database'),
password: config.get('database.password'),
host: config.get('database.host'),
user: config.get('database.user'),
port: config.get('database.port'),
max: config.get('database.poolMax'),
ssl: sslOptions,
}),
});

export const db = new Kysely<Database>({
dialect,
});

health.registerHook('database', async () => {
await sql`SELECT 1`.execute(db);
return true;
});
const dialect = new PostgresDialect({
pool: new pg.Pool({
database: config.get('database.database'),
password: config.get('database.password'),
host: config.get('database.host'),
user: config.get('database.user'),
port: config.get('database.port'),
max: config.get('database.poolMax'),
ssl: sslOptions,
}),
});

export const db = new Kysely<Database>({
dialect,
});

health.registerHook('database', async () => {
await sql`SELECT 1`.execute(db);
return true;
});
Solution:
Okay my bad, this was a really dumb mistake... I was destroying the Kysely instance after the migrations...
Jump to solution
7 Replies
koskimas
koskimas•12mo ago
Weird. Could you create a reproduction for this somehow?
Catalysm
Catalysm•12mo ago
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?
koskimas
koskimas•12mo ago
I have no idea 😅 I haven't seen this bug happen before. Did you check that init is in fact never called?
Catalysm
Catalysm•12mo ago
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.
Catalysm
Catalysm•12mo ago
Okay so I dunno if this helps but I tried making a second db instance inside the health hook and this is working :O
Catalysm
Catalysm•12mo ago
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
Catalysm
Catalysm•12mo ago
Okay my bad, this was a really dumb mistake... I was destroying the Kysely instance after the migrations
Want results from more Discord servers?
Add your server
More Posts