Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sequelize#model to fetch DAOFactory of previously defined model #868

Merged
merged 1 commit into from Sep 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/sequelize.js
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/sequelize.test.js
Expand Up @@ -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
Expand Down