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

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'; 'use strict';
var AbstractConnectionManager = require('../abstract/connection-manager') const AbstractConnectionManager = require('../abstract/connection-manager');
, ConnectionManager const ResourceLock = require('./resource-lock');
, ResourceLock = require('./resource-lock') const Promise = require('../../promise');
, Utils = require('../../utils') const sequelizeErrors = require('../../errors');
, Promise = require('../../promise') const parserStore = require('../parserStore')('mssql');
, sequelizeErrors = require('../../errors') const _ = require('lodash');
, parserStore = require('../parserStore')('mssql')
, _ = require('lodash'); class ConnectionManager extends AbstractConnectionManager {
constructor(dialect, sequelize) {
ConnectionManager = function(dialect, sequelize) { super(dialect, sequelize);
AbstractConnectionManager.call(this, dialect, sequelize);
this.sequelize = sequelize; this.sequelize = sequelize;
this.sequelize.config.port = this.sequelize.config.port || 1433; this.sequelize.config.port = this.sequelize.config.port || 1433;
...@@ -22,24 +21,20 @@ ConnectionManager = function(dialect, sequelize) { ...@@ -22,24 +21,20 @@ ConnectionManager = function(dialect, sequelize) {
} }
throw err; 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 // 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) { $refreshTypeParser(dataType) {
parserStore.refresh(dataType); parserStore.refresh(dataType);
}; }
ConnectionManager.prototype.$clearTypeParser = function () { $clearTypeParser() {
parserStore.clear(); parserStore.clear();
}; }
ConnectionManager.prototype.connect = function(config) { connect(config) {
var self = this; return new Promise((resolve, reject) => {
return new Promise(function (resolve, reject) { const connectionConfig = {
var connectionConfig = {
userName: config.username, userName: config.username,
password: config.password, password: config.password,
server: config.host, server: config.host,
...@@ -60,16 +55,16 @@ ConnectionManager.prototype.connect = function(config) { ...@@ -60,16 +55,16 @@ ConnectionManager.prototype.connect = function(config) {
connectionConfig.domain = config.dialectOptions.domain; 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]; connectionConfig.options[key] = config.dialectOptions[key];
}); }
} }
var connection = new self.lib.Connection(connectionConfig) const connection = new this.lib.Connection(connectionConfig);
, connectionLock = new ResourceLock(connection); const connectionLock = new ResourceLock(connection);
connection.lib = self.lib; connection.lib = this.lib;
connection.on('connect', function(err) { connection.on('connect', err => {
if (!err) { if (!err) {
resolve(connectionLock); resolve(connectionLock);
return; return;
...@@ -112,36 +107,39 @@ ConnectionManager.prototype.connect = function(config) { ...@@ -112,36 +107,39 @@ ConnectionManager.prototype.connect = function(config) {
}); });
if (config.pool.handleDisconnects) { if (config.pool.handleDisconnects) {
connection.on('error', function (err) { connection.on('error', err => {
switch (err.code) { switch (err.code) {
case 'ESOCKET': case 'ESOCKET':
case 'ECONNRESET': case 'ECONNRESET':
self.pool.destroy(connectionLock); this.pool.destroy(connectionLock);
} }
}); });
} }
}); });
}; }
ConnectionManager.prototype.disconnect = function(connectionLock) { disconnect(connectionLock) {
var connection = connectionLock.unwrap(); const connection = connectionLock.unwrap();
// Dont disconnect a connection that is already disconnected // Dont disconnect a connection that is already disconnected
if (!!connection.closed) { if (!!connection.closed) {
return Promise.resolve(); return Promise.resolve();
} }
return new Promise(function (resolve, reject) { return new Promise(resolve => {
connection.on('end', resolve); connection.on('end', resolve);
connection.close(); connection.close();
}); });
}; }
ConnectionManager.prototype.validate = function(connectionLock) { validate(connectionLock) {
var connection = connectionLock.unwrap(); const connection = connectionLock.unwrap();
return connection && connection.loggedIn; return connection && connection.loggedIn;
}; }
}
module.exports = ConnectionManager; 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!