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

Commit ae1d4f6b by Jan Aagaard Meier

Allow find and findall to take query options

1 parent 3935784d
Showing with 88 additions and 8 deletions
...@@ -156,11 +156,11 @@ module.exports = (function() { ...@@ -156,11 +156,11 @@ module.exports = (function() {
} }
// alias for findAll // alias for findAll
DAOFactory.prototype.all = function(options) { DAOFactory.prototype.all = function(options, queryOptions) {
return this.findAll(options) return this.findAll(options, queryOptions)
} }
DAOFactory.prototype.findAll = function(options) { DAOFactory.prototype.findAll = function(options, queryOptions) {
var hasJoin = false var hasJoin = false
var options = Utils._.clone(options) var options = Utils._.clone(options)
...@@ -177,10 +177,10 @@ module.exports = (function() { ...@@ -177,10 +177,10 @@ module.exports = (function() {
this.options.whereCollection = options.where || null this.options.whereCollection = options.where || null
} }
return this.QueryInterface.select(this, this.tableName, options, { return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({
type: 'SELECT', type: 'SELECT',
hasJoin: hasJoin hasJoin: hasJoin
}) }, queryOptions))
} }
//right now, the caller (has-many-double-linked) is in charge of the where clause //right now, the caller (has-many-double-linked) is in charge of the where clause
...@@ -199,9 +199,10 @@ module.exports = (function() { ...@@ -199,9 +199,10 @@ module.exports = (function() {
* *
* @param {Object} options Options to describe the scope of the search. * @param {Object} options Options to describe the scope of the search.
* @param {Array} include A list of associations which shall get eagerly loaded. Supported is either { include: [ DaoFactory1, DaoFactory2, ...] } or { include: [ { daoFactory: DaoFactory1, as: 'Alias' } ] }. * @param {Array} include A list of associations which shall get eagerly loaded. Supported is either { include: [ DaoFactory1, DaoFactory2, ...] } or { include: [ { daoFactory: DaoFactory1, as: 'Alias' } ] }.
* @param {Object} set the query options, e.g. raw, specifying that you want raw data instead of built data
* @return {Object} A promise which fires `success`, `error`, `complete` and `sql`. * @return {Object} A promise which fires `success`, `error`, `complete` and `sql`.
*/ */
DAOFactory.prototype.find = function(options) { DAOFactory.prototype.find = function(options, queryOptions) {
var hasJoin = false var hasJoin = false
// no options defined? // no options defined?
...@@ -253,11 +254,11 @@ module.exports = (function() { ...@@ -253,11 +254,11 @@ module.exports = (function() {
options.limit = 1 options.limit = 1
return this.QueryInterface.select(this, this.getTableName(), options, { return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
plain: true, plain: true,
type: 'SELECT', type: 'SELECT',
hasJoin: hasJoin hasJoin: hasJoin
}) }, queryOptions))
} }
DAOFactory.prototype.count = function(options) { DAOFactory.prototype.count = function(options) {
......
...@@ -1060,6 +1060,42 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1060,6 +1060,42 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}) })
}) })
}) })
describe('queryOptions', function() {
before(function(done) {
this.User.create({
username: 'barfooz'
}).success(function(user) {
this.user = user
done()
}.bind(this))
})
it("should return a DAO when queryOptions are not set", function (done) {
this.User.find({ where: { username: 'barfooz'}}).done(function (err, user) {
expect(user).toHavePrototype(this.User.DAO.prototype)
done();
}.bind(this))
})
it("should return a DAO when raw is false", function (done) {
this.User.find({ where: { username: 'barfooz'}}, { raw: false }).done(function (err, user) {
expect(user).toHavePrototype(this.User.DAO.prototype)
done();
}.bind(this))
})
it("should return raw data when raw is true", function (done) {
this.User.find({ where: { username: 'barfooz'}}, { raw: true }).done(function (err, user) {
expect(user).not.toHavePrototype(this.User.DAO.prototype)
expect(user).toBeObject()
done();
}.bind(this))
})
}) // - describe: queryOptions
}) //- describe: find }) //- describe: find
describe('findAll', function findAll() { describe('findAll', function findAll() {
...@@ -1292,6 +1328,49 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1292,6 +1328,49 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.bind(this)) }.bind(this))
}) })
}) })
describe('queryOptions', function() {
before(function(done) {
this.User.create({
username: 'barfooz'
}).success(function(user) {
this.user = user
done()
}.bind(this))
})
it("should return a DAO when queryOptions are not set", function (done) {
this.User.findAll({ where: { username: 'barfooz'}}).done(function (err, users) {
users.forEach(function (user) {
expect(user).toHavePrototype(this.User.DAO.prototype)
}, this)
done();
}.bind(this))
})
it("should return a DAO when raw is false", function (done) {
this.User.findAll({ where: { username: 'barfooz'}}, { raw: false }).done(function (err, users) {
users.forEach(function (user) {
expect(user).toHavePrototype(this.User.DAO.prototype)
}, this)
done();
}.bind(this))
})
it("should return raw data when raw is true", function (done) {
this.User.findAll({ where: { username: 'barfooz'}}, { raw: true }).done(function (err, users) {
users.forEach(function (user) {
expect(user).not.toHavePrototype(this.User.DAO.prototype)
expect(users[0]).toBeObject()
}, this)
done();
}.bind(this))
})
}) // - describe: queryOptions
}) })
}) //- describe: findAll }) //- describe: findAll
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!