K
Kyselyā€¢17mo ago
cheekybuddha

Typescript Error when creating MySQL db pool

Hi Everyone, I'm getting the following error when creating a database:
kysely Type '() => Promise<Pool>' is not assignable to type 'MysqlPool | (() => Promise<MysqlPool>)'
kysely Type '() => Promise<Pool>' is not assignable to type 'MysqlPool | (() => Promise<MysqlPool>)'
I'm using the following code:
// db.ts
import {
Kysely,
MysqlDialect,
} from 'kysely'
import { createPool } from 'mysql2'
import {
DB_HOST,
DB_PORT,
DB_USER,
DB_PASS,
DB_NAME
} from '$env/static/private'
import type { DB } from 'kysely-codegen'

export const mysqlPoolOpts = {
database: DB_NAME,
host: DB_HOST,
port: parseInt(DB_PORT),
user: DB_USER,
password: DB_PASS,
waitForConnections: true,
connectionLimit: 10,
// maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit`
// idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000
queueLimit: 0
}

export const db = new Kysely<DB>({
dialect: new MysqlDialect({
pool: async () => createPool(mysqlPoolOpts)
// ^^ -- error here
})
})
// db.ts
import {
Kysely,
MysqlDialect,
} from 'kysely'
import { createPool } from 'mysql2'
import {
DB_HOST,
DB_PORT,
DB_USER,
DB_PASS,
DB_NAME
} from '$env/static/private'
import type { DB } from 'kysely-codegen'

export const mysqlPoolOpts = {
database: DB_NAME,
host: DB_HOST,
port: parseInt(DB_PORT),
user: DB_USER,
password: DB_PASS,
waitForConnections: true,
connectionLimit: 10,
// maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit`
// idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000
queueLimit: 0
}

export const db = new Kysely<DB>({
dialect: new MysqlDialect({
pool: async () => createPool(mysqlPoolOpts)
// ^^ -- error here
})
})
(cont'd in following message ...)
11 Replies
cheekybuddha
cheekybuddhaOPā€¢17mo ago
My dependencies are:
$ pnpm ls --depth=0
Legend: production dependency, optional only, dev only

[email protected] /home/dm/Documents/websites/thdt_admin (PRIVATE)

dependencies:
@lucia-auth/adapter-mysql 2.0.0
kysely 0.26.1
lucia 2.0.0
mysql2 3.6.0

devDependencies:
@csstools/postcss-global-data 2.0.1 eslint 8.46.0 postcss-preset-env 9.1.1 sveltekit-superforms 1.5.0
@sveltejs/adapter-auto 2.1.0 eslint-config-prettier 8.10.0 prettier 3.0.1 tslib 2.6.1
@sveltejs/kit 1.22.4 eslint-plugin-svelte 2.32.4 prettier-plugin-svelte 3.0.3 typescript 5.1.6
@typescript-eslint/eslint-plugin 5.62.0 kysely-codegen 0.10.1 svelte 4.1.2 vite 4.4.9
@typescript-eslint/parser 5.62.0 postcss 8.4.27 svelte-check 3.4.6 zod 3.21.4
autoprefixer 10.4.14 postcss-csso 6.0.1 svelte-preprocess 5.0.4
$ pnpm ls --depth=0
Legend: production dependency, optional only, dev only

[email protected] /home/dm/Documents/websites/thdt_admin (PRIVATE)

dependencies:
@lucia-auth/adapter-mysql 2.0.0
kysely 0.26.1
lucia 2.0.0
mysql2 3.6.0

devDependencies:
@csstools/postcss-global-data 2.0.1 eslint 8.46.0 postcss-preset-env 9.1.1 sveltekit-superforms 1.5.0
@sveltejs/adapter-auto 2.1.0 eslint-config-prettier 8.10.0 prettier 3.0.1 tslib 2.6.1
@sveltejs/kit 1.22.4 eslint-plugin-svelte 2.32.4 prettier-plugin-svelte 3.0.3 typescript 5.1.6
@typescript-eslint/eslint-plugin 5.62.0 kysely-codegen 0.10.1 svelte 4.1.2 vite 4.4.9
@typescript-eslint/parser 5.62.0 postcss 8.4.27 svelte-check 3.4.6 zod 3.21.4
autoprefixer 10.4.14 postcss-csso 6.0.1 svelte-preprocess 5.0.4
I created the DB types using kysely-codegen:
$ npx kysely-codegen
ā€¢ Loaded environment variables from .env file.
ā€¢ No dialect specified. Assuming 'mysql'.
ā€¢ Introspecting database...
āœ“ Introspected 29 tables and generated ./node_modules/kysely-codegen/dist/db.d.ts in 408ms.
$ npx kysely-codegen
ā€¢ Loaded environment variables from .env file.
ā€¢ No dialect specified. Assuming 'mysql'.
ā€¢ Introspecting database...
āœ“ Introspected 29 tables and generated ./node_modules/kysely-codegen/dist/db.d.ts in 408ms.
I'm still very new with typescript - how can I get the type MysqlPool to map to Pool ? Or am I doing something else wrong? TIA for any assistance, d
Igal
Igalā€¢17mo ago
Hey šŸ‘‹ Can you share your tsconfig.json? can you share the entire typescript error?
cheekybuddha
cheekybuddhaOPā€¢17mo ago
Sure!
// tsconfig.json
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
}
// tsconfig.json
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
}
Full error:
Type '() => Promise<Pool>' is not assignable to type 'MysqlPool | (() => Promise<MysqlPool>)'.
Type '() => Promise<Pool>' is not assignable to type '() => Promise<MysqlPool>'.
Type 'Promise<Pool>' is not assignable to type 'Promise<MysqlPool>'.
Type 'Pool' is not assignable to type 'MysqlPool'.
Types of property 'getConnection' are incompatible.
Type '(callback: (err: any, connection: PoolConnection) => any) => void' is not assignable to type '(callback: (error: unknown, connection: MysqlPoolConnection) => void) => void'.
Types of parameters 'callback' and 'callback' are incompatible.
Types of parameters 'connection' and 'connection' are incompatible.
Property 'query' is missing in type 'PoolConnection' but required in type 'MysqlPoolConnection'.ts(2322)
mysql-dialect-config.d.ts(34, 5): 'query' is declared here.
mysql-dialect-config.d.ts(15, 5): The expected type comes from property 'pool' which is declared here on type 'MysqlDialectConfig'
(property) MysqlDialectConfig.pool: MysqlPool | (() => Promise<MysqlPool>)
A mysql2 Pool instance or a function that returns one.

If a function is provided, it's called once when the first query is executed.

https://github.com/sidorares/node-mysql2#using-connection-pools
Type '() => Promise<Pool>' is not assignable to type 'MysqlPool | (() => Promise<MysqlPool>)'.
Type '() => Promise<Pool>' is not assignable to type '() => Promise<MysqlPool>'.
Type 'Promise<Pool>' is not assignable to type 'Promise<MysqlPool>'.
Type 'Pool' is not assignable to type 'MysqlPool'.
Types of property 'getConnection' are incompatible.
Type '(callback: (err: any, connection: PoolConnection) => any) => void' is not assignable to type '(callback: (error: unknown, connection: MysqlPoolConnection) => void) => void'.
Types of parameters 'callback' and 'callback' are incompatible.
Types of parameters 'connection' and 'connection' are incompatible.
Property 'query' is missing in type 'PoolConnection' but required in type 'MysqlPoolConnection'.ts(2322)
mysql-dialect-config.d.ts(34, 5): 'query' is declared here.
mysql-dialect-config.d.ts(15, 5): The expected type comes from property 'pool' which is declared here on type 'MysqlDialectConfig'
(property) MysqlDialectConfig.pool: MysqlPool | (() => Promise<MysqlPool>)
A mysql2 Pool instance or a function that returns one.

If a function is provided, it's called once when the first query is executed.

https://github.com/sidorares/node-mysql2#using-connection-pools
// ./.svelte-kit/tsconfig.json
{
"compilerOptions": {
"paths": {
"$lib": [
"../src/lib"
],
"$lib/*": [
"../src/lib/*"
]
},
"rootDirs": [
"..",
"./types"
],
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"preserveValueImports": true,
"lib": [
"esnext",
"DOM",
"DOM.Iterable"
],
"moduleResolution": "node",
"module": "esnext",
"target": "esnext",
"ignoreDeprecations": "5.0"
},
"include": [
"ambient.d.ts",
"./types/**/$types.d.ts",
"../vite.config.ts",
"../src/**/*.js",
"../src/**/*.ts",
"../src/**/*.svelte",
"../tests/**/*.js",
"../tests/**/*.ts",
"../tests/**/*.svelte"
],
"exclude": [
"../node_modules/**",
"./[!ambient.d.ts]**",
"../src/service-worker.js",
"../src/service-worker.ts",
"../src/service-worker.d.ts"
]
}
// ./.svelte-kit/tsconfig.json
{
"compilerOptions": {
"paths": {
"$lib": [
"../src/lib"
],
"$lib/*": [
"../src/lib/*"
]
},
"rootDirs": [
"..",
"./types"
],
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"preserveValueImports": true,
"lib": [
"esnext",
"DOM",
"DOM.Iterable"
],
"moduleResolution": "node",
"module": "esnext",
"target": "esnext",
"ignoreDeprecations": "5.0"
},
"include": [
"ambient.d.ts",
"./types/**/$types.d.ts",
"../vite.config.ts",
"../src/**/*.js",
"../src/**/*.ts",
"../src/**/*.svelte",
"../tests/**/*.js",
"../tests/**/*.ts",
"../tests/**/*.svelte"
],
"exclude": [
"../node_modules/**",
"./[!ambient.d.ts]**",
"../src/service-worker.js",
"../src/service-worker.ts",
"../src/service-worker.d.ts"
]
}
cheekybuddha
cheekybuddhaOPā€¢17mo ago
Don't you love it when you search for the solution to a problem for a couple of days and find the answer immediately after posting asking for assistance!! šŸ˜– šŸ˜³ I have found the answer here: https://lucia-auth.com/guidebook/kysely Turns out that createPool() does not return the pool - you have to access its pool property:
export const db = new Kysely<DB>({
dialect: new MysqlDialect({
pool: async () => createPool(mysqlPoolOpts).pool
// ^^^^^ reference .pool here!!!
})
})
export const db = new Kysely<DB>({
dialect: new MysqlDialect({
pool: async () => createPool(mysqlPoolOpts).pool
// ^^^^^ reference .pool here!!!
})
})
Thanks for looking in šŸ‘
Igal
Igalā€¢17mo ago
hmm mysql2 might have introduced a breaking change there need to investigate
cheekybuddha
cheekybuddhaOPā€¢17mo ago
OK, keep us posted - will be interesting to see what you discover. Thanks for this, and Kysely!! šŸ‘ Actually, something is strange - returning .pool gets rid of the error, but it is not listed as a property in the intellisense. Like I said, I'm still new to typescript, so I don't know whether this is expected. Tried to add screenshots, but it failed. Will try again later.
Igal
Igalā€¢17mo ago
thank @koskimas šŸ‘ˆšŸ»
Igal
Igalā€¢17mo ago
Your example doesn't error in TypeScript playground https://tsplay.dev/w8659m
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Igal
Igalā€¢17mo ago
I have a feeling svelte kit not having skipLibCheck: true in its tsconfig.json causes this They are breaking things after a LOT of time of inactivity over at mysql2
Igal
Igalā€¢17mo ago
GitHub
fix: change from .ts files to .d.ts by wellwelwel Ā· Pull Request #2...
See: #2086 reproduce error from #2086 reproduce error from sveltejs/language-tools#2068 resolve new conflicts on current tests add these errors to CI Test create declarations for: .../Ex...
Want results from more Discord servers?
Add your server