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

Commit a57bd01d by sdepold

renamed ModelManager to ModelFactoryManager

1 parent 8f84cc19
# v1.3.1 #
- [REFACTORING] renamed ModelManager to ModelFactoryManager
# v1.3.0 # # v1.3.0 #
- [REFACTORING] Model#all is now a function and not a getter. - [REFACTORING] Model#all is now a function and not a getter.
- [REFACTORING] Renamed ModelDefinition to ModelFactory - [REFACTORING] Renamed ModelDefinition to ModelFactory
......
...@@ -48,7 +48,7 @@ module.exports = (function() { ...@@ -48,7 +48,7 @@ module.exports = (function() {
combinedTableAttributes[this.identifier] = {type:DataTypes.INTEGER, primaryKey: true} combinedTableAttributes[this.identifier] = {type:DataTypes.INTEGER, primaryKey: true}
combinedTableAttributes[this.foreignIdentifier] = {type:DataTypes.INTEGER, primaryKey: true} combinedTableAttributes[this.foreignIdentifier] = {type:DataTypes.INTEGER, primaryKey: true}
this.connectorModel = this.source.modelManager.sequelize.define(this.combinedName, combinedTableAttributes) this.connectorModel = this.source.modelFactoryManager.sequelize.define(this.combinedName, combinedTableAttributes)
if(!this.isSelfAssociation) this.target.associations[this.associationAccessor].connectorModel = this.connectorModel if(!this.isSelfAssociation) this.target.associations[this.associationAccessor].connectorModel = this.connectorModel
this.connectorModel.sync() this.connectorModel.sync()
......
...@@ -119,7 +119,7 @@ module.exports = (function() { ...@@ -119,7 +119,7 @@ module.exports = (function() {
var self = this var self = this
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var storedModel = self.sequelize.modelManager.getModel('SequelizeMeta') var storedModel = self.sequelize.modelFactoryManager.getModel('SequelizeMeta')
, SequelizeMeta = storedModel , SequelizeMeta = storedModel
if(!storedModel) { if(!storedModel) {
......
module.exports = (function() { module.exports = (function() {
var ModelManager = function(sequelize) { var ModelFactoryManager = function(sequelize) {
this.models = [] this.models = []
this.sequelize = sequelize this.sequelize = sequelize
} }
ModelManager.prototype.addModel = function(model) { ModelFactoryManager.prototype.addModel = function(model) {
this.models.push(model) this.models.push(model)
return model return model
} }
ModelManager.prototype.removeModel = function(model) { ModelFactoryManager.prototype.removeModel = function(model) {
this.models = this.models.filter(function(_model) { this.models = this.models.filter(function(_model) {
return _model.name != model.name return _model.name != model.name
}) })
} }
ModelManager.prototype.getModel = function(modelName) { ModelFactoryManager.prototype.getModel = function(modelName) {
var model = this.models.filter(function(model) { var model = this.models.filter(function(model) {
return model.name == modelName return model.name == modelName
}) })
...@@ -24,9 +24,9 @@ module.exports = (function() { ...@@ -24,9 +24,9 @@ module.exports = (function() {
return !!model ? model[0] : null return !!model ? model[0] : null
} }
ModelManager.prototype.__defineGetter__('all', function() { ModelFactoryManager.prototype.__defineGetter__('all', function() {
return this.models return this.models
}) })
return ModelManager return ModelFactoryManager
})() })()
...@@ -19,7 +19,7 @@ module.exports = (function() { ...@@ -19,7 +19,7 @@ module.exports = (function() {
this.name = name this.name = name
this.tableName = this.options.freezeTableName ? name : Utils.pluralize(name) this.tableName = this.options.freezeTableName ? name : Utils.pluralize(name)
this.rawAttributes = attributes this.rawAttributes = attributes
this.modelManager = null // defined in init function this.modelFactoryManager = null // defined in init function
this.associations = {} this.associations = {}
// extract validation // extract validation
...@@ -33,7 +33,7 @@ module.exports = (function() { ...@@ -33,7 +33,7 @@ module.exports = (function() {
}) })
Object.defineProperty(ModelFactory.prototype, 'QueryInterface', { Object.defineProperty(ModelFactory.prototype, 'QueryInterface', {
get: function() { return this.modelManager.sequelize.getQueryInterface() } get: function() { return this.modelFactoryManager.sequelize.getQueryInterface() }
}) })
Object.defineProperty(ModelFactory.prototype, 'QueryGenerator', { Object.defineProperty(ModelFactory.prototype, 'QueryGenerator', {
...@@ -48,8 +48,8 @@ module.exports = (function() { ...@@ -48,8 +48,8 @@ module.exports = (function() {
get: function() { return this.primaryKeyCount > 0 } get: function() { return this.primaryKeyCount > 0 }
}) })
ModelFactory.prototype.init = function(modelManager) { ModelFactory.prototype.init = function(modelFactoryManager) {
this.modelManager = modelManager this.modelFactoryManager = modelFactoryManager
addDefaultAttributes.call(this) addDefaultAttributes.call(this)
addOptionalClassMethods.call(this) addOptionalClassMethods.call(this)
...@@ -189,7 +189,7 @@ module.exports = (function() { ...@@ -189,7 +189,7 @@ module.exports = (function() {
var query = function() { var query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg }) var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.modelManager.sequelize , s = this.modelFactoryManager.sequelize
// add this as the second argument // add this as the second argument
if(arguments.length == 1) args.push(this) if(arguments.length == 1) args.push(this)
......
...@@ -22,7 +22,7 @@ module.exports = (function() { ...@@ -22,7 +22,7 @@ module.exports = (function() {
Utils._.extend(Model.prototype, Mixin.prototype) Utils._.extend(Model.prototype, Mixin.prototype)
Object.defineProperty(Model.prototype, 'sequelize', { Object.defineProperty(Model.prototype, 'sequelize', {
get: function(){ return this.__factory.modelManager.sequelize } get: function(){ return this.__factory.modelFactoryManager.sequelize }
}) })
Object.defineProperty(Model.prototype, 'QueryInterface', { Object.defineProperty(Model.prototype, 'QueryInterface', {
...@@ -256,7 +256,7 @@ module.exports = (function() { ...@@ -256,7 +256,7 @@ module.exports = (function() {
var query = function() { var query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg }) var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.__factory.modelManager.sequelize , s = this.__factory.modelFactoryManager.sequelize
args.push(this) args.push(this)
return s.query.apply(s, args) return s.query.apply(s, args)
......
var Utils = require("./utils") var Utils = require("./utils")
, ModelFactory = require("./model-factory") , ModelFactory = require("./model-factory")
, DataTypes = require('./data-types') , DataTypes = require('./data-types')
, ModelManager = require("./model-manager") , ModelFactoryManager = require("./model-factory-manager")
, Migrator = require("./migrator") , Migrator = require("./migrator")
, QueryInterface = require("./query-interface") , QueryInterface = require("./query-interface")
...@@ -26,7 +26,7 @@ module.exports = (function() { ...@@ -26,7 +26,7 @@ module.exports = (function() {
var ConnectorManager = require("./dialects/" + this.options.dialect + "/connector-manager") var ConnectorManager = require("./dialects/" + this.options.dialect + "/connector-manager")
this.modelManager = new ModelManager(this) this.modelFactoryManager = new ModelFactoryManager(this)
this.connectorManager = new ConnectorManager(this, this.config) this.connectorManager = new ConnectorManager(this, this.config)
} }
...@@ -55,7 +55,7 @@ module.exports = (function() { ...@@ -55,7 +55,7 @@ module.exports = (function() {
var factory = new ModelFactory(modelName, attributes, options) var factory = new ModelFactory(modelName, attributes, options)
this.modelManager.addModel(factory.init(this.modelManager)) this.modelFactoryManager.addModel(factory.init(this.modelFactoryManager))
return factory return factory
} }
...@@ -89,7 +89,7 @@ module.exports = (function() { ...@@ -89,7 +89,7 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer var chainer = new Utils.QueryChainer
self.modelManager.models.forEach(function(model) { chainer.add(model.sync(options)) }) self.modelFactoryManager.models.forEach(function(model) { chainer.add(model.sync(options)) })
chainer chainer
.run() .run()
...@@ -104,7 +104,7 @@ module.exports = (function() { ...@@ -104,7 +104,7 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer var chainer = new Utils.QueryChainer
self.modelManager.models.forEach(function(model) { chainer.add(model.drop()) }) self.modelFactoryManager.models.forEach(function(model) { chainer.add(model.drop()) })
chainer chainer
.run() .run()
......
...@@ -102,7 +102,7 @@ describe('HasMany', function() { ...@@ -102,7 +102,7 @@ describe('HasMany', function() {
expect(Task.attributes.UserId).toBeUndefined() expect(Task.attributes.UserId).toBeUndefined()
expect(User.attributes.UserId).toBeUndefined() expect(User.attributes.UserId).toBeUndefined()
var models = sequelize.modelManager.models.filter(function(model) { var models = sequelize.modelFactoryManager.models.filter(function(model) {
return (model.tableName == (Task.tableName + User.tableName)) return (model.tableName == (Task.tableName + User.tableName))
}) })
...@@ -122,7 +122,7 @@ describe('HasMany', function() { ...@@ -122,7 +122,7 @@ describe('HasMany', function() {
expect(Task.attributes.user_id).toBeUndefined() expect(Task.attributes.user_id).toBeUndefined()
expect(User.attributes.user_id).toBeUndefined() expect(User.attributes.user_id).toBeUndefined()
var models = sequelize.modelManager.models.filter(function(model) { var models = sequelize.modelFactoryManager.models.filter(function(model) {
return (model.tableName == (Task.tableName + User.tableName)) return (model.tableName == (Task.tableName + User.tableName))
}) })
...@@ -136,7 +136,7 @@ describe('HasMany', function() { ...@@ -136,7 +136,7 @@ describe('HasMany', function() {
User.hasMany(Task, { foreignKey: 'person_id' }) User.hasMany(Task, { foreignKey: 'person_id' })
Task.hasMany(User, { foreignKey: 'work_item_id' }) Task.hasMany(User, { foreignKey: 'work_item_id' })
var models = sequelize.modelManager.models.filter(function(model) { var models = sequelize.modelFactoryManager.models.filter(function(model) {
return (model.tableName == (Task.tableName + User.tableName)) return (model.tableName == (Task.tableName + User.tableName))
}) })
...@@ -227,7 +227,7 @@ describe('HasMany', function() { ...@@ -227,7 +227,7 @@ describe('HasMany', function() {
Person.hasMany(Person, {as: 'CoWorkers'}) Person.hasMany(Person, {as: 'CoWorkers'})
Person.sync({force: true}).success(function() { Person.sync({force: true}).success(function() {
var modelNames = sequelize.modelManager.models.map(function(model) { return model.tableName }) var modelNames = sequelize.modelFactoryManager.models.map(function(model) { return model.tableName })
, expectation = ["Persons", "ChildrenPersons", "CoWorkersPersons", "FriendsPersons"] , expectation = ["Persons", "ChildrenPersons", "CoWorkersPersons", "FriendsPersons"]
expectation.forEach(function(ex) { expectation.forEach(function(ex) {
......
...@@ -10,7 +10,7 @@ Factories.prototype.Model = function(modelName, options, callback, count) { ...@@ -10,7 +10,7 @@ Factories.prototype.Model = function(modelName, options, callback, count) {
, models = [] , models = []
this.helpers.async(function(done) { this.helpers.async(function(done) {
var Model = self.sequelize.modelManager.getModel(modelName) var Model = self.sequelize.modelFactoryManager.getModel(modelName)
var create = function(cb) { var create = function(cb) {
Model.create(options).on('success', function(model) { Model.create(options).on('success', function(model) {
......
...@@ -19,7 +19,7 @@ describe('Associations', function() { ...@@ -19,7 +19,7 @@ describe('Associations', function() {
it("should create a table wp_table1wp_table2s", function() { it("should create a table wp_table1wp_table2s", function() {
Helpers.async(function(done) { Helpers.async(function(done) {
expect(sequelize.modelManager.getModel('wp_table1swp_table2s')).toBeDefined() expect(sequelize.modelFactoryManager.getModel('wp_table1swp_table2s')).toBeDefined()
done() done()
}) })
}) })
......
...@@ -36,9 +36,9 @@ describe('Sequelize', function() { ...@@ -36,9 +36,9 @@ describe('Sequelize', function() {
describe('define', function() { describe('define', function() {
it("adds a new model to the model manager", function() { it("adds a new model to the model manager", function() {
expect(sequelize.modelManager.all.length).toEqual(0) expect(sequelize.modelFactoryManager.all.length).toEqual(0)
sequelize.define('foo', { title: Sequelize.STRING }) sequelize.define('foo', { title: Sequelize.STRING })
expect(sequelize.modelManager.all.length).toEqual(1) expect(sequelize.modelFactoryManager.all.length).toEqual(1)
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!