-
-
Notifications
You must be signed in to change notification settings - Fork 657
Description
I was writing some wrappers to abstract away the difference for some conversion code and I noticed that the runtime mysql2 and mysql2/promise don't match the types that are shipped.
Namely:
- https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Pool.d.ts is not set up to extend the connection like the PromisePool does, which means it does not inherit escape/escapeId/format, but those functions do exist at runtime, seen here: https://github.com/sidorares/node-mysql2/blob/master/lib/base/pool.js#L224
When I set a debugger and instantiated a callback based pool and called escape on the object, it got to that code, but I had to write it aspool['escape']('1')and turn off TS warnings because TS didn't believe the function existed. - All that said, the
PromisePoolextends the Connection class, so it has all the members the connection has. This fixes the above, though it introduces a slight bug, as Connections have athreadIdand neither amysql2/Poolor amysql2/Promise/Poolhave athreadIdon them. - The callback based
PoolConnection.promise()function is typed to return a Pool, but it actually returns a promise wrappedmysql2/promise/PoolConnection. You can see the type at https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/PoolConnection.d.ts#L7 but the runtime code at https://github.com/sidorares/node-mysql2/blob/master/lib/pool_connection.js#L8
Why does any of this matter you might ask? Well for most people it might not, definitely if they're raw JS using the types as hints. But if they're converting from mysql to mysql2 without going to /promise, the missing functions could throw them for a loop. For me, I'm torn because I'm making some wrapper classes that are providing a consistent interface between the two drivers to share between my projects as a utility so they can be used by projects in different states of conversion. The issue is that I have to deal with the fact that there are two types to deal with: 1. the runtime truth, which I have to declare to make TS happy when I'm doing operations, and 2. a type that is compatible with the mysql2 shipped types or when people pass in constructed types, they'll be shown as incompatible.