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

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 82 additions and 84 deletions
'use strict'; 'use strict';
var Toposort = require('toposort-class') const Toposort = require('toposort-class');
, _ = require('lodash'); const _ = require('lodash');
var ModelManager = function(sequelize) {
this.models = [];
this.sequelize = sequelize;
};
ModelManager.prototype.addModel = function(model) {
this.models.push(model);
this.sequelize.models[model.name] = model;
return model;
};
ModelManager.prototype.removeModel = function(model) {
this.models = this.models.filter(function($model) {
return $model.name !== model.name;
});
delete this.sequelize.models[model.name];
};
ModelManager.prototype.getModel = function(against, options) {
options = _.defaults(options || {}, {
attribute: 'name'
});
var model = this.models.filter(function(model) {
return model[options.attribute] === against;
});
return !!model ? model[0] : null;
};
ModelManager.prototype.__defineGetter__('all', function() {
return this.models;
});
/**
* Iterate over Models in an order suitable for e.g. creating tables. Will
* take foreign key constraints into account so that dependencies are visited
* before dependents.
*/
ModelManager.prototype.forEachModel = function(iterator, options) {
var models = {}
, sorter = new Toposort()
, sorted
, dep;
options = _.defaults(options || {}, {
reverse: true
});
this.models.forEach(function(model) {
var deps = []
, tableName = model.getTableName();
if (_.isObject(tableName)) {
tableName = tableName.schema + '.' + tableName.tableName;
}
models[tableName] = model; class ModelManager {
constructor(sequelize) {
this.models = [];
this.sequelize = sequelize;
}
for (var attrName in model.rawAttributes) { addModel(model) {
if (model.rawAttributes.hasOwnProperty(attrName)) { this.models.push(model);
var attribute = model.rawAttributes[attrName]; this.sequelize.models[model.name] = model;
if (attribute.references) { return model;
dep = attribute.references.model; }
if (_.isObject(dep)) { removeModel(model) {
dep = dep.schema + '.' + dep.tableName; this.models = this.models.filter($model => $model.name !== model.name);
}
delete this.sequelize.models[model.name];
}
getModel(against, options) {
options = _.defaults(options || {}, {
attribute: 'name'
});
const model = this.models.filter(model => model[options.attribute] === against);
return !!model ? model[0] : null;
}
get all() {
return this.models;
}
/**
* Iterate over Models in an order suitable for e.g. creating tables. Will
* take foreign key constraints into account so that dependencies are visited
* before dependents.
*/
forEachModel(iterator, options) {
const models = {};
const sorter = new Toposort();
let sorted;
let dep;
options = _.defaults(options || {}, {
reverse: true
});
for (const model of this.models) {
let deps = [];
let tableName = model.getTableName();
if (_.isObject(tableName)) {
tableName = tableName.schema + '.' + tableName.tableName;
}
models[tableName] = model;
for (const attrName in model.rawAttributes) {
if (model.rawAttributes.hasOwnProperty(attrName)) {
const attribute = model.rawAttributes[attrName];
if (attribute.references) {
dep = attribute.references.model;
deps.push(dep); if (_.isObject(dep)) {
dep = dep.schema + '.' + dep.tableName;
}
deps.push(dep);
}
} }
} }
}
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();
}
for (const name of sorted) {
iterator(models[name], name);
}
} }
sorted.forEach(function(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!