forked from featurist/sworm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssqlDriver.js
70 lines (56 loc) · 1.63 KB
/
mssqlDriver.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
var promisify = require('./promisify');
var optionalRequire = require("./optionalRequire");
var debug = require('debug')('sworm:mssql');
module.exports = function() {
var sql = optionalRequire("mssql");
return {
query: function(query, params) {
var request = new sql.Request(this.transaction || this.connection);
if (params) {
Object.keys(params).forEach(function (key) {
request.input(key, params[key]);
});
}
return promisify(function(cb) {
debug(query, params);
return request.query(query, cb);
});
},
connect: function(config) {
var self = this;
self.connection = new sql.Connection(config.config);
return promisify(function(cb) {
return self.connection.connect(cb);
});
},
begin: function() {
this.transaction = this.connection.transaction();
debug('begin transaction');
return this.transaction.begin();
},
commit: function() {
var self = this;
debug('commit');
return this.transaction.commit().then(function() {
self.transaction = undefined;
});
},
rollback: function() {
var self = this;
debug('rollback');
return this.transaction.rollback().then(function() {
self.transaction = undefined;
});
},
insert: function(query, params, options) {
var id = options.id;
return this.query(query + "; select scope_identity() as " + id, params).then(function (rows) {
return rows[0][id];
});
},
close: function() {
this.connection.close();
return Promise.resolve();
}
};
};