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

Commit f50469b6 by Felix Becker Committed by Mick Hansen

ES6 refactor: dialects / SQLite (#6051)

* ES6 refactor of SQLite ConnectionManager

* ES6 refactor of SQLiteDialect

* Make sqlite Query an ES6 class

* ES6 refactor of SQLite Query

* ES6 refactor of SQLite QueryGenerator

* ES6 refactor of SQLite QueryInterface
1 parent 3107396d
'use strict'; 'use strict';
var AbstractConnectionManager = require('../abstract/connection-manager') const AbstractConnectionManager = require('../abstract/connection-manager');
, ConnectionManager const Promise = require('../../promise');
, Utils = require('../../utils') const dataTypes = require('../../data-types').sqlite;
, Promise = require('../../promise') const sequelizeErrors = require('../../errors');
, dataTypes = require('../../data-types').sqlite const parserStore = require('../parserStore')('sqlite');
, sequelizeErrors = require('../../errors')
, parserStore = require('../parserStore')('sqlite');
ConnectionManager = function(dialect, sequelize) { class ConnectionManager extends AbstractConnectionManager {
this.sequelize = sequelize; constructor(dialect, sequelize) {
this.config = sequelize.config; super(dialect, sequelize);
this.dialect = dialect; this.sequelize = sequelize;
this.dialectName = this.sequelize.options.dialect; this.config = sequelize.config;
this.connections = {}; this.dialect = dialect;
this.dialectName = this.sequelize.options.dialect;
this.connections = {};
// We attempt to parse file location from a connection uri but we shouldn't match sequelize default host. // We attempt to parse file location from a connection uri but we shouldn't match sequelize default host.
if (this.sequelize.options.host === 'localhost') delete this.sequelize.options.host; if (this.sequelize.options.host === 'localhost') delete this.sequelize.options.host;
try { try {
this.lib = require(sequelize.config.dialectModulePath || 'sqlite3').verbose(); this.lib = require(sequelize.config.dialectModulePath || 'sqlite3').verbose();
} catch (err) { } catch (err) {
if (err.code === 'MODULE_NOT_FOUND') { if (err.code === 'MODULE_NOT_FOUND') {
throw new Error('Please install sqlite3 package manually'); throw new Error('Please install sqlite3 package manually');
}
throw err;
} }
throw err;
}
this.refreshTypeParser(dataTypes);
};
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype); this.refreshTypeParser(dataTypes);
}
// 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.getConnection = function(options) { getConnection(options) {
var self = this; options = options || {};
options = options || {}; options.uuid = options.uuid || 'default';
options.uuid = options.uuid || 'default'; options.inMemory = ((this.sequelize.options.storage || this.sequelize.options.host || ':memory:') === ':memory:') ? 1 : 0;
options.inMemory = ((self.sequelize.options.storage || self.sequelize.options.host || ':memory:') === ':memory:') ? 1 : 0;
var dialectOptions = self.sequelize.options.dialectOptions; const dialectOptions = this.sequelize.options.dialectOptions;
options.readWriteMode = dialectOptions && dialectOptions.mode; options.readWriteMode = dialectOptions && dialectOptions.mode;
if (self.connections[options.inMemory || options.uuid]) { if (this.connections[options.inMemory || options.uuid]) {
return Promise.resolve(self.connections[options.inMemory || options.uuid]); return Promise.resolve(this.connections[options.inMemory || options.uuid]);
} }
return new Promise(function (resolve, reject) { return new Promise((resolve, reject) => {
self.connections[options.inMemory || options.uuid] = new self.lib.Database( this.connections[options.inMemory || options.uuid] = new this.lib.Database(
self.sequelize.options.storage || self.sequelize.options.host || ':memory:', this.sequelize.options.storage || this.sequelize.options.host || ':memory:',
options.readWriteMode || (self.lib.OPEN_READWRITE | self.lib.OPEN_CREATE), // default mode options.readWriteMode || (this.lib.OPEN_READWRITE | this.lib.OPEN_CREATE), // default mode
function(err) { err => {
if (err) { if (err) {
if (err.code === 'SQLITE_CANTOPEN') return reject(new sequelizeErrors.ConnectionError(err)); if (err.code === 'SQLITE_CANTOPEN') return reject(new sequelizeErrors.ConnectionError(err));
return reject(new sequelizeErrors.ConnectionError(err)); return reject(new sequelizeErrors.ConnectionError(err));
}
resolve(this.connections[options.inMemory || options.uuid]);
} }
resolve(self.connections[options.inMemory || options.uuid]); );
}).tap(connection => {
if (this.sequelize.options.foreignKeys !== false) {
// Make it possible to define and use foreign key constraints unless
// explicitly disallowed. It's still opt-in per relation
connection.run('PRAGMA FOREIGN_KEYS=ON');
} }
); });
}).tap(function (connection) { }
if (self.sequelize.options.foreignKeys !== false) {
// Make it possible to define and use foreign key constraints unless
// explicitly disallowed. It's still opt-in per relation
connection.run('PRAGMA FOREIGN_KEYS=ON');
}
});
};
ConnectionManager.prototype.releaseConnection = function(connection, force) { releaseConnection(connection, force) {
if (connection.filename === ':memory:' && force !== true) return; if (connection.filename === ':memory:' && force !== true) return;
if (connection.uuid) { if (connection.uuid) {
connection.close(); connection.close();
delete this.connections[connection.uuid]; delete this.connections[connection.uuid];
}
} }
}; }
module.exports = ConnectionManager; module.exports = ConnectionManager;
module.exports.ConnectionManager = ConnectionManager;
module.exports.default = ConnectionManager;
'use strict'; 'use strict';
var _ = require('lodash') const _ = require('lodash');
, Abstract = require('../abstract') const AbstractDialect = require('../abstract');
, ConnectionManager = require('./connection-manager') const ConnectionManager = require('./connection-manager');
, Query = require('./query') const Query = require('./query');
, QueryGenerator = require('./query-generator') const QueryGenerator = require('./query-generator');
, DataTypes = require('../../data-types').sqlite; const DataTypes = require('../../data-types').sqlite;
var SqliteDialect = function(sequelize) { class SqliteDialect extends AbstractDialect {
this.sequelize = sequelize; constructor(sequelize) {
this.connectionManager = new ConnectionManager(this, sequelize); super();
this.QueryGenerator = _.extend({}, QueryGenerator, { this.sequelize = sequelize;
options: sequelize.options, this.connectionManager = new ConnectionManager(this, sequelize);
_dialect: this, this.QueryGenerator = _.extend({}, QueryGenerator, {
sequelize: sequelize options: sequelize.options,
}); _dialect: this,
}; sequelize
});
}
}
SqliteDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supports), { SqliteDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype.supports), {
'DEFAULT': false, 'DEFAULT': false,
'DEFAULT VALUES': true, 'DEFAULT VALUES': true,
'UNION ALL': false, 'UNION ALL': false,
...@@ -43,3 +46,5 @@ SqliteDialect.prototype.TICK_CHAR_LEFT = SqliteDialect.prototype.TICK_CHAR; ...@@ -43,3 +46,5 @@ SqliteDialect.prototype.TICK_CHAR_LEFT = SqliteDialect.prototype.TICK_CHAR;
SqliteDialect.prototype.TICK_CHAR_RIGHT = SqliteDialect.prototype.TICK_CHAR; SqliteDialect.prototype.TICK_CHAR_RIGHT = SqliteDialect.prototype.TICK_CHAR;
module.exports = SqliteDialect; module.exports = SqliteDialect;
module.exports.SqliteDialect = SqliteDialect;
module.exports.default = SqliteDialect;
'use strict'; 'use strict';
var Utils = require('../../utils') const Utils = require('../../utils');
, Promise = require('../../promise'); const Promise = require('../../promise');
/** /**
Returns an object that treats SQLite's inabilities to do certain queries. Returns an object that treats SQLite's inabilities to do certain queries.
...@@ -25,21 +25,20 @@ var Utils = require('../../utils') ...@@ -25,21 +25,20 @@ var Utils = require('../../utils')
@since 1.6.0 @since 1.6.0
*/ */
var removeColumn = function(tableName, attributeName, options) { function removeColumn(tableName, attributeName, options) {
var self = this;
options = options || {}; options = options || {};
return this.describeTable(tableName, options).then(function(fields) { /* jshint validthis:true */
return this.describeTable(tableName, options).then(fields => {
delete fields[attributeName]; delete fields[attributeName];
var sql = self.QueryGenerator.removeColumnQuery(tableName, fields) const sql = this.QueryGenerator.removeColumnQuery(tableName, fields);
, subQueries = sql.split(';').filter(function(q) { return q !== ''; }); const subQueries = sql.split(';').filter(q => q !== '');
return Promise.each(subQueries, function(subQuery) { return Promise.each(subQueries, subQuery => this.sequelize.query(subQuery + ';', Utils._.assign({raw: true}, options)));
return self.sequelize.query(subQuery + ';', Utils._.assign({raw: true}, options));
});
}); });
}; }
exports.removeColumn = removeColumn;
/** /**
A wrapper that fixes SQLite's inability to change columns from existing tables. A wrapper that fixes SQLite's inability to change columns from existing tables.
...@@ -56,22 +55,21 @@ var removeColumn = function(tableName, attributeName, options) { ...@@ -56,22 +55,21 @@ var removeColumn = function(tableName, attributeName, options) {
@since 1.6.0 @since 1.6.0
*/ */
var changeColumn = function(tableName, attributes, options) { function changeColumn(tableName, attributes, options) {
var attributeName = Utils._.keys(attributes)[0] const attributeName = Object.keys(attributes)[0];
, self = this;
options = options || {}; options = options || {};
return this.describeTable(tableName, options).then(function(fields) { /* jshint validthis:true */
return this.describeTable(tableName, options).then(fields => {
fields[attributeName] = attributes[attributeName]; fields[attributeName] = attributes[attributeName];
var sql = self.QueryGenerator.removeColumnQuery(tableName, fields) const sql = this.QueryGenerator.removeColumnQuery(tableName, fields);
, subQueries = sql.split(';').filter(function(q) { return q !== ''; }); const subQueries = sql.split(';').filter(q => q !== '');
return Promise.each(subQueries, function(subQuery) { return Promise.each(subQueries, subQuery => this.sequelize.query(subQuery + ';', Utils._.assign({raw: true}, options)));
return self.sequelize.query(subQuery + ';', Utils._.assign({raw: true}, options));
});
}); });
}; }
exports.changeColumn = changeColumn;
/** /**
A wrapper that fixes SQLite's inability to rename columns from existing tables. A wrapper that fixes SQLite's inability to rename columns from existing tables.
...@@ -89,25 +87,18 @@ var changeColumn = function(tableName, attributes, options) { ...@@ -89,25 +87,18 @@ var changeColumn = function(tableName, attributes, options) {
@since 1.6.0 @since 1.6.0
*/ */
var renameColumn = function(tableName, attrNameBefore, attrNameAfter, options) { function renameColumn(tableName, attrNameBefore, attrNameAfter, options) {
var self = this;
options = options || {}; options = options || {};
return this.describeTable(tableName, options).then(function(fields) { /* jshint validthis:true */
return this.describeTable(tableName, options).then(fields => {
fields[attrNameAfter] = Utils._.clone(fields[attrNameBefore]); fields[attrNameAfter] = Utils._.clone(fields[attrNameBefore]);
delete fields[attrNameBefore]; delete fields[attrNameBefore];
var sql = self.QueryGenerator.renameColumnQuery(tableName, attrNameBefore, attrNameAfter, fields) const sql = this.QueryGenerator.renameColumnQuery(tableName, attrNameBefore, attrNameAfter, fields);
, subQueries = sql.split(';').filter(function(q) { return q !== ''; }); const subQueries = sql.split(';').filter(q => q !== '');
return Promise.each(subQueries, function(subQuery) { return Promise.each(subQueries, subQuery => this.sequelize.query(subQuery + ';', Utils._.assign({raw: true}, options)));
return self.sequelize.query(subQuery + ';', Utils._.assign({raw: true}, options));
});
}); });
}; }
exports.renameColumn = renameColumn;
module.exports = {
removeColumn: removeColumn,
changeColumn: changeColumn,
renameColumn: renameColumn
};
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!