From 5f47d04abe601da39ddbc642894b4d569c0a005a Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Fri, 29 Dec 2023 17:25:11 -0500 Subject: [PATCH 1/7] types: export RowDataPacket, ResultSetHeader and ProcedureCallPacket from MySQL2 --- types/index.d.ts | 11 +++++- types/index.test-d.ts | 85 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 55d40fd..85c0bc9 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -6,6 +6,9 @@ import { format, Pool, PoolOptions, + ProcedureCallPacket, + ResultSetHeader, + RowDataPacket, } from "mysql2"; import { Connection as PromiseConnection, @@ -56,9 +59,15 @@ declare namespace fastifyMysql { connectionString?: string; } + export type MySQLProcedureCallPacket< + T = [MySQLRowDataPacket[], MySQLResultSetHeader] | MySQLResultSetHeader, + > = ProcedureCallPacket + export type MySQLResultSetHeader = ResultSetHeader + export type MySQLRowDataPacket = RowDataPacket + export const fastifyMysql: FastifyMysql export { fastifyMysql as default } } declare function fastifyMysql(...params: Parameters): ReturnType -export = fastifyMysql \ No newline at end of file +export = fastifyMysql diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 3ed5b3f..71cb154 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -4,7 +4,11 @@ import fastifyMysql, { MySQLPool, MySQLPromiseConnection, MySQLPromisePool, + MySQLProcedureCallPacket, + MySQLResultSetHeader, + MySQLRowDataPacket, } from ".."; +import {expectType} from 'tsd'; declare module "fastify" { interface FastifyInstance { @@ -81,3 +85,84 @@ app await mysql.execute("SELECT NOW()"); mysql.connection.end(); }); + +app + .register(fastifyMysql, { + type: "connection", + promise: true, + connectionString: "mysql://root@localhost/mysql", + }) + .after(async function (err) { + const mysql = app.mysql as MySQLPromiseConnection; + const result = await mysql.connection.query('SELECT NOW()'); + expectType(result[0]); + mysql.connection.end(); + }); + +app + .register(fastifyMysql, { + type: "connection", + promise: true, + connectionString: "mysql://root@localhost/mysql", + multipleStatements: true, + }) + .after(async function (err) { + const mysql = app.mysql as MySQLPromiseConnection; + const result = await mysql.connection.query(` + SELECT 1 + 1 AS test; + SELECT 2 + 2 AS test; + `); + expectType(result[0]); + mysql.connection.end(); + }); + +app + .register(fastifyMysql, { + type: "connection", + promise: true, + connectionString: "mysql://root@localhost/mysql", + }) + .after(async function (err) { + const mysql = app.mysql as MySQLPromiseConnection; + const result = await mysql.connection.query('SET @1 = 1'); + expectType(result[0]); + mysql.connection.end(); + }); + +app + .register(fastifyMysql, { + type: "connection", + promise: true, + connectionString: "mysql://root@localhost/mysql", + multipleStatements: true, + }) + .after(async function (err) { + const mysql = app.mysql as MySQLPromiseConnection; + const result = await mysql.connection.query(` + SET @1 = 1; + SET @2 = 2; + `); + expectType(result[0]); + mysql.connection.end(); + }); + +app + .register(fastifyMysql, { + type: "connection", + promise: true, + connectionString: "mysql://root@localhost/mysql", + }) + .after(async function (err) { + const mysql = app.mysql as MySQLPromiseConnection; + mysql.connection.query('DROP PROCEDURE IF EXISTS myProcedure'); + mysql.connection.query(` + CREATE PROCEDURE myProcedure() + BEGIN + SET @1 = 1; + SET @2 = 2; + END + `); + const result = await mysql.connection.query>('CALL myProcedure()'); + expectType>(result[0]); + mysql.connection.end(); + }); From 099b5d7645da261ee73f7ede2628396f502e54a8 Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Sat, 30 Dec 2023 13:13:51 -0500 Subject: [PATCH 2/7] feat: added type guards for pool, promisepool, connection, promiseconnection --- index.js | 24 ++++++ test/connection.test.js | 34 ++++++++ test/pool.promise.test.js | 17 ++++ test/pool.test.js | 16 ++++ types/index.d.ts | 6 ++ types/index.test-d.ts | 158 ++++++++++++++++++++++---------------- 6 files changed, 187 insertions(+), 68 deletions(-) diff --git a/index.js b/index.js index fd6d65f..fe7c1fc 100644 --- a/index.js +++ b/index.js @@ -100,9 +100,33 @@ function _createConnection ({ connectionType, options, usePromise }, cb) { } } +function isMySQLPool (obj) { + if (!('pool' in obj)) return false + return obj && typeof obj.query === 'function' && typeof obj.execute === 'function' && typeof obj.getConnection === 'function' && typeof obj.pool === 'object' +} + +function isMySQLPromisePool (obj) { + if (!('pool' in obj)) return false + return obj && typeof obj.query === 'function' && typeof obj.execute === 'function' && typeof obj.getConnection === 'function' && typeof obj.pool === 'object' +} + +function isMySQLConnection (obj) { + if (!('connection' in obj)) return false + return obj && typeof obj.query === 'function' && typeof obj.execute === 'function' && obj.connection && typeof obj.connection === 'object' +} + +function isMySQLPromiseConnection (obj) { + if (!('connection' in obj)) return false + return obj && typeof obj.query === 'function' && typeof obj.execute === 'function' && obj.connection && typeof obj.connection === 'object' +} + module.exports = fp(fastifyMysql, { fastify: '4.x', name: '@fastify/mysql' }) module.exports.default = fastifyMysql module.exports.fastifyMysql = fastifyMysql +module.exports.isMySQLPool = isMySQLPool +module.exports.isMySQLPromisePool = isMySQLPromisePool +module.exports.isMySQLConnection = isMySQLConnection +module.exports.isMySQLPromiseConnection = isMySQLPromiseConnection diff --git a/test/connection.test.js b/test/connection.test.js index b7f4dd2..17ec1e1 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -3,6 +3,7 @@ const test = require('tap').test const Fastify = require('fastify') const fastifyMysql = require('../index') +const { isMySQLPool, isMySQLPromisePool, isMySQLConnection, isMySQLPromiseConnection } = fastifyMysql test('fastify.mysql namespace should exist', (t) => { const fastify = Fastify() @@ -198,6 +199,39 @@ test('promise connection', (t) => { t.end() }) +test('isMySQLConnection is true', (t) => { + t.plan(3) + const fastify = Fastify() + fastify.register(fastifyMysql, { + type: 'connection', + connectionString: 'mysql://root@localhost/mysql' + }) + fastify.ready((err) => { + t.error(err) + t.equal(isMySQLConnection(fastify.mysql), true) + t.equal(isMySQLPool(fastify.mysql), false) + t.end() + }) + fastify.close() +}) + +test('isMySQLPromiseConnection is true', (t) => { + t.plan(3) + const fastify = Fastify() + fastify.register(fastifyMysql, { + promise: true, + type: 'connection', + connectionString: 'mysql://root@localhost/mysql' + }) + fastify.ready((err) => { + t.error(err) + t.equal(isMySQLPromiseConnection(fastify.mysql), true) + t.equal(isMySQLPromisePool(fastify.mysql), false) + t.end() + }) + fastify.close() +}) + test('Promise: should throw when mysql2 fail to perform operation', (t) => { t.plan(3) diff --git a/test/pool.promise.test.js b/test/pool.promise.test.js index 1364ae1..15feee7 100644 --- a/test/pool.promise.test.js +++ b/test/pool.promise.test.js @@ -3,6 +3,7 @@ const test = require('tap').test const Fastify = require('fastify') const fastifyMysql = require('../index') +const { isMySQLPromisePool, isMySQLPromiseConnection } = fastifyMysql test('promise pool', (t) => { let fastify @@ -82,3 +83,19 @@ test('promise pool', (t) => { t.end() }) + +test('isMySQLPromisePool is true', (t) => { + t.plan(3) + const fastify = Fastify() + fastify.register(fastifyMysql, { + promise: true, + connectionString: 'mysql://root@localhost/mysql' + }) + fastify.ready((err) => { + t.error(err) + t.equal(isMySQLPromisePool(fastify.mysql), true) + t.equal(isMySQLPromiseConnection(fastify.mysql), false) + t.end() + }) + fastify.close() +}) diff --git a/test/pool.test.js b/test/pool.test.js index 51af15b..0196f6e 100644 --- a/test/pool.test.js +++ b/test/pool.test.js @@ -4,6 +4,7 @@ const t = require('tap') const test = t.test const Fastify = require('fastify') const fastifyMysql = require('../index') +const { isMySQLPool, isMySQLConnection } = fastifyMysql test('fastify.mysql namespace should exist', t => { t.plan(9) @@ -168,3 +169,18 @@ test('synchronous functions', (t) => { t.end() }) }) + +test('isMySQLPool is true', (t) => { + t.plan(3) + const fastify = Fastify() + fastify.register(fastifyMysql, { + connectionString: 'mysql://root@localhost/mysql' + }) + fastify.ready((err) => { + t.error(err) + t.equal(isMySQLPool(fastify.mysql), true) + t.equal(isMySQLConnection(fastify.mysql), false) + t.end() + }) + fastify.close() +}) diff --git a/types/index.d.ts b/types/index.d.ts index 85c0bc9..0940126 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -19,6 +19,12 @@ type FastifyMysql = FastifyPluginCallback; declare namespace fastifyMysql { + type MySQLPoolConnection = MySQLPool | MySQLConnection | MySQLPromisePool | MySQLPromiseConnection; + export function isMySQLPool(obj: MySQLPoolConnection): obj is MySQLPool; + export function isMySQLPromisePool(obj: MySQLPoolConnection): obj is MySQLPromisePool; + export function isMySQLConnection(obj: MySQLPoolConnection): obj is MySQLConnection; + export function isMySQLPromiseConnection(obj: MySQLPoolConnection): obj is MySQLPromiseConnection; + // upstream package missed type type escapeId = (val: any, forbidQualified?: boolean) => string; diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 71cb154..09aa342 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -7,8 +7,12 @@ import fastifyMysql, { MySQLProcedureCallPacket, MySQLResultSetHeader, MySQLRowDataPacket, + isMySQLPromiseConnection, + isMySQLConnection, + isMySQLPromisePool, + isMySQLPool, } from ".."; -import {expectType} from 'tsd'; +import { expectType } from 'tsd'; declare module "fastify" { interface FastifyInstance { @@ -26,16 +30,18 @@ app connectionString: "mysql://root@localhost/mysql", }) .after(function (err) { - const mysql = app.mysql as MySQLPool; - mysql.escapeId("foo"); - mysql.escape("bar"); - mysql.format("baz"); - mysql.query("SELECT NOW()", function () {}); - mysql.execute("SELECT NOW()", function () {}); - mysql.getConnection(function (err, con) { - con.release(); - }); - mysql.pool.end(); + if (isMySQLPool(app.mysql)) { + const mysql = app.mysql + mysql.escapeId("foo"); + mysql.escape("bar"); + mysql.format("baz"); + mysql.query("SELECT NOW()", function () {}); + mysql.execute("SELECT NOW()", function () {}); + mysql.getConnection(function (err, con) { + con.release(); + }); + mysql.pool.end(); + } }); app @@ -44,15 +50,17 @@ app connectionString: "mysql://root@localhost/mysql", }) .after(async function (err) { - const mysql = app.mysql as MySQLPromisePool; - mysql.escapeId("foo"); - mysql.escape("bar"); - mysql.format("baz"); - await mysql.query("SELECT NOW()"); - await mysql.execute("SELECT NOW()"); - const con = await mysql.getConnection(); - con.release(); - mysql.pool.end(); + if (isMySQLPromisePool(app.mysql)) { + const mysql = app.mysql + mysql.escapeId("foo"); + mysql.escape("bar"); + mysql.format("baz"); + await mysql.query("SELECT NOW()"); + await mysql.execute("SELECT NOW()"); + const con = await mysql.getConnection(); + con.release(); + mysql.pool.end(); + } }); app @@ -61,13 +69,15 @@ app connectionString: "mysql://root@localhost/mysql", }) .after(async function (err) { - const mysql = app.mysql as MySQLConnection; - mysql.escapeId("foo"); - mysql.escape("bar"); - mysql.format("baz"); - mysql.query("SELECT NOW()", function () {}); - mysql.execute("SELECT NOW()", function () {}); - mysql.connection.end(); + if (isMySQLConnection(app.mysql)) { + const mysql = app.mysql + mysql.escapeId("foo"); + mysql.escape("bar"); + mysql.format("baz"); + mysql.query("SELECT NOW()", function () {}); + mysql.execute("SELECT NOW()", function () {}); + mysql.connection.end(); + } }); app @@ -77,13 +87,15 @@ app connectionString: "mysql://root@localhost/mysql", }) .after(async function (err) { - const mysql = app.mysql as MySQLPromiseConnection; - mysql.escapeId("foo"); - mysql.escape("bar"); - mysql.format("baz"); - await mysql.query("SELECT NOW()"); - await mysql.execute("SELECT NOW()"); - mysql.connection.end(); + if (isMySQLPromiseConnection(app.mysql)) { + const mysql = app.mysql + mysql.escapeId("foo"); + mysql.escape("bar"); + mysql.format("baz"); + await mysql.query("SELECT NOW()"); + await mysql.execute("SELECT NOW()"); + mysql.connection.end(); + } }); app @@ -93,10 +105,12 @@ app connectionString: "mysql://root@localhost/mysql", }) .after(async function (err) { - const mysql = app.mysql as MySQLPromiseConnection; - const result = await mysql.connection.query('SELECT NOW()'); - expectType(result[0]); - mysql.connection.end(); + if (isMySQLPromiseConnection(app.mysql)) { + const mysql = app.mysql + const result = await mysql.connection.query('SELECT NOW()'); + expectType(result[0]); + mysql.connection.end(); + } }); app @@ -107,13 +121,15 @@ app multipleStatements: true, }) .after(async function (err) { - const mysql = app.mysql as MySQLPromiseConnection; - const result = await mysql.connection.query(` - SELECT 1 + 1 AS test; - SELECT 2 + 2 AS test; - `); - expectType(result[0]); - mysql.connection.end(); + if (isMySQLPromiseConnection(app.mysql)) { + const mysql = app.mysql + const result = await mysql.connection.query(` + SELECT 1 + 1 AS test; + SELECT 2 + 2 AS test; + `); + expectType(result[0]); + mysql.connection.end(); + } }); app @@ -123,10 +139,12 @@ app connectionString: "mysql://root@localhost/mysql", }) .after(async function (err) { - const mysql = app.mysql as MySQLPromiseConnection; - const result = await mysql.connection.query('SET @1 = 1'); - expectType(result[0]); - mysql.connection.end(); + if (isMySQLPromiseConnection(app.mysql)) { + const mysql = app.mysql + const result = await mysql.connection.query('SET @1 = 1'); + expectType(result[0]); + mysql.connection.end(); + } }); app @@ -137,13 +155,15 @@ app multipleStatements: true, }) .after(async function (err) { - const mysql = app.mysql as MySQLPromiseConnection; - const result = await mysql.connection.query(` - SET @1 = 1; - SET @2 = 2; - `); - expectType(result[0]); - mysql.connection.end(); + if (isMySQLPromiseConnection(app.mysql)) { + const mysql = app.mysql + const result = await mysql.connection.query(` + SET @1 = 1; + SET @2 = 2; + `); + expectType(result[0]); + mysql.connection.end(); + } }); app @@ -153,16 +173,18 @@ app connectionString: "mysql://root@localhost/mysql", }) .after(async function (err) { - const mysql = app.mysql as MySQLPromiseConnection; - mysql.connection.query('DROP PROCEDURE IF EXISTS myProcedure'); - mysql.connection.query(` - CREATE PROCEDURE myProcedure() - BEGIN - SET @1 = 1; - SET @2 = 2; - END - `); - const result = await mysql.connection.query>('CALL myProcedure()'); - expectType>(result[0]); - mysql.connection.end(); + if (isMySQLPromiseConnection(app.mysql)) { + const mysql = app.mysql + mysql.connection.query('DROP PROCEDURE IF EXISTS myProcedure'); + mysql.connection.query(` + CREATE PROCEDURE myProcedure() + BEGIN + SET @1 = 1; + SET @2 = 2; + END + `); + const result = await mysql.connection.query>('CALL myProcedure()'); + expectType>(result[0]); + mysql.connection.end(); + } }); From 93215291d2971347675ce8dbc11e1c383083f2c5 Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Fri, 16 Feb 2024 22:16:27 -0500 Subject: [PATCH 3/7] update to distingush between promise and normal type guards --- index.js | 20 ++++++++++++-------- types/index.test-d.ts | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index fe7c1fc..3eea179 100644 --- a/index.js +++ b/index.js @@ -100,24 +100,28 @@ function _createConnection ({ connectionType, options, usePromise }, cb) { } } -function isMySQLPool (obj) { - if (!('pool' in obj)) return false +function isMySQLPoolOrPromisePool (obj) { return obj && typeof obj.query === 'function' && typeof obj.execute === 'function' && typeof obj.getConnection === 'function' && typeof obj.pool === 'object' } +function isMySQLConnectionOrPromiseConnection (obj) { + return obj && typeof obj.query === 'function' && typeof obj.execute === 'function' && typeof obj.connection === 'object' +} + +function isMySQLPool (obj) { + return isMySQLPoolOrPromisePool(obj) && typeof obj.pool.promise === 'function' +} + function isMySQLPromisePool (obj) { - if (!('pool' in obj)) return false - return obj && typeof obj.query === 'function' && typeof obj.execute === 'function' && typeof obj.getConnection === 'function' && typeof obj.pool === 'object' + return isMySQLPoolOrPromisePool(obj) && typeof obj.threadId === 'number' && obj.pool.promise === undefined } function isMySQLConnection (obj) { - if (!('connection' in obj)) return false - return obj && typeof obj.query === 'function' && typeof obj.execute === 'function' && obj.connection && typeof obj.connection === 'object' + return isMySQLConnectionOrPromiseConnection(obj) && typeof obj.connection.promise === 'function' && typeof obj.connection.authorized === 'boolean' } function isMySQLPromiseConnection (obj) { - if (!('connection' in obj)) return false - return obj && typeof obj.query === 'function' && typeof obj.execute === 'function' && obj.connection && typeof obj.connection === 'object' + return isMySQLConnectionOrPromiseConnection(obj) && typeof obj.threadId === 'number' && obj.connection.promise === undefined } module.exports = fp(fastifyMysql, { diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 09aa342..7e39234 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -13,6 +13,9 @@ import fastifyMysql, { isMySQLPool, } from ".."; import { expectType } from 'tsd'; +import { Pool } from "mysql2/typings/mysql/lib/Pool"; +import { Connection, PoolConnection } from "mysql2"; +import { Pool as PromisePool, Connection as PromiseConnection, PoolConnection as PromisePoolConnection } from "mysql2/promise"; declare module "fastify" { interface FastifyInstance { @@ -24,6 +27,10 @@ declare module "fastify" { } } +type PoolGetConnectionType = (callback: (err: NodeJS.ErrnoException | null, connection: PoolConnection) => any) => void; +type PoolPromiseType = (promiseImpl?: PromiseConstructor) => PromisePool; +type ConnectionPromiseType = (promiseImpl?: PromiseConstructor) => PromiseConnection; + const app = fastify(); app .register(fastifyMysql, { @@ -41,6 +48,9 @@ app con.release(); }); mysql.pool.end(); + expectType(app.mysql.getConnection); + expectType(app.mysql.pool); + expectType(app.mysql.pool.promise); } }); @@ -60,6 +70,8 @@ app const con = await mysql.getConnection(); con.release(); mysql.pool.end(); + expectType<() => Promise>(app.mysql.getConnection); + expectType(app.mysql.pool); } }); @@ -77,6 +89,9 @@ app mysql.query("SELECT NOW()", function () {}); mysql.execute("SELECT NOW()", function () {}); mysql.connection.end(); + expectType(app.mysql.connection); + expectType(app.mysql.connection.promise); + expectType(app.mysql.connection.authorized); } }); @@ -95,6 +110,8 @@ app await mysql.query("SELECT NOW()"); await mysql.execute("SELECT NOW()"); mysql.connection.end(); + expectType(app.mysql.connection); + expectType(app.mysql.connection.threadId); } }); From b49f7acd3b0452a7cd58a0828af8f0282613c5db Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Fri, 8 Mar 2024 22:08:48 -0500 Subject: [PATCH 4/7] docs: add docs for typescript usage --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/README.md b/README.md index c6044ea..df0f0ec 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,64 @@ declare module 'fastify' { } ``` +#### MySQLRowDataPacket +Ability to add type for return data using mysql2 [RowDataPacket](https://sidorares.github.io/node-mysql2/docs/documentation/typescript-examples#rowdatapacket). + +``` +const fastifyMysql, { MySQLRowDataPacket } from '@fastify/mysql' + +const app = fastify(); + +app.register(fastifyMysql, { + connectionString: "mysql://root@localhost/mysql", +}); + +app.get("/", async () => { + const connection = app.mysql; + + // SELECT + const [rows, fields] = await connection.query( + "SELECT 1 + 1 AS `test`;", + ); + + /** + * @rows: [ { test: 2 } ] + */ + return rows[0]; +}); +``` + +#### MySQLResultSetHeader +Ability to add type for return data using mysql2 [ResultSetHeader](https://sidorares.github.io/node-mysql2/docs/documentation/typescript-examples#resultsetheader). + +``` +const fastifyMysql, { MySQLResultSetHeader } from '@fastify/mysql' + +const app = fastify(); + +app.register(fastifyMysql, { + connectionString: "mysql://root@localhost/mysql", +}); + +app.get("/", async () => { + const connection = app.mysql; + const result = await connection.query("SET @1 = 1"); + + /** + * @result: ResultSetHeader { + fieldCount: 0, + affectedRows: 0, + insertId: 0, + info: '', + serverStatus: 2, + warningStatus: 0, + changedRows: 0 + } + */ + return result +}); +``` + ## Acknowledgements This project is kindly sponsored by: From b08b4124ac780711349ac4f45866b45e9f0c4839 Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Thu, 28 Mar 2024 20:52:44 -0400 Subject: [PATCH 5/7] fix: add unit tests --- test/connection.test.js | 6 +++++- test/pool.promise.test.js | 6 ++++-- test/pool.test.js | 6 ++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/test/connection.test.js b/test/connection.test.js index 17ec1e1..78d3496 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -200,7 +200,7 @@ test('promise connection', (t) => { }) test('isMySQLConnection is true', (t) => { - t.plan(3) + t.plan(5) const fastify = Fastify() fastify.register(fastifyMysql, { type: 'connection', @@ -210,6 +210,8 @@ test('isMySQLConnection is true', (t) => { t.error(err) t.equal(isMySQLConnection(fastify.mysql), true) t.equal(isMySQLPool(fastify.mysql), false) + t.equal(isMySQLPromiseConnection(fastify.mysql), false) + t.equal(isMySQLPromisePool(fastify.mysql), false) t.end() }) fastify.close() @@ -227,6 +229,8 @@ test('isMySQLPromiseConnection is true', (t) => { t.error(err) t.equal(isMySQLPromiseConnection(fastify.mysql), true) t.equal(isMySQLPromisePool(fastify.mysql), false) + t.equal(isMySQLConnection(fastify.mysql), false) + t.equal(isMySQLPool(fastify.mysql), false) t.end() }) fastify.close() diff --git a/test/pool.promise.test.js b/test/pool.promise.test.js index 15feee7..982ef7a 100644 --- a/test/pool.promise.test.js +++ b/test/pool.promise.test.js @@ -3,7 +3,7 @@ const test = require('tap').test const Fastify = require('fastify') const fastifyMysql = require('../index') -const { isMySQLPromisePool, isMySQLPromiseConnection } = fastifyMysql +const { isMySQLPromisePool, isMySQLPromiseConnection, isMySQLPool, isMySQLConnection } = fastifyMysql test('promise pool', (t) => { let fastify @@ -85,7 +85,7 @@ test('promise pool', (t) => { }) test('isMySQLPromisePool is true', (t) => { - t.plan(3) + t.plan(5) const fastify = Fastify() fastify.register(fastifyMysql, { promise: true, @@ -95,6 +95,8 @@ test('isMySQLPromisePool is true', (t) => { t.error(err) t.equal(isMySQLPromisePool(fastify.mysql), true) t.equal(isMySQLPromiseConnection(fastify.mysql), false) + t.equal(isMySQLPool(fastify.mysql), false) + t.equal(isMySQLConnection(fastify.mysql), false) t.end() }) fastify.close() diff --git a/test/pool.test.js b/test/pool.test.js index 0196f6e..f41fc8a 100644 --- a/test/pool.test.js +++ b/test/pool.test.js @@ -4,7 +4,7 @@ const t = require('tap') const test = t.test const Fastify = require('fastify') const fastifyMysql = require('../index') -const { isMySQLPool, isMySQLConnection } = fastifyMysql +const { isMySQLPool, isMySQLPromisePool, isMySQLConnection, isMySQLPromiseConnection } = fastifyMysql test('fastify.mysql namespace should exist', t => { t.plan(9) @@ -171,7 +171,7 @@ test('synchronous functions', (t) => { }) test('isMySQLPool is true', (t) => { - t.plan(3) + t.plan(5) const fastify = Fastify() fastify.register(fastifyMysql, { connectionString: 'mysql://root@localhost/mysql' @@ -180,6 +180,8 @@ test('isMySQLPool is true', (t) => { t.error(err) t.equal(isMySQLPool(fastify.mysql), true) t.equal(isMySQLConnection(fastify.mysql), false) + t.equal(isMySQLPromisePool(fastify.mysql), false) + t.equal(isMySQLPromiseConnection(fastify.mysql), false) t.end() }) fastify.close() From c86d33e3659dc3103bcbc2473d114d137f49b735 Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Fri, 19 Apr 2024 21:16:59 -0400 Subject: [PATCH 6/7] docs: add docs of is* methods --- README.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/README.md b/README.md index df0f0ec..0bbdfd1 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,83 @@ app.get("/", async () => { }); ``` +##### isMySQLPool +Method to check if fastify decorator, mysql is type of [MySQLPool](https://github.com/fastify/fastify-mysql/blob/master/types/index.d.ts#L32) + +```typescript +const app = fastify(); +app + .register(fastifyMysql, { + connectionString: "mysql://root@localhost/mysql", + }) + .after(function (err) { + if (isMySQLPool(app.mysql)) { + const mysql = app.mysql + mysql.getConnection(function (err, con) { + con.release(); + }); + mysql.pool.end(); + } + }) +``` + + +##### isMySQLPromisePool +Method to check if fastify decorator, mysql is type of [MySQLPromisePool](https://github.com/fastify/fastify-mysql/blob/master/types/index.d.ts#L43) + +```typescript +app + .register(fastifyMysql, { + promise: true, + connectionString: "mysql://root@localhost/mysql", + }) + .after(async function (err) { + if (isMySQLPromisePool(app.mysql)) { + const mysql = app.mysql + const con = await mysql.getConnection(); + con.release(); + mysql.pool.end(); + } + }); +``` + + +##### isMySQLConnection +Method to check if fastify decorator, mysql is type of [MySQLConnection](https://github.com/fastify/fastify-mysql/blob/master/types/index.d.ts#L28) + +```typescript +app + .register(fastifyMysql, { + type: "connection", + connectionString: "mysql://root@localhost/mysql", + }) + .after(async function (err) { + if (isMySQLConnection(app.mysql)) { + const mysql = app.mysql + mysql.connection.end(); + } + }); +``` + + +##### isMySQLPromiseConnection +Method to check if fastify decorator, mysql is type of [MySQLPromiseConnection](https://github.com/fastify/fastify-mysql/blob/master/types/index.d.ts#L36) + +```typescript +app + .register(fastifyMysql, { + type: "connection", + promise: true, + connectionString: "mysql://root@localhost/mysql", + }) + .after(async function (err) { + if (isMySQLPromiseConnection(app.mysql)) { + const mysql = app.mysql + mysql.connection.end(); + } + }); +``` + ## Acknowledgements This project is kindly sponsored by: From 60039f4788281cf5611f32dc8bd02fde9a6982d9 Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Mon, 22 Apr 2024 20:54:42 -0400 Subject: [PATCH 7/7] fix: tests based on pr suggestion https://github.com/fastify/fastify-mysql/pull/133\#discussion_r1542576749 --- index.js | 4 ++-- test/connection.test.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3eea179..e972401 100644 --- a/index.js +++ b/index.js @@ -113,7 +113,7 @@ function isMySQLPool (obj) { } function isMySQLPromisePool (obj) { - return isMySQLPoolOrPromisePool(obj) && typeof obj.threadId === 'number' && obj.pool.promise === undefined + return isMySQLPoolOrPromisePool(obj) && obj.pool.promise === undefined } function isMySQLConnection (obj) { @@ -121,7 +121,7 @@ function isMySQLConnection (obj) { } function isMySQLPromiseConnection (obj) { - return isMySQLConnectionOrPromiseConnection(obj) && typeof obj.threadId === 'number' && obj.connection.promise === undefined + return isMySQLConnectionOrPromiseConnection(obj) && obj.connection.promise === undefined } module.exports = fp(fastifyMysql, { diff --git a/test/connection.test.js b/test/connection.test.js index 78d3496..32bf361 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -218,7 +218,7 @@ test('isMySQLConnection is true', (t) => { }) test('isMySQLPromiseConnection is true', (t) => { - t.plan(3) + t.plan(5) const fastify = Fastify() fastify.register(fastifyMysql, { promise: true,