不要怂,就是干,撸起袖子干!

Commit 7275c7c6 by Felix Becker Committed by Jan Aagaard Meier

ES6 refactor: dialects / MSSQL (#6049)

* ES6 refactor of MSSQL ConnectionManager

classes, let, const, arrow functions, export default

* Make Mssql Query an ES6 class

* ES6 refactor of Mssql Query

let, const, arrow functions, property shorthands, for of, export default
1 parent 3ff27d10
'use strict';
var AbstractConnectionManager = require('../abstract/connection-manager')
, ConnectionManager
, ResourceLock = require('./resource-lock')
, Utils = require('../../utils')
, Promise = require('../../promise')
, sequelizeErrors = require('../../errors')
, parserStore = require('../parserStore')('mssql')
, _ = require('lodash');
ConnectionManager = function(dialect, sequelize) {
AbstractConnectionManager.call(this, dialect, sequelize);
const AbstractConnectionManager = require('../abstract/connection-manager');
const ResourceLock = require('./resource-lock');
const Promise = require('../../promise');
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;
......@@ -22,24 +21,20 @@ ConnectionManager = function(dialect, sequelize) {
}
throw err;
}
};
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
}
// Expose this as a method so that the parsing may be updated when the user has added additional, custom types
ConnectionManager.prototype.$refreshTypeParser = function (dataType) {
// 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);
};
}
ConnectionManager.prototype.$clearTypeParser = function () {
$clearTypeParser() {
parserStore.clear();
};
}
ConnectionManager.prototype.connect = function(config) {
var self = this;
return new Promise(function (resolve, reject) {
var connectionConfig = {
connect(config) {
return new Promise((resolve, reject) => {
const connectionConfig = {
userName: config.username,
password: config.password,
server: config.host,
......@@ -60,16 +55,16 @@ ConnectionManager.prototype.connect = function(config) {
connectionConfig.domain = config.dialectOptions.domain;
}
Object.keys(config.dialectOptions).forEach(function(key) {
for (const key of Object.keys(config.dialectOptions)) {
connectionConfig.options[key] = config.dialectOptions[key];
});
}
}
var connection = new self.lib.Connection(connectionConfig)
, connectionLock = new ResourceLock(connection);
connection.lib = self.lib;
const connection = new this.lib.Connection(connectionConfig);
const connectionLock = new ResourceLock(connection);
connection.lib = this.lib;
connection.on('connect', function(err) {
connection.on('connect', err => {
if (!err) {
resolve(connectionLock);
return;
......@@ -112,36 +107,39 @@ ConnectionManager.prototype.connect = function(config) {
});
if (config.pool.handleDisconnects) {
connection.on('error', function (err) {
connection.on('error', err => {
switch (err.code) {
case 'ESOCKET':
case 'ECONNRESET':
self.pool.destroy(connectionLock);
this.pool.destroy(connectionLock);
}
});
}
});
};
}
ConnectionManager.prototype.disconnect = function(connectionLock) {
var connection = connectionLock.unwrap();
disconnect(connectionLock) {
const connection = connectionLock.unwrap();
// Dont disconnect a connection that is already disconnected
if (!!connection.closed) {
return Promise.resolve();
}
return new Promise(function (resolve, reject) {
return new Promise(resolve => {
connection.on('end', resolve);
connection.close();
});
};
}
ConnectionManager.prototype.validate = function(connectionLock) {
var connection = connectionLock.unwrap();
validate(connectionLock) {
const connection = connectionLock.unwrap();
return connection && connection.loggedIn;
};
}
}
module.exports = ConnectionManager;
module.exports.ConnectionManager = ConnectionManager;
module.exports.default = ConnectionManager;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!