Skip to content

Commit 52c7497

Browse files
committed
Rewrite
1 parent baae03e commit 52c7497

File tree

3 files changed

+72
-75
lines changed

3 files changed

+72
-75
lines changed

index.js

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,77 @@ var mssql = require('mssql');
1111
*/
1212
class 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

86122
module.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
};

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
!!! WARNING !!! - This library is under development and IS NOT STABLE YET.
66

7-
Works with MS SQL Server 2000-2017 through the [TDS protocol](http://msdn.microsoft.com/en-us/library/dd304523.aspx).
7+
It a wrapper for [node-mssql](https://github.com/thiagodp/database-js-mssql.git) and works with MS SQL Server 2000-2017.
88

99

1010
## Install

test/test.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@ var assert = require( 'assert' );
44

55
describe( 'database-js-mssql', function () {
66

7-
var connStr = 'mssql:///root:123456@localhost/testdb';
7+
// Change this line before testing:
8+
var connStr = 'mssql://username:password@localhost/database';
89

9-
it( 'queries an existing table correctly', function( done ) {
10+
it( 'queries an existing table correctly', async function() {
1011
var conn;
1112
try {
1213
conn = new dbjs.Connection( connStr, driver );
1314
} catch ( err ) {
1415
assert.fail( err.message );
1516
return;
1617
}
17-
18-
var st = conn.prepareStatement( 'SELECT * FROM user WHERE username = ?' );
19-
var search = 'alice';
20-
st.query( search )
21-
.then( function( data ) {
22-
assert.equal( data[ 0 ].username, search );
23-
done();
24-
} )
25-
.catch( function( err ) {
26-
done( err );
27-
} );
18+
var st = conn.prepareStatement( 'SELECT * FROM states WHERE state = ?' );
19+
var data = await st.query( 'Dakota' );
20+
console.log( data );
2821
} );
2922

3023
} );

0 commit comments

Comments
 (0)