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

Commit e772a0cc by durango

Merge pull request #613 from innofluence/findQueryOptions

Make it possible to specify query options for find and findAll (specifically raw: true)
2 parents 3935784d 023e7516
......@@ -19,5 +19,4 @@ env:
language: node_js
node_js:
- 0.8
\ No newline at end of file
- 0.8
\ No newline at end of file
......@@ -9,6 +9,7 @@
- [FEATURE] Schematics. [#564](https://github.com/sequelize/sequelize/pull/564). thanks to durango
- [FEATURE] Foreign key constraints. [#595](https://github.com/sequelize/sequelize/pull/595). thanks to optilude
- [FEATURE] Support for bulk insert (`<DAOFactory>.bulkCreate()`, update (`<DAOFactory>.update()`) and delete (`<DAOFactory>.destroy()`) [#569](https://github.com/sequelize/sequelize/pull/569). thanks to optilude
- [FEATURE] Add an extra `queryOptions` parameter to `DAOFactory.find` and `DAOFactory.findAll`. This allows a user to specify `{ raw: true }`, meaning that the raw result should be returned, instead of built DAOs. Usefull for queries returning large datasets, see [#611](https://github.com/sequelize/sequelize/pull/611) janmeier
# v1.6.0 #
- [DEPENDENCIES] upgrade mysql to alpha7. You *MUST* use this version or newer for DATETIMEs to work
......
......@@ -156,11 +156,11 @@ module.exports = (function() {
}
// alias for findAll
DAOFactory.prototype.all = function(options) {
return this.findAll(options)
DAOFactory.prototype.all = function(options, queryOptions) {
return this.findAll(options, queryOptions)
}
DAOFactory.prototype.findAll = function(options) {
DAOFactory.prototype.findAll = function(options, queryOptions) {
var hasJoin = false
var options = Utils._.clone(options)
......@@ -177,10 +177,10 @@ module.exports = (function() {
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',
hasJoin: hasJoin
})
}, queryOptions))
}
//right now, the caller (has-many-double-linked) is in charge of the where clause
......@@ -199,9 +199,10 @@ module.exports = (function() {
*
* @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 {Object} set the query options, e.g. raw, specifying that you want raw data instead of built DAOs
* @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
// no options defined?
......@@ -253,11 +254,11 @@ module.exports = (function() {
options.limit = 1
return this.QueryInterface.select(this, this.getTableName(), options, {
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
plain: true,
type: 'SELECT',
hasJoin: hasJoin
})
}, queryOptions))
}
DAOFactory.prototype.count = function(options) {
......
......@@ -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('findAll', function findAll() {
......@@ -1292,6 +1328,49 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.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
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!