Skip to content
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

fix: promise connection typescript mismatched to underlying code #3321

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions promise.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
FieldPacket,
QueryOptions,
ConnectionOptions,
Connection as CoreConnection,
PoolOptions,
PoolClusterOptions,
Pool as CorePool,
PoolConnection as CorePoolConnection,
} from './index.js';
import { ExecutableBase as ExecutableBaseClass } from './typings/mysql/lib/protocol/sequences/promise/ExecutableBase.js';
import { QueryableBase as QueryableBaseClass } from './typings/mysql/lib/protocol/sequences/promise/QueryableBase.js';
Expand Down Expand Up @@ -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)


threadId: number;

connect(): Promise<void>;
Expand Down Expand Up @@ -78,17 +82,16 @@ export interface Connection extends QueryableAndExecutableBase {

export interface PoolConnection extends Connection {
release(): void;
connection: Connection;
}

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

getConnection(): Promise<PoolConnection>;

releaseConnection(connection: PoolConnection): void;

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.

on(event: 'acquire', listener: (connection: CorePoolConnection) => any): this;
on(event: 'release', listener: (connection: CorePoolConnection) => any): this;
on(event: 'enqueue', listener: () => any): this;

end(): Promise<void>;
Expand Down
11 changes: 7 additions & 4 deletions typings/mysql/lib/Connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ export interface ConnectionOptions {
jsonStrings?: boolean;
}

declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) {
declare class BaseConnection extends QueryableBase(
ExecutableBase(EventEmitter),
) {
config: ConnectionOptions;

threadId: number;
Expand Down Expand Up @@ -414,8 +416,6 @@ declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) {

serverHandshake(args: any): any;

promise(promiseImpl?: PromiseConstructor): PromiseConnection;

ping(callback?: (err: QueryError | null) => any): void;

writeOk(args?: OkPacketParams): void;
Expand All @@ -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

promise(promiseImpl?: PromiseConstructor): PromiseConnection;
}
export { Connection, BaseConnection };
7 changes: 7 additions & 0 deletions typings/mysql/lib/Pool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.


escape(value: any): string;

escapeId(value: string | string[]): string;
escapeId(values: string[]): string;

config: PoolOptions;
}

Expand Down
4 changes: 2 additions & 2 deletions typings/mysql/lib/PoolConnection.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Connection } from './Connection.js';
import { Connection, BaseConnection } from './Connection.js';
import { Pool as PromisePool } from '../../../promise.js';

declare class PoolConnection extends Connection {
declare class PoolConnection extends BaseConnection {
connection: Connection;
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.

release(): void;
promise(promiseImpl?: PromiseConstructor): PromisePool;
Expand Down
Loading