@@ -11,42 +11,77 @@ var mssql = require('mssql');
1111 */
1212class MsSql {
1313
14- constructor ( pool , params ) {
15- this . pool = pool ;
16- this . params = params ;
14+ constructor ( connection ) {
15+ this . connection = connection ;
16+ this . _isConnected = false ;
17+
18+ this . pool = null ;
1719 this . transaction = null ;
1820 this . _inTransaction = false ;
1921 }
2022
23+ async _pool ( ) {
24+
25+ if ( this . _isConnected ) {
26+ return this . pool ;
27+ }
28+
29+ var connection = this . connection ;
30+
31+ if ( 'string' === typeof connection ) {
32+ this . pool = await mssql . connect ( connection ) ;
33+ this . _isConnected = true ;
34+ return this . pool ;
35+ }
36+
37+ var convertedConnection = {
38+ server : connection . Hostname || 'localhost' ,
39+ port : parseInt ( connection . Port ) || 1433 ,
40+ database : connection . Database ,
41+ user : connection . Username ,
42+ password : connection . Password ,
43+ options : connection . options || { }
44+ } ;
45+
46+ var config = Object . assign (
47+ convertedConnection ,
48+ {
49+ options : {
50+ abortTransactionOnError : true ,
51+ encrypt : false
52+ }
53+ }
54+ ) ;
55+
56+ this . pool = await mssql . connect ( config ) ;
57+ this . _isConnected = true ;
58+ return this . pool ;
59+ }
60+
2161 /**
2262 * Performs a query or data manipulation command.
2363 *
2464 * @param {string } sql
2565 */
26- query ( sql ) {
27- var request = new mssql . Request ( this . pool ) ;
28- return request . query ( sql ) ;
66+ async query ( sql ) {
67+ let pool = await this . _pool ( ) ;
68+ let result = await pool . request ( ) . query ( sql ) ;
69+ return result . recordset ;
2970 }
3071
3172 /**
3273 * Executes a data manipulation command.
3374 *
3475 * @param {string } sql
3576 */
36- execute ( sql ) {
37- return this . query ( sql ) ;
77+ async execute ( sql ) {
78+ return await this . query ( sql ) ;
3879 }
3980
40- close ( ) {
41- var self = this ;
42- return new Promise ( function closePromise ( resolve , reject ) {
43- try {
44- self . pool . close ( ) ;
45- resolve ( ) ;
46- } catch ( err ) {
47- reject ( err ) ;
48- }
49- } ) ;
81+ async close ( ) {
82+ let pool = await this . _pool ( ) ;
83+ await pool . close ( ) ;
84+ this . _isConnected = false ;
5085 }
5186
5287 isTransactionSupported ( ) {
@@ -57,26 +92,27 @@ class MsSql {
5792 return this . _inTransaction ;
5893 }
5994
60- beginTransaction ( ) {
61- if ( true === this . inTransaction ( ) ) {
62- return Promise . resolve ( false ) ;
95+ async beginTransaction ( ) {
96+ if ( this . inTransaction ( ) ) {
97+ false ;
6398 }
64- this . transaction = new mssql . Transaction ( this . pool ) ;
99+ let pool = await this . _pool ( ) ;
100+ this . transaction = new mssql . Transaction ( pool ) ;
65101 this . _inTransaction = true ;
66102 return this . transaction . begin ( ) ;
67103 }
68104
69- commit ( ) {
70- if ( false === this . inTransaction ( ) ) {
71- return Promise . resolve ( false ) ;
105+ async commit ( ) {
106+ if ( ! this . inTransaction ( ) ) {
107+ return false ;
72108 }
73109 this . _inTransaction = false ;
74110 return this . transaction . commit ( ) ;
75111 }
76112
77- rollback ( ) {
78- if ( false === this . inTransaction ( ) ) {
79- return Promise . resolve ( false ) ;
113+ async rollback ( ) {
114+ if ( ! this . inTransaction ( ) ) {
115+ return false ;
80116 }
81117 this . _inTransaction = false ;
82118 return this . transaction . rollback ( ) ;
@@ -85,38 +121,6 @@ class MsSql {
85121
86122module . exports = {
87123 open : function ( connection ) {
88-
89- try { // avoid warnings
90- ( async function openAsync ( connection ) {
91-
92- if ( 'string' === typeof connection ) {
93- return new MsSql ( await mssql . connect ( connection ) ) ;
94- }
95-
96- var convertedConnection = {
97- server : connection . Hostname || 'localhost' ,
98- port : parseInt ( connection . Port ) || 1433 ,
99- database : connection . Database ,
100- user : connection . Username ,
101- password : connection . Password ,
102- options : connection . options || { }
103- } ;
104-
105- var config = Object . assign (
106- {
107- options : {
108- abortTransactionOnError : true ,
109- encrypt : false
110- }
111- } ,
112- convertedConnection
113- ) ;
114-
115- return new MsSql ( await mssql . connect ( config ) ) ;
116- } ) ( connection ) ;
117-
118- } catch ( err ) {
119- throw err ; // rethrow
120- }
124+ return new MsSql ( connection ) ;
121125 }
122126} ;
0 commit comments