Skip to content

Commit

Permalink
nodenext compatibility (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Dec 5, 2022
1 parent 57a8e60 commit 5ac54ae
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 63 deletions.
56 changes: 0 additions & 56 deletions index.d.ts

This file was deleted.

12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ function fastifyMysql (fastify, options, next) {
})
}

module.exports = fp(fastifyMysql, {
fastify: '4.x',
name: '@fastify/mysql'
})

function _createConnection ({ connectionType, options, usePromise }, cb) {
const { format, escape, escapeId } = require('mysql2')
const mysql = usePromise ? require('mysql2/promise') : require('mysql2')
Expand Down Expand Up @@ -104,3 +99,10 @@ function _createConnection ({ connectionType, options, usePromise }, cb) {
}
}
}

module.exports = fp(fastifyMysql, {
fastify: '4.x',
name: '@fastify/mysql'
})
module.exports.default = fastifyMysql
module.exports.fastifyMysql = fastifyMysql
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "4.0.0",
"description": "Fastify Mysql connection plugin",
"main": "index.js",
"types": "index.d.ts",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard",
"mariadb": "docker run -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes --rm mariadb:10.1",
Expand Down
64 changes: 64 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { FastifyPluginCallback } from "fastify";
import {
Connection,
ConnectionOptions,
escape,
format,
Pool,
PoolOptions,
} from "mysql2";
import {
Connection as PromiseConnection,
Pool as PromisePool,
} from "mysql2/promise";

type FastifyMysql = FastifyPluginCallback<fastifyMysql.MySQLOptions>;

declare namespace fastifyMysql {

// upstream package missed type
type escapeId = (val: any, forbidQualified?: boolean) => string;

interface BaseClient {
format: typeof format;
escape: typeof escape;
escapeId: escapeId;
}

export type MySQLConnection = Pick<Connection, "query" | "execute"> & {
connection: Connection;
} & BaseClient;

export type MySQLPool = Pick<Pool, "query" | "execute" | "getConnection"> & {
pool: Pool;
} & BaseClient;

export type MySQLPromiseConnection = Pick<
PromiseConnection,
"query" | "execute"
> & {
connection: PromiseConnection;
} & BaseClient;

export type MySQLPromisePool = Pick<
PromisePool,
"query" | "execute" | "getConnection"
> & {
pool: PromisePool;
} & BaseClient;

export type ConnectionType = "connection" | "pool";

export interface MySQLOptions extends PoolOptions, ConnectionOptions {
type?: ConnectionType;
name?: string;
promise?: boolean;
connectionString?: string;
}

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

declare function fastifyMysql(...params: Parameters<FastifyMysql>): ReturnType<FastifyMysql>
export = fastifyMysql
2 changes: 1 addition & 1 deletion index.test-d.ts → types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fastifyMysql, {
MySQLPool,
MySQLPromiseConnection,
MySQLPromisePool,
} from ".";
} from "..";

declare module "fastify" {
interface FastifyInstance {
Expand Down

0 comments on commit 5ac54ae

Please sign in to comment.