Skip to content
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
135 changes: 135 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,141 @@ 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<MySQLRowDataPacket[]>(
"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<MySQLResultSetHeader>("SET @1 = 1");

/**
* @result: ResultSetHeader {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
info: '',
serverStatus: 2,
warningStatus: 0,
changedRows: 0
}
*/
return result
});
```

##### 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:
Expand Down
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,37 @@ function _createConnection ({ connectionType, options, usePromise }, cb) {
}
}

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) {
return isMySQLPoolOrPromisePool(obj) && obj.pool.promise === undefined
}

function isMySQLConnection (obj) {
return isMySQLConnectionOrPromiseConnection(obj) && typeof obj.connection.promise === 'function' && typeof obj.connection.authorized === 'boolean'
}

function isMySQLPromiseConnection (obj) {
return isMySQLConnectionOrPromiseConnection(obj) && obj.connection.promise === undefined
}

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
38 changes: 38 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -198,6 +199,43 @@ test('promise connection', (t) => {
t.end()
})

test('isMySQLConnection is true', (t) => {
t.plan(5)
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.equal(isMySQLPromiseConnection(fastify.mysql), false)
t.equal(isMySQLPromisePool(fastify.mysql), false)
t.end()
})
fastify.close()
})

test('isMySQLPromiseConnection is true', (t) => {
t.plan(5)
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.equal(isMySQLConnection(fastify.mysql), false)
t.equal(isMySQLPool(fastify.mysql), false)
t.end()
})
fastify.close()
})

test('Promise: should throw when mysql2 fail to perform operation', (t) => {
t.plan(3)

Expand Down
19 changes: 19 additions & 0 deletions test/pool.promise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const test = require('tap').test
const Fastify = require('fastify')
const fastifyMysql = require('../index')
const { isMySQLPromisePool, isMySQLPromiseConnection, isMySQLPool, isMySQLConnection } = fastifyMysql

test('promise pool', (t) => {
let fastify
Expand Down Expand Up @@ -82,3 +83,21 @@ test('promise pool', (t) => {

t.end()
})

test('isMySQLPromisePool is true', (t) => {
t.plan(5)
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.equal(isMySQLPool(fastify.mysql), false)
t.equal(isMySQLConnection(fastify.mysql), false)
t.end()
})
fastify.close()
})
18 changes: 18 additions & 0 deletions test/pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const t = require('tap')
const test = t.test
const Fastify = require('fastify')
const fastifyMysql = require('../index')
const { isMySQLPool, isMySQLPromisePool, isMySQLConnection, isMySQLPromiseConnection } = fastifyMysql

test('fastify.mysql namespace should exist', t => {
t.plan(9)
Expand Down Expand Up @@ -168,3 +169,20 @@ test('synchronous functions', (t) => {
t.end()
})
})

test('isMySQLPool is true', (t) => {
t.plan(5)
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.equal(isMySQLPromisePool(fastify.mysql), false)
t.equal(isMySQLPromiseConnection(fastify.mysql), false)
t.end()
})
fastify.close()
})
17 changes: 16 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
format,
Pool,
PoolOptions,
ProcedureCallPacket,
ResultSetHeader,
RowDataPacket,
} from "mysql2";
import {
Connection as PromiseConnection,
Expand All @@ -16,6 +19,12 @@ type FastifyMysql = FastifyPluginCallback<fastifyMysql.MySQLOptions>;

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;

Expand Down Expand Up @@ -56,9 +65,15 @@ declare namespace fastifyMysql {
connectionString?: string;
}

export type MySQLProcedureCallPacket<
T = [MySQLRowDataPacket[], MySQLResultSetHeader] | MySQLResultSetHeader,
> = ProcedureCallPacket<T>
export type MySQLResultSetHeader = ResultSetHeader
export type MySQLRowDataPacket = RowDataPacket

export const fastifyMysql: FastifyMysql
export { fastifyMysql as default }
}

declare function fastifyMysql(...params: Parameters<FastifyMysql>): ReturnType<FastifyMysql>
export = fastifyMysql
export = fastifyMysql
Loading