-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
167 lines (147 loc) · 3.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var mssql = require('mssql');
/**
* Database-js driver for MS SQL Server
*
* @author Thiago Delgado Pinto
*
* @see https://github.com/mlaanderson/database-js
* @see https://github.com/tediousjs/node-mssql
* @see https://github.com/tediousjs/tedious
*/
class MsSql {
constructor( connection ) {
this.connection = connection;
this._isConnected = false;
this.pool = null;
this.transaction = null;
this._inTransaction = false;
}
/**
* Creates a connection pool on demand.
*
* @returns Promise< Connection >
*/
async _pool() {
if ( this._isConnected ) {
return this.pool;
}
var connection = this.connection;
if ('string' === typeof connection) {
this.pool = await mssql.connect( connection );
this._isConnected = true;
return this.pool;
}
var convertedConnection = {
server: connection.Hostname || 'localhost',
port: parseInt(connection.Port) || 1433,
database: connection.Database,
user: connection.Username,
password: connection.Password,
options: connection.options || {}
};
var config = Object.assign(
convertedConnection,
{
options: {
abortTransactionOnError: true,
encrypt: false
}
}
);
this.pool = await mssql.connect( config );
this._isConnected = true;
return this.pool;
}
/**
* Performs a query or data manipulation command.
*
* @param {string} sql
* @returns Promise< Array< object > >
*/
async query(sql) {
let pool = await this._pool();
let result = await pool.request().query( sql );
return result.recordset;
}
/**
* Executes a data manipulation command.
*
* @param {string} sql
* @returns Promise< Array< object > >
*/
async execute(sql) {
return await this.query(sql);
}
/**
* Closes a database connection.
*
* @returns Promise<>
*/
async close() {
if ( this._isConnected ) {
let pool = await this._pool();
await pool.close();
await mssql.close();
this._isConnected = false;
}
}
/**
* Checks whether transaction is supported.
*
* @returns boolean
*/
isTransactionSupported() {
return true;
}
/**
* Checks whether it is in a transaction.
*
* @returns boolean
*/
inTransaction() {
return this._inTransaction;
}
/**
* Begins a transaction.
*
* @returns Promise< boolean >
*/
async beginTransaction() {
if ( this.inTransaction() ) {
return false;
}
let pool = await this._pool();
this.transaction = new mssql.Transaction( pool );
this._inTransaction = true;
return this.transaction.begin();
}
/**
* Confirms a transaction.
*
* @returns Promise< boolean >
*/
async commit() {
if ( ! this.inTransaction() ) {
return false;
}
this._inTransaction = false;
return this.transaction.commit();
}
/**
* Undoes a transaction.
*
* @returns Promise< boolean >
*/
async rollback() {
if ( ! this.inTransaction() ) {
return false;
}
this._inTransaction = false;
return this.transaction.rollback();
}
}
module.exports = {
open: function(connection) {
return new MsSql( connection );
}
};