connection-manager.js
2.69 KB
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
"use strict";
var AbstractConnectionManager = require('../abstract/connection-manager')
, Pooling = require('generic-pool')
, ConnectionManager
, Utils = require('../../utils')
, Promise = require('../../promise');
ConnectionManager = function(dialect, sequelize) {
AbstractConnectionManager.call(this, dialect, sequelize);
this.sequelize = sequelize;
this.sequelize.config.port = this.sequelize.config.port || 1433;
this.connection = null;
try {
this.lib = require(sequelize.config.dialectModulePath || 'mssql');
} catch (err) {
throw new Error('Please install mssql package');
}
};
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
ConnectionManager.prototype.connect = function(config) {
var self = this;
return new Promise(function (resolve, reject) {
var connectionConfig = {
host: config.host,
port: config.port,
user: config.username,
password: config.password,
database: config.database,
timezone: self.sequelize.options.timezone
};
if (config.dialectOptions) {
Object.keys(config.dialectOptions).forEach(function(key) {
connectionConfig[key] = config.dialectOptions[key];
});
}
if(!self.connection){
config = {
user: connectionConfig.user,
password: connectionConfig.password,
server: connectionConfig.host,
port: connectionConfig.port,
database: connectionConfig.database,
pool: {
max: config.max,
min: config.min,
idleTimeoutMillis: config.idle
}
};
self.connection = {
config: config,
lib: self.lib
};
self.lib._transaction = null;
var conn = new self.lib.Connection(config, function(err) {
if (err) {
reject(err);
return;
}
self.connection.context = conn;
resolve(self.connection);
});
}else{
resolve(self.connection);
}
});
};
ConnectionManager.prototype.getConnection = function(options) {
var self = this;
options = options || {};
//TODO: dialect check
return new Promise(function (resolve, reject) {
resolve(self.$connect(self.config));
});
};
ConnectionManager.prototype.releaseConnection = function(connection) {
var self = this;
return new Promise(function (resolve, reject) {
//self.pool.release(connection);
resolve();
});
};
ConnectionManager.prototype.disconnect = function(connection) {
return new Promise(function (resolve, reject) {
resolve();
});
};
ConnectionManager.prototype.validate = function(connection) {
// console.log('add code for validations here', connection);
return true;
};
module.exports = ConnectionManager;