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: constants, parserCache, Pool and Pool Cluster typings #2084

Merged
merged 18 commits into from
Jun 24, 2023
Merged
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
27 changes: 27 additions & 0 deletions promise.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
QueryOptions,
ConnectionOptions,
PoolOptions,
PoolClusterOptions,
Pool as CorePool,
} from './index.js';
import { ExecutableBase as ExecutableBaseClass } from './typings/mysql/lib/protocol/sequences/promise/ExecutableBase.js';
Expand Down Expand Up @@ -95,10 +96,36 @@ export interface Pool extends Connection {
pool: CorePool;
}

export interface PoolNamespace extends QueryableAndExecutableBase {
getConnection(): Promise<PoolConnection>;
}

export interface PoolCluster extends EventEmitter {
config: PoolClusterOptions;

add(config: PoolOptions): void;
add(group: string, connectionUri: string): void;
add(group: string, config: PoolOptions): void;

end(): Promise<void>;

getConnection(): Promise<PoolConnection>;
getConnection(group: string): Promise<PoolConnection>;
getConnection(group: string, selector: string): Promise<PoolConnection>;

of(pattern: string, selector?: string): PoolNamespace;

on(event: string, listener: (args: any[]) => void): this;
on(event: 'remove', listener: (nodeId: number) => void): this;
on(event: 'connection', listener: (connection: PoolConnection) => void): this;
}

export function createConnection(connectionUri: string): Promise<Connection>;
export function createConnection(
config: ConnectionOptions
): Promise<Connection>;

export function createPool(connectionUri: string): Pool;
export function createPool(config: PoolOptions): Pool;

export function createPoolCluster(config?: PoolClusterOptions): PoolCluster;
19 changes: 19 additions & 0 deletions promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const core = require('./index.js');
const EventEmitter = require('events').EventEmitter;
const parserCache = require('./lib/parsers/parser_cache.js');

function makeDoneCb(resolve, reject, localErr) {
return function (err, rows, fields) {
Expand Down Expand Up @@ -563,3 +564,21 @@ exports.raw = core.raw;
exports.PromisePool = PromisePool;
exports.PromiseConnection = PromiseConnection;
exports.PromisePoolConnection = PromisePoolConnection;

exports.__defineGetter__('Types', () => require('./lib/constants/types.js'));

exports.__defineGetter__('Charsets', () =>
require('./lib/constants/charsets.js')
);

exports.__defineGetter__('CharsetToEncoding', () =>
require('./lib/constants/charset_encodings.js')
);

exports.setMaxParserCache = function(max) {
parserCache.setMaxCache(max);
};

exports.clearParserCache = function() {
parserCache.clearCache();
};
2 changes: 2 additions & 0 deletions test/tsc-build/mysql/baseConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const access: mysql.ConnectionOptions = {
database: '',
};

export const uriAccess = `mysql://${access.host}:${access.password}@${access.host}:${access.port}/${access.database}`;

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

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

const charsetToEncoding: string[] = mysql.CharsetToEncoding;
const utf8: string = charsetToEncoding[0];

console.log(utf8, charsetToEncoding);
6 changes: 6 additions & 0 deletions test/tsc-build/mysql/constants/Charsets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { mysql } from '../../index.js';

const BIG5_CHINESE_CI: number = mysql.Charsets.BIG5_CHINESE_CI;
const BIG5: number = mysql.Charsets.BIG5;

console.log(BIG5_CHINESE_CI, BIG5);
7 changes: 7 additions & 0 deletions test/tsc-build/mysql/constants/Types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { mysql } from '../../index.js';

const BLOB: number = mysql.Types.BLOB;
const DECIMAL: string = mysql.Types[0x00];
const DOUBLE: string = mysql.Types[5];

console.log(BLOB, DECIMAL, DOUBLE);
12 changes: 12 additions & 0 deletions test/tsc-build/mysql/createPool/callbacks/createPool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { mysql } from '../../../index.js';
import { uriAccess, access } from '../../baseConnection.js';

(() => {
let uriPool: mysql.Pool | null = null;
let pool: mysql.Pool | null = null;

if (uriPool === null || pool === null) return;

uriPool = mysql.createPool(uriAccess);
pool = mysql.createPool(access);
})();
14 changes: 14 additions & 0 deletions test/tsc-build/mysql/createPoolCluster/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { mysql } from '../../index.js';
import { access, uriAccess } from '../baseConnection.js';

const poolCluster = mysql.createPoolCluster();

// Overload: poolCluster.add(group, connectionUri);
poolCluster.add('cluster1', uriAccess);
// Overload: poolCluster.add(group, config);
poolCluster.add('cluster2', access);
// Overload: poolCluster.add(config);
poolCluster.add(access);

// @ts-expect-error: The option to pass only `URI` doesn't exists
poolCluster.add(uriAccess);
69 changes: 69 additions & 0 deletions test/tsc-build/mysql/createPoolCluster/getConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { mysql } from '../../index.js';
import { access, uriAccess, sql, sqlPS, values } from '../baseConnection.js';

const poolCluster = mysql.createPoolCluster();

poolCluster.add('cluster1', uriAccess);
poolCluster.add('cluster2', access);

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

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

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

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

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

/** Checking `PoolConnection` */
conn.release();
});

/** query */
poolCluster.getConnection('cluster1', (err, conn) => {
/** Overload: query(sql, () => {}}) */
conn.query(sql, (err, result, fields) => {
console.log(err, result, fields);
});

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

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

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

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

/** Checking `PoolConnection` */
conn.release();
});
69 changes: 69 additions & 0 deletions test/tsc-build/mysql/createPoolCluster/of/getConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { mysql } from '../../../index.js';
import { access, uriAccess, sql, sqlPS, values } from '../../baseConnection.js';

const poolCluster = mysql.createPoolCluster();

poolCluster.add('cluster1', uriAccess);
poolCluster.add('cluster2', access);

/** execute */
poolCluster.of('cluster1').getConnection((err, conn) => {
/** Overload: execute(sql, () => {}}) */
conn.execute(sql, (err, result, fields) => {
console.log(err, result, fields);
});

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

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

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

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

/** Checking `PoolConnection` */
conn.release();
});

/** query */
poolCluster.of('cluster2').getConnection((err, conn) => {
/** Overload: query(sql, () => {}}) */
conn.query(sql, (err, result, fields) => {
console.log(err, result, fields);
});

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

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

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

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

/** Checking `PoolConnection` */
conn.release();
});
73 changes: 73 additions & 0 deletions test/tsc-build/mysql/createPoolCluster/of/of.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { mysql } from '../../../index.js';
import { access, uriAccess, sql, sqlPS, values } from '../../baseConnection.js';

const poolCluster = mysql.createPoolCluster();

poolCluster.add('cluster1', uriAccess);
poolCluster.add('cluster2', access);

/** execute */
{
const conn = poolCluster.of('cluster1');

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

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

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

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

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

/** @ts-expect-error: PoolNamespace can't be a `PoolConnection` */
conn.release();
}

/** query */
{
const conn = poolCluster.of('cluster2');

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

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

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

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

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

/** @ts-expect-error: PoolNamespace can't be a `PoolConnection` */
conn.release();
}
3 changes: 3 additions & 0 deletions test/tsc-build/mysql/parsers/clearParserCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { mysql } from '../../index.js';

mysql.clearParserCache();
6 changes: 6 additions & 0 deletions test/tsc-build/mysql/parsers/setMaxParserCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { mysql } from '../../index.js';

mysql.setMaxParserCache(1000);

// @ts-expect-error: The `max` param is required
mysql.setMaxParserCache();
2 changes: 2 additions & 0 deletions test/tsc-build/promise/baseConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const access: mysql.ConnectionOptions = {
database: '',
};

export const uriAccess = `mysql://${access.host}:${access.password}@${access.host}:${access.port}/${access.database}`;

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

Expand Down
6 changes: 6 additions & 0 deletions test/tsc-build/promise/constants/CharsetToEncoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { mysqlp as mysql } from '../../index.js';

const charsetToEncoding: string[] = mysql.CharsetToEncoding;
const utf8: string = charsetToEncoding[0];

console.log(utf8, charsetToEncoding);
6 changes: 6 additions & 0 deletions test/tsc-build/promise/constants/Charsets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { mysqlp as mysql } from '../../index.js';

const BIG5_CHINESE_CI: number = mysql.Charsets.BIG5_CHINESE_CI;
const BIG5: number = mysql.Charsets.BIG5;

console.log(BIG5_CHINESE_CI, BIG5);
7 changes: 7 additions & 0 deletions test/tsc-build/promise/constants/Types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { mysqlp as mysql } from '../../index.js';

const BLOB: number = mysql.Types.BLOB;
const DECIMAL: string = mysql.Types[0x00];
const DOUBLE: string = mysql.Types[5];

console.log(BLOB, DECIMAL, DOUBLE);
Loading