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

Commit 75a695b7 by Sascha Depold

Merge branch 'eagerloadedAttributes' of https://github.com/janmeier/sequelize in…

…to janmeier-eagerloadedAttributes
2 parents 295d0c25 43c6e948
...@@ -169,7 +169,7 @@ module.exports = (function() { ...@@ -169,7 +169,7 @@ module.exports = (function() {
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
} }
...@@ -334,7 +334,7 @@ module.exports = (function() { ...@@ -334,7 +334,7 @@ module.exports = (function() {
options = {where: parseInt(Number(options) || 0, 0)} options = {where: parseInt(Number(options) || 0, 0)}
} }
} }
options.limit = 1 options.limit = 1
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
...@@ -646,6 +646,18 @@ module.exports = (function() { ...@@ -646,6 +646,18 @@ module.exports = (function() {
delete include.model delete include.model
} }
if (include.hasOwnProperty('attributes')) {
var primaryKeys;
if (include.daoFactory.hasPrimaryKeys) {
primaryKeys = include.daoFactory.primaryKeys
} else {
primaryKeys = ['id']
}
include.attributes = include.attributes.concat(primaryKeys)
} else {
include.attributes = Object.keys(include.daoFactory.attributes)
}
if (include.hasOwnProperty('daoFactory') && (include.hasOwnProperty('as'))) { if (include.hasOwnProperty('daoFactory') && (include.hasOwnProperty('as'))) {
var usesAlias = (include.as !== include.daoFactory.tableName) var usesAlias = (include.as !== include.daoFactory.tableName)
, association = (usesAlias ? this.getAssociationByAlias(include.as) : this.getAssociation(include.daoFactory)) , association = (usesAlias ? this.getAssociationByAlias(include.as) : this.getAssociation(include.daoFactory))
......
...@@ -182,7 +182,7 @@ module.exports = (function() { ...@@ -182,7 +182,7 @@ module.exports = (function() {
var optAttributes = options.attributes === '*' ? [options.table + '.*'] : [options.attributes] var optAttributes = options.attributes === '*' ? [options.table + '.*'] : [options.attributes]
options.include.forEach(function(include) { options.include.forEach(function(include) {
var attributes = Object.keys(include.daoFactory.attributes).map(function(attr) { var attributes = include.attributes.map(function(attr) {
return this.quoteIdentifier(include.as) + "." + this.quoteIdentifier(attr) + " AS " + this.quoteIdentifier(include.as + "." + attr) return this.quoteIdentifier(include.as) + "." + this.quoteIdentifier(attr) + " AS " + this.quoteIdentifier(include.as + "." + attr)
}.bind(this)) }.bind(this))
......
...@@ -253,7 +253,7 @@ module.exports = (function() { ...@@ -253,7 +253,7 @@ module.exports = (function() {
var optAttributes = options.attributes === '*' ? [options.table + '.*'] : [options.attributes] var optAttributes = options.attributes === '*' ? [options.table + '.*'] : [options.attributes]
options.include.forEach(function(include) { options.include.forEach(function(include) {
var attributes = Object.keys(include.daoFactory.attributes).map(function(attr) { var attributes = include.attributes.map(function(attr) {
return this.quoteIdentifier(include.as) + "." + this.quoteIdentifier(attr) + " AS " + this.quoteIdentifier(include.as + "." + attr, true) return this.quoteIdentifier(include.as) + "." + this.quoteIdentifier(attr) + " AS " + this.quoteIdentifier(include.as + "." + attr, true)
}.bind(this)) }.bind(this))
......
...@@ -164,7 +164,7 @@ module.exports = (function() { ...@@ -164,7 +164,7 @@ module.exports = (function() {
var optAttributes = options.attributes === '*' ? [options.table + '.*'] : [options.attributes] var optAttributes = options.attributes === '*' ? [options.table + '.*'] : [options.attributes]
options.include.forEach(function(include) { options.include.forEach(function(include) {
var attributes = Object.keys(include.daoFactory.attributes).map(function(attr) { var attributes = include.attributes.map(function(attr) {
return this.quoteIdentifier(include.as) + "." + this.quoteIdentifier(attr) + " AS " + this.quoteIdentifier(include.as + "." + attr) return this.quoteIdentifier(include.as) + "." + this.quoteIdentifier(attr) + " AS " + this.quoteIdentifier(include.as + "." + attr)
}.bind(this)) }.bind(this))
......
...@@ -1471,6 +1471,36 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1471,6 +1471,36 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('returns the selected fields for both the base table and the included table as instance.selectedValues', function(done) {
var self = this
self.Mission = self.sequelize.define('Mission', {
title: {type: Sequelize.STRING, defaultValue: 'another mission!!'},
foo: {type: Sequelize.INTEGER, defaultValue: 4},
})
self.Mission.belongsTo(self.User)
self.User.hasMany(self.Mission)
self.Mission.sync({ force: true }).success(function() {
self.Mission.create().success(function(mission) {
self.User.create({username: 'Brain Picker'}).success(function(user) {
mission.setUser(user).success(function() {
self.User.find({
where: { username: 'Brain Picker' },
attributes: ['username'],
include: [{model: self.Mission, as: self.Mission.tableName, attributes: ['title']}]
}).success(function(user) {
expect(user.selectedValues).to.deep.equal({ username: 'Brain Picker' })
expect(user.missions[0].selectedValues).to.deep.equal({ id: 1, title: 'another mission!!'})
expect(user.missions[0].foo).not.to.exist
done()
})
})
})
})
})
})
it('always honors ZERO as primary key', function(_done) { it('always honors ZERO as primary key', function(_done) {
var self = this var self = this
, permutations = [ , permutations = [
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!