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

Commit dc5a38f2 by Felix Becker Committed by Jan Aagaard Meier

ES6 refactor: ModelManager (#6045)

* Make ModelManager an ES6 class

* ES6 refactor of ModelManager
1 parent fe4cded5
Showing with 34 additions and 36 deletions
'use strict'; 'use strict';
var Toposort = require('toposort-class') const Toposort = require('toposort-class');
, _ = require('lodash'); const _ = require('lodash');
var ModelManager = function(sequelize) { class ModelManager {
constructor(sequelize) {
this.models = []; this.models = [];
this.sequelize = sequelize; this.sequelize = sequelize;
}; }
ModelManager.prototype.addModel = function(model) { addModel(model) {
this.models.push(model); this.models.push(model);
this.sequelize.models[model.name] = model; this.sequelize.models[model.name] = model;
return model; return model;
}; }
ModelManager.prototype.removeModel = function(model) { removeModel(model) {
this.models = this.models.filter(function($model) { this.models = this.models.filter($model => $model.name !== model.name);
return $model.name !== model.name;
});
delete this.sequelize.models[model.name]; delete this.sequelize.models[model.name];
}; }
ModelManager.prototype.getModel = function(against, options) { getModel(against, options) {
options = _.defaults(options || {}, { options = _.defaults(options || {}, {
attribute: 'name' attribute: 'name'
}); });
var model = this.models.filter(function(model) { const model = this.models.filter(model => model[options.attribute] === against);
return model[options.attribute] === against;
});
return !!model ? model[0] : null; return !!model ? model[0] : null;
}; }
ModelManager.prototype.__defineGetter__('all', function() { get all() {
return this.models; return this.models;
}); }
/** /**
* Iterate over Models in an order suitable for e.g. creating tables. Will * Iterate over Models in an order suitable for e.g. creating tables. Will
* take foreign key constraints into account so that dependencies are visited * take foreign key constraints into account so that dependencies are visited
* before dependents. * before dependents.
*/ */
ModelManager.prototype.forEachModel = function(iterator, options) { forEachModel(iterator, options) {
var models = {} const models = {};
, sorter = new Toposort() const sorter = new Toposort();
, sorted let sorted;
, dep; let dep;
options = _.defaults(options || {}, { options = _.defaults(options || {}, {
reverse: true reverse: true
}); });
this.models.forEach(function(model) { for (const model of this.models) {
var deps = [] let deps = [];
, tableName = model.getTableName(); let tableName = model.getTableName();
if (_.isObject(tableName)) { if (_.isObject(tableName)) {
tableName = tableName.schema + '.' + tableName.tableName; tableName = tableName.schema + '.' + tableName.tableName;
...@@ -64,9 +61,9 @@ ModelManager.prototype.forEachModel = function(iterator, options) { ...@@ -64,9 +61,9 @@ ModelManager.prototype.forEachModel = function(iterator, options) {
models[tableName] = model; models[tableName] = model;
for (var attrName in model.rawAttributes) { for (const attrName in model.rawAttributes) {
if (model.rawAttributes.hasOwnProperty(attrName)) { if (model.rawAttributes.hasOwnProperty(attrName)) {
var attribute = model.rawAttributes[attrName]; const attribute = model.rawAttributes[attrName];
if (attribute.references) { if (attribute.references) {
dep = attribute.references.model; dep = attribute.references.model;
...@@ -80,20 +77,21 @@ ModelManager.prototype.forEachModel = function(iterator, options) { ...@@ -80,20 +77,21 @@ ModelManager.prototype.forEachModel = function(iterator, options) {
} }
} }
deps = deps.filter(function(dep) { deps = deps.filter(dep => tableName !== dep);
return tableName !== dep;
});
sorter.add(tableName, deps); sorter.add(tableName, deps);
}); }
sorted = sorter.sort(); sorted = sorter.sort();
if (options.reverse) { if (options.reverse) {
sorted = sorted.reverse(); sorted = sorted.reverse();
} }
sorted.forEach(function(name) { for (const name of sorted) {
iterator(models[name], name); iterator(models[name], name);
}); }
}; }
}
module.exports = ModelManager; module.exports = ModelManager;
module.exports.ModelManager = ModelManager;
module.exports.default = ModelManager;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!