@@ -2,7 +2,7 @@ import * as mysql from 'mysql2/promise';
22import * as pg from 'pg' ;
33import * as mssql from 'mssql' ;
44
5- export const supportedDrivers = [ 'mysql' , 'postgres' , 'mssql' ] as const ;
5+ const supportedDrivers = [ 'mysql' , 'postgres' , 'mssql' ] as const ;
66
77export type DriverKey = typeof supportedDrivers [ number ] ;
88
@@ -17,51 +17,48 @@ export type Row = { [key: string]: string | number | null };
1717
1818export type ResultTable = Row [ ] ;
1919
20- export interface Conn {
20+ interface Conn {
2121 release : ( ) => void ;
2222 query : ( q : string ) => Promise < QueryResult > ;
2323 destroy : ( ) => void ;
2424}
2525
26- interface PoolConfig {
26+ export type PoolConfig = MySQLConfig | MSSQLConfig | PostgresConfig ;
27+
28+ interface BaseConfig {
29+ driver : DriverKey ;
2730 host : string ;
2831 port : number ;
2932 user : string ;
3033 password ?: string ;
3134 database ?: string ;
3235}
3336
34- export interface Driver {
35- createPool : ( config : PoolConfig ) => Promise < Pool > ;
37+ interface MySQLConfig extends BaseConfig {
38+ driver : 'mysql' ;
3639}
3740
38- export const getDriver = ( driverKey : DriverKey ) : Driver = > {
39- switch ( driverKey ) {
41+ export async function getPool ( c : PoolConfig ) : Promise < Pool > {
42+ switch ( c . driver ) {
4043 case 'mysql' :
41- return mysqlDriver ( ) ;
42- case 'postgres' :
43- return postgresDriver ( ) ;
44+ return createMySQLPool ( c ) ;
4445 case 'mssql' :
45- return mssqlDriver ( ) ;
46+ return createMSSQLPool ( c ) ;
47+ case 'postgres' :
48+ return createPostgresPool ( c ) ;
4649 default :
47- throw new Error ( ` invalid driver key: ${ driverKey } ` ) ;
50+ throw Error ( ' invalid driver key' ) ;
4851 }
49- } ;
52+ }
5053
51- function mysqlDriver ( ) : Driver {
52- return {
53- async createPool ( {
54- host,
55- port,
56- user,
57- password,
58- database,
59- } : PoolConfig ) : Promise < Pool > {
60- return mysqlPool (
61- mysql . createPool ( { host, port, user, password, database, } )
62- ) ;
63- } ,
64- } ;
54+ async function createMySQLPool ( {
55+ host,
56+ port,
57+ user,
58+ password,
59+ database,
60+ } : MySQLConfig ) : Promise < Pool > {
61+ return mysqlPool ( mysql . createPool ( { host, port, user, password, database } ) ) ;
6562}
6663
6764function mysqlPool ( pool : mysql . Pool ) : Pool {
@@ -93,25 +90,25 @@ function mysqlConn(conn: mysql.PoolConnection): Conn {
9390 } ;
9491}
9592
96- function postgresDriver ( ) : Driver {
97- return {
98- async createPool ( {
99- host ,
100- port ,
101- user ,
102- password ,
103- database ,
104- } : PoolConfig ) : Promise < Pool > {
105- const pool = new pg . Pool ( {
106- host ,
107- port ,
108- password ,
109- database ,
110- user ,
111- } ) ;
112- return postgresPool ( pool ) ;
113- } ,
114- } ;
93+ interface PostgresConfig extends BaseConfig {
94+ driver : 'postgres' ;
95+ }
96+
97+ async function createPostgresPool ( {
98+ host ,
99+ port ,
100+ user ,
101+ password ,
102+ database ,
103+ } : PostgresConfig ) : Promise < Pool > {
104+ const pool = new pg . Pool ( {
105+ host ,
106+ port ,
107+ password ,
108+ database ,
109+ user ,
110+ } ) ;
111+ return postgresPool ( pool ) ;
115112}
116113
117114function postgresPool ( pool : pg . Pool ) : Pool {
@@ -142,23 +139,23 @@ function postgresConn(conn: pg.PoolClient): Conn {
142139 } ;
143140}
144141
145- function mssqlDriver ( ) : Driver {
146- return {
147- async createPool ( config : PoolConfig ) : Promise < Pool > {
148- const conn = await mssql . connect ( {
149- server : config . host ,
150- port : config . port ,
151- user : config . user ,
152- password : config . password ,
153- database : config . database ,
154- options : {
155- encrypt : false ,
156- trustServerCertificate : true ,
157- } ,
158- } ) ;
159- return mssqlPool ( conn ) ;
160- } ,
161- } ;
142+ interface MSSQLConfig extends BaseConfig {
143+ driver : 'mssql' ;
144+ encrypt : boolean ;
145+ }
146+
147+ async function createMSSQLPool ( config : MSSQLConfig ) : Promise < Pool > {
148+ const conn = await mssql . connect ( {
149+ server : config . host ,
150+ port : config . port ,
151+ user : config . user ,
152+ password : config . password ,
153+ database : config . database ,
154+ options : {
155+ encrypt : config . encrypt ,
156+ } ,
157+ } ) ;
158+ return mssqlPool ( conn ) ;
162159}
163160
164161function mssqlPool ( pool : mssql . ConnectionPool ) : Pool {
0 commit comments