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

Commit a1adbadf by Sascha Depold

Merge pull request #868 from jwilm/model-accessor

Add Sequelize#model to fetch DAOFactory of previously defined model
2 parents 96874904 1937368f
Showing with 31 additions and 0 deletions
......@@ -195,6 +195,20 @@ module.exports = (function() {
return factory
}
/**
Fetch a DAO factory
@param {String} daoName The name of a model defined with Sequelize.define
@returns {DAOFactory} The DAOFactory for daoName
*/
Sequelize.prototype.model = function(daoName) {
if(!this.isDefined(daoName)) {
throw new Error(daoName + ' has not been defined')
}
return this.daoFactoryManager.getDAO(daoName)
}
Sequelize.prototype.isDefined = function(daoName) {
var daos = this.daoFactoryManager.daos
return (daos.filter(function(dao) { return dao.name === daoName }).length !== 0)
......
......@@ -51,6 +51,23 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
})
})
describe('model', function() {
it('throws an error if the dao being accessed is undefined', function() {
var self = this
expect(function() {
self.sequelize.model('Project')
}).to.throw(/project has not been defined/i)
})
it('returns the dao factory defined by daoName', function() {
var project = this.sequelize.define('Project', {
name: DataTypes.STRING
})
expect(this.sequelize.model('Project')).to.equal(project)
})
})
describe('query', function() {
afterEach(function(done) {
this.sequelize.options.quoteIdentifiers = true
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!