connection-manager.js
4.4 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
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
'use strict';
const AbstractConnectionManager = require('../abstract/connection-manager');
const ResourceLock = require('./resource-lock');
const Promise = require('../../promise');
const Utils = require('../../utils');
const debug = Utils.getLogger().debugContext('connection:mssql');
const sequelizeErrors = require('../../errors');
const parserStore = require('../parserStore')('mssql');
const _ = require('lodash');
class ConnectionManager extends AbstractConnectionManager {
constructor(dialect, sequelize) {
super(dialect, sequelize);
this.sequelize = sequelize;
this.sequelize.config.port = this.sequelize.config.port || 1433;
try {
this.lib = require(sequelize.config.dialectModulePath || 'tedious');
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
throw new Error('Please install tedious package manually');
}
throw err;
}
}
// Expose this as a method so that the parsing may be updated when the user has added additional, custom types
$refreshTypeParser(dataType) {
parserStore.refresh(dataType);
}
$clearTypeParser() {
parserStore.clear();
}
connect(config) {
return new Promise((resolve, reject) => {
const connectionConfig = {
userName: config.username,
password: config.password,
server: config.host,
options: {
port: config.port,
database: config.database
}
};
if (config.dialectOptions) {
// only set port if no instance name was provided
if (config.dialectOptions.instanceName) {
delete connectionConfig.options.port;
}
// The 'tedious' driver needs domain property to be in the main Connection config object
if(config.dialectOptions.domain) {
connectionConfig.domain = config.dialectOptions.domain;
}
for (const key of Object.keys(config.dialectOptions)) {
connectionConfig.options[key] = config.dialectOptions[key];
}
}
const connection = new this.lib.Connection(connectionConfig);
const connectionLock = new ResourceLock(connection);
connection.lib = this.lib;
connection.on('connect', err => {
if (!err) {
debug(`connection acquired`);
resolve(connectionLock);
return;
}
if (!err.code) {
reject(new sequelizeErrors.ConnectionError(err));
return;
}
switch (err.code) {
case 'ESOCKET':
if (_.includes(err.message, 'connect EHOSTUNREACH')) {
reject(new sequelizeErrors.HostNotReachableError(err));
} else if (_.includes(err.message, 'connect ECONNREFUSED')) {
reject(new sequelizeErrors.ConnectionRefusedError(err));
} else {
reject(new sequelizeErrors.ConnectionError(err));
}
break;
case 'ECONNREFUSED':
reject(new sequelizeErrors.ConnectionRefusedError(err));
break;
case 'ER_ACCESS_DENIED_ERROR':
reject(new sequelizeErrors.AccessDeniedError(err));
break;
case 'ENOTFOUND':
reject(new sequelizeErrors.HostNotFoundError(err));
break;
case 'EHOSTUNREACH':
reject(new sequelizeErrors.HostNotReachableError(err));
break;
case 'EINVAL':
reject(new sequelizeErrors.InvalidConnectionError(err));
break;
default:
reject(new sequelizeErrors.ConnectionError(err));
break;
}
});
if (config.pool.handleDisconnects) {
connection.on('error', err => {
switch (err.code) {
case 'ESOCKET':
case 'ECONNRESET':
this.pool.destroy(connectionLock);
}
});
}
});
}
disconnect(connectionLock) {
const connection = connectionLock.unwrap();
// Dont disconnect a connection that is already disconnected
if (!!connection.closed) {
return Promise.resolve();
}
return new Promise(resolve => {
connection.on('end', resolve);
connection.close();
debug(`connection closed`);
});
}
validate(connectionLock) {
const connection = connectionLock.unwrap();
return connection && connection.loggedIn;
}
}
module.exports = ConnectionManager;
module.exports.ConnectionManager = ConnectionManager;
module.exports.default = ConnectionManager;