Skip to content

Commit

Permalink
fix: Missing types in "mysql" import (sidorares#1995)
Browse files Browse the repository at this point in the history
* fix: `execute` types in `Connection`

* fix: `promise` options don't appears

* fix: missing `execute` method in `Pool`

* fix: missing `getConnection()` in `.promise()`

* fix: patterning `execute` overloads in mysql import

* fix: `promise` method types in `createConnection`

* fix: `promise` method types in `createPool`

* add: TypeScript build tests for mysql `createPool` and `createConnection`

* add: TypeScript build tests for mysql/promise `createPool` and `createConnection`

* add: description for further contributions in TypeScript build tests

* fix: call methods in createConnection tsc-build
  • Loading branch information
wellwelwel authored May 11, 2023
1 parent 0bf4df2 commit b8c79d0
Show file tree
Hide file tree
Showing 21 changed files with 715 additions and 25 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface Connection extends mysql.Connection {
writeEof(warnings?: number, statusFlags?: number): void;
writeTextResult(rows?: Array<any>, columns?: Array<any>): void;
writePacket(packet: any): void;
promise(promiseImpl?: PromiseConstructor): PromiseConnection;
sequenceId: number;
}

Expand Down Expand Up @@ -157,7 +158,7 @@ export interface Pool extends mysql.Connection {
on(event: 'enqueue', listener: () => any): this;
unprepare(sql: string): mysql.PrepareStatementInfo;
prepare(sql: string, callback?: (err: mysql.QueryError | null, statement: mysql.PrepareStatementInfo) => any): mysql.Prepare;

promise(promiseImpl?: PromiseConstructor): PromisePoolConnection;
config: mysql.PoolOptions;
}

Expand Down
4 changes: 1 addition & 3 deletions promise.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface Connection extends EventEmitter {

export interface PoolConnection extends Connection {
connection: Connection;
getConnection(): Promise<PoolConnection>;
release(): void;
}

Expand Down Expand Up @@ -153,6 +154,3 @@ export interface PreparedStatementInfo {
execute(parameters: any[]): Promise<[RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader, FieldPacket[]]>;
}

export interface PromisePoolConnection extends Connection {
destroy(): any;
}
13 changes: 13 additions & 0 deletions test/tsc-build/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* This tests doesn't execute the scripts or connect in any database.
* It only compiles all typings in the project and ensures that the compilation will be successful.
* To test it, run: npm run test:tsc-build
*
* The purpose of this test is to prevent changes that break the typings in new PRs
*
* Contributions:
*
* For mysql build tests: './mysql/...'
* For mysql/promise build tests: './promise/...'
*/

import mysql from '../../index';
import mysqlp from '../../promise';

Expand Down
17 changes: 17 additions & 0 deletions test/tsc-build/mysql/baseConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { mysql } from '../index';

export const access: mysql.ConnectionOptions = {
host: '',
user: '',
password: '',
database: '',
};

/** The SQL for the query */
export const sql = 'SELECT * FROM `table`';

/** The SQL for the query with prepared statements */
export const sqlPS = 'SELECT * FROM `table` WHERE `id` = ?';

/** The values for the query with prepared statements */
export const values = [1];
31 changes: 31 additions & 0 deletions test/tsc-build/mysql/createConnection/callbacks/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mysql } from '../../../index';
import { access, sql, sqlPS, values } from '../../baseConnection';

{
const db = mysql.createConnection(access);

/** Overload: execute(sql, () => {}}) */
db.execute(sql, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(sql, values, () => {}}) */
db.execute(sqlPS, values, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(QueryOptions, () => {}}) I */
db.execute({ sql }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(QueryOptions, () => {}}) II */
db.execute({ sql: sqlPS, values }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(QueryOptions, values, () => {}}) */
db.execute({ sql: sqlPS }, values, (err, result, fields) => {
console.log(err, result, fields);
});
}
31 changes: 31 additions & 0 deletions test/tsc-build/mysql/createConnection/callbacks/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mysql } from '../../../index';
import { access, sql, sqlPS, values } from '../../baseConnection';

{
const db = mysql.createConnection(access);

/** Overload: query(sql, () => {}}) */
db.query(sql, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(sql, values, () => {}}) */
db.query(sqlPS, values, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(QueryOptions, () => {}}) I */
db.query({ sql }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(QueryOptions, () => {}}) II */
db.query({ sql: sqlPS, values }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(QueryOptions, values, () => {}}) */
db.query({ sql: sqlPS }, values, (err, result, fields) => {
console.log(err, result, fields);
});
}
38 changes: 38 additions & 0 deletions test/tsc-build/mysql/createConnection/promise/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { mysql } from '../../../index';
import { access, sql, sqlPS, values } from '../../baseConnection';

(async () => {
const db = mysql.createConnection(access).promise();

{
/** Overload: execute(sql) */
const [results, fields] = await db.execute(sql);
console.log(results, fields);
}

{
/** Overload: execute(sql, values) */
const [results, fields] = await db.execute(sqlPS, values);
console.log(results, fields);
}

{
/** Overload: execute(QueryOptions) I */
const [results, fields] = await db.execute({ sql });
console.log(results, fields);
}

{
/** Overload: execute(QueryOptions) II */
const [results, fields] = await db.execute({ sql: sqlPS, values });
console.log(results, fields);
}

{
/** Overload: execute(QueryOptions, values) */
const [results, fields] = await db.execute({ sql: sqlPS }, values);
console.log(results, fields);
}

await db.end();
})();
38 changes: 38 additions & 0 deletions test/tsc-build/mysql/createConnection/promise/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { mysql } from '../../../index';
import { access, sql, sqlPS, values } from '../../baseConnection';

(async () => {
const db = mysql.createConnection(access).promise();

{
/** Overload: query(sql) */
const [results, fields] = await db.query(sql);
console.log(results, fields);
}

{
/** Overload: query(sql, values) */
const [results, fields] = await db.query(sqlPS, values);
console.log(results, fields);
}

{
/** Overload: query(QueryOptions) I */
const [results, fields] = await db.query({ sql });
console.log(results, fields);
}

{
/** Overload: query(QueryOptions) II */
const [results, fields] = await db.query({ sql: sqlPS, values });
console.log(results, fields);
}

{
/** Overload: query(QueryOptions, values) */
const [results, fields] = await db.query({ sql: sqlPS }, values);
console.log(results, fields);
}

await db.end();
})();
61 changes: 61 additions & 0 deletions test/tsc-build/mysql/createPool/callbacks/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { mysql } from '../../../index';
import { access, sql, sqlPS, values } from '../../baseConnection';

{
const db = mysql.createPool(access);

/** Overload: execute(sql, () => {}}) */
db.execute(sql, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(sql, values, () => {}}) */
db.execute(sqlPS, values, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(QueryOptions, () => {}}) I */
db.execute({ sql }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(QueryOptions, () => {}}) II */
db.execute({ sql: sqlPS, values }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(QueryOptions, values, () => {}}) */
db.execute({ sql: sqlPS }, values, (err, result, fields) => {
console.log(err, result, fields);
});
}

/** getConnection and query */
{
mysql.createPool(access).getConnection((err, connection) => {
/** Overload: execute(sql, () => {}}) */
connection.execute(sql, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(sql, values, () => {}}) */
connection.execute(sqlPS, values, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(QueryOptions, () => {}}) I */
connection.execute({ sql }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(QueryOptions, () => {}}) II */
connection.execute({ sql: sqlPS, values }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: execute(QueryOptions, values, () => {}}) */
connection.execute({ sql: sqlPS }, values, (err, result, fields) => {
console.log(err, result, fields);
});
});
}
61 changes: 61 additions & 0 deletions test/tsc-build/mysql/createPool/callbacks/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { mysql } from '../../../index';
import { access, sql, sqlPS, values } from '../../baseConnection';

{
const db = mysql.createPool(access);

/** Overload: query(sql, () => {}}) */
db.query(sql, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(sql, values, () => {}}) */
db.query(sqlPS, values, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(QueryOptions, () => {}}) I */
db.query({ sql }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(QueryOptions, () => {}}) II */
db.query({ sql: sqlPS, values }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(QueryOptions, values, () => {}}) */
db.query({ sql: sqlPS }, values, (err, result, fields) => {
console.log(err, result, fields);
});
}

/** getConnection */
{
mysql.createPool(access).getConnection((err, connection) => {
/** Overload: query(sql, () => {}}) */
connection.query(sql, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(sql, values, () => {}}) */
connection.query(sqlPS, values, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(QueryOptions, () => {}}) I */
connection.query({ sql }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(QueryOptions, () => {}}) II */
connection.query({ sql: sqlPS, values }, (err, result, fields) => {
console.log(err, result, fields);
});

/** Overload: query(QueryOptions, values, () => {}}) */
connection.query({ sql: sqlPS }, values, (err, result, fields) => {
console.log(err, result, fields);
});
});
}
Loading

0 comments on commit b8c79d0

Please sign in to comment.