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

Commit d3ee3bf3 by Daniel Durante

Merge pull request #761 from sequelize/features/reference_by_model

Allow reference by DaoFactory in foreign key definition
2 parents 73bb46b3 851015a9
var Toposort = require('toposort-class') var Toposort = require('toposort-class')
, DaoFactory = require('./dao-factory')
module.exports = (function() { module.exports = (function() {
var DAOFactoryManager = function(sequelize) { var DAOFactoryManager = function(sequelize) {
...@@ -39,16 +40,17 @@ module.exports = (function() { ...@@ -39,16 +40,17 @@ module.exports = (function() {
* before dependents. * before dependents.
*/ */
DAOFactoryManager.prototype.forEachDAO = function(iterator) { DAOFactoryManager.prototype.forEachDAO = function(iterator) {
var daos = {} var daos = {}
, sorter = new Toposort() , sorter = new Toposort()
this.daos.forEach(function(dao) { this.daos.forEach(function(dao) {
daos[dao.tableName] = dao
var deps = [] var deps = []
for(var attrName in dao.rawAttributes) { daos[dao.tableName] = dao
if(dao.rawAttributes.hasOwnProperty(attrName)) {
if(dao.rawAttributes[attrName].references) { for (var attrName in dao.rawAttributes) {
if (dao.rawAttributes.hasOwnProperty(attrName)) {
if (dao.rawAttributes[attrName].references) {
deps.push(dao.rawAttributes[attrName].references) deps.push(dao.rawAttributes[attrName].references)
} }
} }
......
...@@ -5,8 +5,6 @@ var Utils = require("./utils") ...@@ -5,8 +5,6 @@ var Utils = require("./utils")
module.exports = (function() { module.exports = (function() {
var DAOFactory = function(name, attributes, options) { var DAOFactory = function(name, attributes, options) {
var self = this
this.options = Utils._.extend({ this.options = Utils._.extend({
timestamps: true, timestamps: true,
instanceMethods: {}, instanceMethods: {},
...@@ -24,21 +22,28 @@ module.exports = (function() { ...@@ -24,21 +22,28 @@ module.exports = (function() {
// error check options // error check options
Utils._.each(options.validate, function(validator, validatorType) { Utils._.each(options.validate, function(validator, validatorType) {
if (Utils._.contains(Utils._.keys(attributes), validatorType)) if (Utils._.contains(Utils._.keys(attributes), validatorType)) {
throw new Error("A model validator function must not have the same name as a field. Model: " + name + ", field/validation name: " + validatorType) throw new Error("A model validator function must not have the same name as a field. Model: " + name + ", field/validation name: " + validatorType)
if (!Utils._.isFunction(validator)) }
if (!Utils._.isFunction(validator)) {
throw new Error("Members of the validate option must be functions. Model: " + name + ", error with validate member " + validatorType) throw new Error("Members of the validate option must be functions. Model: " + name + ", error with validate member " + validatorType)
}
}) })
this.name = name this.name = name
if (!this.options.tableName) { if (!this.options.tableName) {
this.tableName = this.options.freezeTableName ? name : Utils.pluralize(name, this.options.language) this.tableName = this.options.freezeTableName ? name : Utils.pluralize(name, this.options.language)
} else { } else {
this.tableName = this.options.tableName this.tableName = this.options.tableName
} }
this.rawAttributes = attributes
attributes = replaceReferencesWithTableNames(attributes)
this.rawAttributes = attributes
this.daoFactoryManager = null // defined in init function this.daoFactoryManager = null // defined in init function
this.associations = {} this.associations = {}
} }
Object.defineProperty(DAOFactory.prototype, 'attributes', { Object.defineProperty(DAOFactory.prototype, 'attributes', {
...@@ -59,8 +64,8 @@ module.exports = (function() { ...@@ -59,8 +64,8 @@ module.exports = (function() {
var self = this; var self = this;
this.daoFactoryManager = daoFactoryManager this.daoFactoryManager = daoFactoryManager
this.primaryKeys = {};
this.primaryKeys = {};
Utils._.each(this.attributes, function(dataTypeString, attributeName) { Utils._.each(this.attributes, function(dataTypeString, attributeName) {
if ((attributeName !== 'id') && (dataTypeString.indexOf('PRIMARY KEY') !== -1)) { if ((attributeName !== 'id') && (dataTypeString.indexOf('PRIMARY KEY') !== -1)) {
self.primaryKeys[attributeName] = dataTypeString self.primaryKeys[attributeName] = dataTypeString
...@@ -75,9 +80,11 @@ module.exports = (function() { ...@@ -75,9 +80,11 @@ module.exports = (function() {
findAutoIncrementField.call(this) findAutoIncrementField.call(this)
// DAO prototype // DAO prototype
// WTF ... ?
this.DAO = function() { this.DAO = function() {
DAO.apply(this, arguments); DAO.apply(this, arguments);
}; }
Util.inherits(this.DAO, DAO); Util.inherits(this.DAO, DAO);
this.DAO.prototype.rawAttributes = this.rawAttributes; this.DAO.prototype.rawAttributes = this.rawAttributes;
...@@ -89,47 +96,48 @@ module.exports = (function() { ...@@ -89,47 +96,48 @@ module.exports = (function() {
} }
Utils._.each(['Get', 'Set'], function(type) { Utils._.each(['Get', 'Set'], function(type) {
var prop = type.toLowerCase(), var prop = type.toLowerCase()
opt = prop + 'terMethods', , opt = prop + 'terMethods'
meth = '__define' + type + 'ter__', , meth = '__define' + type + 'ter__'
funcs = Utils._.isObject(self.options[opt]) ? self.options[opt] : {} , funcs = Utils._.isObject(self.options[opt]) ? self.options[opt] : {}
;
Utils._.each(self.rawAttributes, function(attr, name) { Utils._.each(self.rawAttributes, function(attr, name) {
if (attr.hasOwnProperty(prop)) if (attr.hasOwnProperty(prop)) {
funcs[name] = attr[prop] funcs[name] = attr[prop]
}); }
})
Utils._.each(funcs, function(fct, name) { Utils._.each(funcs, function(fct, name) {
if (!Utils._.isFunction(fct)) if (!Utils._.isFunction(fct)) {
throw new Error(type + 'ter for "' + name + '" is not a function.') throw new Error(type + 'ter for "' + name + '" is not a function.')
}
self.DAO.prototype[meth](name, fct); self.DAO.prototype[meth](name, fct)
}) })
}) })
this.DAO.prototype.attributes = Object.keys(this.DAO.prototype.rawAttributes); this.DAO.prototype.attributes = Object.keys(this.DAO.prototype.rawAttributes);
this.DAO.prototype.booleanValues = []
this.DAO.prototype.defaultValues = {}
this.DAO.prototype.validators = {}
this.DAO.prototype.booleanValues = [];
this.DAO.prototype.defaultValues = {};
this.DAO.prototype.validators = {};
Utils._.each(this.rawAttributes, function (definition, name) { Utils._.each(this.rawAttributes, function (definition, name) {
if (((definition === DataTypes.BOOLEAN) || (definition.type === DataTypes.BOOLEAN))) { if (((definition === DataTypes.BOOLEAN) || (definition.type === DataTypes.BOOLEAN))) {
self.DAO.prototype.booleanValues.push(name); self.DAO.prototype.booleanValues.push(name);
} }
if (definition.hasOwnProperty('defaultValue')) { if (definition.hasOwnProperty('defaultValue')) {
self.DAO.prototype.defaultValues[name] = function() { self.DAO.prototype.defaultValues[name] = function() {
return Utils.toDefaultValue(definition.defaultValue); return Utils.toDefaultValue(definition.defaultValue)
} }
} }
if (definition.hasOwnProperty('validate')) { if (definition.hasOwnProperty('validate')) {
self.DAO.prototype.validators[name] = definition.validate; self.DAO.prototype.validators[name] = definition.validate;
} }
}); })
this.DAO.prototype.__factory = this; this.DAO.prototype.__factory = this
this.DAO.prototype.hasDefaultValues = !Utils._.isEmpty(this.DAO.prototype.defaultValues); this.DAO.prototype.hasDefaultValues = !Utils._.isEmpty(this.DAO.prototype.defaultValues)
return this return this
} }
...@@ -520,6 +528,10 @@ module.exports = (function() { ...@@ -520,6 +528,10 @@ module.exports = (function() {
return this.QueryInterface.bulkUpdate(this.tableName, attrValueHash, where) return this.QueryInterface.bulkUpdate(this.tableName, attrValueHash, where)
} }
DAOFactory.prototype.describe = function() {
return this.QueryInterface.describeTable(this.tableName)
}
// private // private
var query = function() { var query = function() {
...@@ -627,6 +639,16 @@ module.exports = (function() { ...@@ -627,6 +639,16 @@ module.exports = (function() {
} }
} }
var replaceReferencesWithTableNames = function(attributes) {
Object.keys(attributes).forEach(function(attrName) {
if (attributes[attrName].references instanceof DAOFactory) {
attributes[attrName].references = attributes[attrName].references.tableName
}
})
return attributes
}
Utils._.extend(DAOFactory.prototype, require("./associations/mixin")) Utils._.extend(DAOFactory.prototype, require("./associations/mixin"))
return DAOFactory return DAOFactory
......
...@@ -221,7 +221,7 @@ module.exports = (function() { ...@@ -221,7 +221,7 @@ module.exports = (function() {
if (options.offset && !options.limit) { if (options.offset && !options.limit) {
/* /*
* If no limit is defined, our best bet is to use the max number of rows in a table. From the MySQL docs: * If no limit is defined, our best bet is to use the max number of rows in a table. From the MySQL docs:
* There is a limit of 2^32 (~4.295E+09) rows in a MyISAM table. If you build MySQL with the --with-big-tables option, * There is a limit of 2^32 (~4.295E+09) rows in a MyISAM table. If you build MySQL with the --with-big-tables option,
* the row limitation is increased to (2^32)^2 (1.844E+19) rows. * the row limitation is increased to (2^32)^2 (1.844E+19) rows.
*/ */
query += " LIMIT " + options.offset + ", " + 18440000000000000000; query += " LIMIT " + options.offset + ", " + 18440000000000000000;
...@@ -536,7 +536,6 @@ module.exports = (function() { ...@@ -536,7 +536,6 @@ module.exports = (function() {
if(dataType.references) { if(dataType.references) {
template += " REFERENCES " + this.quoteIdentifier(dataType.references) template += " REFERENCES " + this.quoteIdentifier(dataType.references)
if(dataType.referencesKey) { if(dataType.referencesKey) {
template += " (" + this.quoteIdentifier(dataType.referencesKey) + ")" template += " (" + this.quoteIdentifier(dataType.referencesKey) + ")"
} else { } else {
......
...@@ -183,11 +183,12 @@ module.exports = (function() { ...@@ -183,11 +183,12 @@ module.exports = (function() {
// if you call "define" multiple times for the same daoName, do not clutter the factory // if you call "define" multiple times for the same daoName, do not clutter the factory
if(this.isDefined(daoName)) { if(this.isDefined(daoName)) {
this.daoFactoryManager.removeDAO(this.daoFactoryManager.getDAO(daoName)) this.daoFactoryManager.removeDAO(this.daoFactoryManager.getDAO(daoName))
} }
var factory = new DAOFactory(daoName, attributes, options) var factory = new DAOFactory(daoName, attributes, options)
this.daoFactoryManager.addDAO(factory.init(this.daoFactoryManager)) this.daoFactoryManager.addDAO(factory.init(this.daoFactoryManager))
return factory return factory
} }
......
...@@ -2039,4 +2039,124 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -2039,4 +2039,124 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.bind(this)) }.bind(this))
}) })
}) })
describe('references', function() {
before(function() {
this.Author = this.sequelize.define('author', { firstName: Sequelize.STRING })
})
describe("use of existing dao factory", function() {
before(function() {
this.Post = this.sequelize.define('post', {
title: Sequelize.STRING,
authorId: {
type: Sequelize.INTEGER,
references: this.Author,
referencesKey: "id"
}
})
this.Author.hasMany(this.Post)
this.Post.belongsTo(this.Author)
})
it('references the author table', function(done) {
this.Author.sync({ force: true }).success(function() {
this.Post.sync({ force: true }).on('sql', function(sql) {
if (dialect === 'postgres') {
expect(sql).toMatch(/"authorId" INTEGER REFERENCES "authors" \("id"\)/)
} else if (dialect === 'mysql') {
expect(sql).toMatch(/FOREIGN KEY \(`authorId`\) REFERENCES `authors` \(`id`\)/)
} else if (dialect === 'sqlite') {
expect(sql).toMatch(/`authorId` INTEGER REFERENCES `authors` \(`id`\)/)
} else {
throw new Error('Undefined dialect!')
}
done()
})
}.bind(this))
})
})
describe('use of table name as string', function() {
before(function() {
this.Post = this.sequelize.define('post', {
title: Sequelize.STRING,
authorId: {
type: Sequelize.INTEGER,
references: 'authors',
referencesKey: "id"
}
})
this.Author.hasMany(this.Post)
this.Post.belongsTo(this.Author)
})
it('references the author table', function(done) {
this.Author.sync({ force: true }).success(function() {
this.Post.sync({ force: true }).on('sql', function(sql) {
if (dialect === 'postgres') {
expect(sql).toMatch(/"authorId" INTEGER REFERENCES "authors" \("id"\)/)
} else if (dialect === 'mysql') {
expect(sql).toMatch(/FOREIGN KEY \(`authorId`\) REFERENCES `authors` \(`id`\)/)
} else if (dialect === 'sqlite') {
expect(sql).toMatch(/`authorId` INTEGER REFERENCES `authors` \(`id`\)/)
} else {
throw new Error('Undefined dialect!')
}
done()
})
}.bind(this))
})
})
describe('use of invalid table name', function() {
before(function() {
this.Post = this.sequelize.define('post', {
title: Sequelize.STRING,
authorId: {
type: Sequelize.INTEGER,
references: '4uth0r5',
referencesKey: "id"
}
})
this.Author.hasMany(this.Post)
this.Post.belongsTo(this.Author)
})
it("emits the error event as the referenced table name is invalid", function(done) {
this.Author.sync({ force: true }).success(function() {
this.Post
.sync({ force: true })
.success(function() {
if (dialect === 'sqlite') {
// sorry ... but sqlite is too stupid to understand whats going on ...
expect(1).toEqual(1)
done()
} else {
// the parser should not end up here ...
expect(2).toEqual(1)
}
}).error(function(err) {
if (dialect === 'mysql') {
expect(err.message).toMatch(/ER_CANT_CREATE_TABLE/)
} else if (dialect === 'sqlite') {
// the parser should not end up here ... see above
expect(1).toEqual(2)
} else if (dialect === 'postgres') {
expect(err.message).toMatch(/relation "4uth0r5" does not exist/)
} else {
throw new Error('Undefined dialect!')
}
done()
})
}.bind(this))
})
})
}) //- describe: references
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!