Skip to content

fix: promise connection typescript mismatched to underlying code #3321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ashleybartlett
Copy link

@ashleybartlett ashleybartlett commented Jan 13, 2025

This fixes a couple of type errors compared to the underlying code.

This is likely a breaking change for some users as it would have required some working around to make it work before.

Should fix #3238 and #2667

Some future improvements that i'd like to see is the exported promise interfaces are converted to classes, to match the underlying code, but that felt like a much bigger change.

This fixes a number of small type errors compared to the underlying code. This is likely a breaking change for some users as it would have required some working around.

Should fix sidorares#3238 and sidorares#2667

This comment was marked as off-topic.

Mistakenly didn't use the promise execute and query import.
@@ -431,4 +431,7 @@ declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) {
sequenceId: number;
}

export { Connection };
declare class Connection extends BaseConnection {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to fix the pool connection inheritance giving a type error on reimplementing the promise

on(event: 'connection', listener: (connection: PoolConnection) => any): this;
on(event: 'acquire', listener: (connection: PoolConnection) => any): this;
on(event: 'release', listener: (connection: PoolConnection) => any): this;
on(event: 'connection', listener: (connection: CorePoolConnection) => any): this;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is reflective of the actual implementation.

}

export interface Pool extends Connection {
export interface Pool extends QueryableAndExecutableBase, Pick<CorePool, 'escape' | 'escapeId' | 'format'> {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pool doesn't extend Connection in code, so this better reflects the actual implementation

@@ -42,6 +44,8 @@ export interface PreparedStatementInfo {
export interface Connection extends QueryableAndExecutableBase {
config: ConnectionOptions;

connection: CoreConnection;
Copy link
Author

@ashleybartlett ashleybartlett Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This connection field is defined on the promise Connection object, so this is now available everywhere this connection is used (including pool connection)

@@ -63,6 +63,13 @@ declare class Pool extends QueryableBase(ExecutableBase(EventEmitter)) {

promise(promiseImpl?: PromiseConstructor): PromisePool;

format(sql: string, values?: any | any[] | { [param: string]: any }): string;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from connection type, as these exist here in code.

import { Pool as PromisePool } from '../../../promise.js';

declare class PoolConnection extends Connection {
declare class PoolConnection extends BaseConnection {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed inheritance type error for the promise function implementation.

@wellwelwel
Copy link
Collaborator

wellwelwel commented Jan 14, 2025

Thanks, @ashleybartlett!

The inheritance from base connections after #3081 makes perfect sense, but I'll keep the #3273 issue as related due to an unexpected breaking change when using instanceof.

I'm planning to run some regression tests in your changes just to make sure we wouldn't imply an additional breaking change.

@ashleybartlett
Copy link
Author

ashleybartlett commented Jan 14, 2025

I think that breaking change makes a lot of sense, as it was done under the hood, but i'm not sure if it's something that can be fixed properly.

Given it wasn't breaking existing checks, I can revert that change for the base type if it's going to be a problem, and it can be revisited in a breaking change release.

@ashleybartlett ashleybartlett changed the title fix: connection types mismatched code fix: Promise connection typescript mismatched to underlying code Jan 15, 2025
@ashleybartlett ashleybartlett changed the title fix: Promise connection typescript mismatched to underlying code fix: promise connection typescript mismatched to underlying code Jan 15, 2025
@ashleybartlett
Copy link
Author

I'll keep the #3273 issue as related due to an unexpected breaking change when using instanceof.

Revisiting this, I realised that instanceof would be a runtime check, not a typescript one. For this, I would ensure Typescript is accurately describing the Javascript code it's referencing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Promise Pool events inherit original pool connection class, not promise pool
2 participants