node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Is it possible to create a pool from a connection string?

Open rcoundon opened this issue 2 years ago • 5 comments

For example, to create a single connection using a connection string we do:

mysql.createConnection(connectionString);

I can't seem to find a way to do the same to create a pool, is it possible?

rcoundon avatar Apr 19 '22 11:04 rcoundon

Ah - is this the uri property passed to the createPool function?

rcoundon avatar Apr 19 '22 11:04 rcoundon

hm, I'd really like .createPool to match .createConnection in a way that it treats single string parameter as a connection uri

I'll keep this issue open as a place for discussion why this is a good or bad idea

sidorares avatar Apr 20 '22 01:04 sidorares

actually I believe this is already behaviour for .createPool

https://github.com/sidorares/node-mysql2/blob/dbb344e89a1cc8bb457b24e67b07cdb3013fe844/index.js#L22

https://github.com/sidorares/node-mysql2/blob/dbb344e89a1cc8bb457b24e67b07cdb3013fe844/lib/pool_config.js#L8

What have you tried @rcoundon , what result you see and what you expected to see?

sidorares avatar Apr 20 '22 01:04 sidorares

Thanks for getting back to me. What I ended up doing that works is to specify the URL as the uri property of the poolConfig. i.e.

const pool = mysql.createPool({
    uri: connectionString,
  })

When trying to pass a string to the createPool method TypeScript complains so it's probably a case that the types aren't quite right. I'll submit a PR to add a string as a possible option to the createPool method.

rcoundon avatar Apr 20 '22 06:04 rcoundon

It looks like the types here haven't been touched in a long while and could be improved - e.g. there are lots of implicit any errors in the tests.

If you like, I'll work on improving them and bringing it all up-to-date but will commit this PR for this issue and pick up later as I have a bit of time pressure right now

rcoundon avatar Apr 20 '22 06:04 rcoundon