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

Commit 4c7e4674 by Mick Hansen

Make groupJoinData able to handle nested belongsTo (naive implementation, will b…

…reak on multiple objects for sure)
1 parent 60a118f9
...@@ -376,9 +376,7 @@ module.exports = (function() { ...@@ -376,9 +376,7 @@ module.exports = (function() {
if (options.hasOwnProperty('include')) { if (options.hasOwnProperty('include')) {
hasJoin = true hasJoin = true
options.include = options.include.map(function(include) { validateIncludedElements.call(this, options)
return validateIncludedElement.call(this, include)
}.bind(this))
} }
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
...@@ -461,9 +459,7 @@ module.exports = (function() { ...@@ -461,9 +459,7 @@ module.exports = (function() {
if (options.hasOwnProperty('include')) { if (options.hasOwnProperty('include')) {
hasJoin = true hasJoin = true
options.include = options.include.map(function(include) { validateIncludedElements.call(this, options)
return validateIncludedElement.call(this, include)
}.bind(this))
} }
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
...@@ -1194,6 +1190,18 @@ module.exports = (function() { ...@@ -1194,6 +1190,18 @@ module.exports = (function() {
}.bind(this)) }.bind(this))
} }
var validateIncludedElements = function(options) {
options.includeNames = []
options.includeMap = {}
options.include = options.include.map(function(include) {
include = validateIncludedElement.call(this, include, options.daoFactory)
options.includeMap[include.as] = include
options.includeNames.push(include.as)
return include
}.bind(this))
};
var validateIncludedElement = function(include, parent) { var validateIncludedElement = function(include, parent) {
if (include instanceof DAOFactory) { if (include instanceof DAOFactory) {
include = { daoFactory: include, as: include.tableName } include = { daoFactory: include, as: include.tableName }
...@@ -1242,9 +1250,10 @@ module.exports = (function() { ...@@ -1242,9 +1250,10 @@ module.exports = (function() {
include.association = association include.association = association
if (include.hasOwnProperty('include')) { if (include.hasOwnProperty('include')) {
include.include = include.include.map(function(childInclude) { /*include.include = include.include.map(function(childInclude) {
return validateIncludedElement.call(this, childInclude, include.daoFactory) return validateIncludedElement.call(this, childInclude, include.daoFactory)
}.bind(this)) }.bind(this))*/
validateIncludedElements(include)
} }
return include return include
......
...@@ -219,16 +219,15 @@ module.exports = (function() { ...@@ -219,16 +219,15 @@ module.exports = (function() {
// Queries with include // Queries with include
} else if (this.options.hasJoin === true) { } else if (this.options.hasJoin === true) {
this.options.includeNames = this.options.include.map(function (include) { results = groupJoinData(results, this.options)
return include.as //console.log(JSON.stringify(results, null, '\t'));
})
results = groupJoinData.call(this, results)
result = results.map(function(result) { result = results.map(function(result) {
return this.callee.build(result, { return this.callee.build(result, {
isNewRecord: false, isNewRecord: false,
isDirty: false, isDirty: false,
include:this.options.include, include:this.options.include,
includeNames: this.options.includeNames, includeNames: this.options.includeNames,
includeMap: this.options.includeMap,
includeValidated: true includeValidated: true
}) })
}.bind(this)) }.bind(this))
...@@ -318,18 +317,18 @@ module.exports = (function() { ...@@ -318,18 +317,18 @@ module.exports = (function() {
] ]
*/ */
var groupJoinData = function(data) { var groupJoinData = function(data, options) {
var self = this var results = []
, results = []
, existingResult , existingResult
, calleeData , calleeData
, child
data.forEach(function (row) { data.forEach(function (row) {
row = Dot.transform(row) row = Dot.transform(row)
calleeData = _.omit(row, self.options.includeNames) calleeData = _.omit(row, options.includeNames)
existingResult = _.find(results, function (result) { existingResult = _.find(results, function (result) {
return Utils._.isEqual(_.omit(result, self.options.includeNames), calleeData) return Utils._.isEqual(_.omit(result, options.includeNames), calleeData)
}) })
if (!existingResult) { if (!existingResult) {
...@@ -337,13 +336,20 @@ module.exports = (function() { ...@@ -337,13 +336,20 @@ module.exports = (function() {
} }
for (var attrName in row) { for (var attrName in row) {
if (row.hasOwnProperty(attrName) && Object(row[attrName]) === row[attrName] && self.options.includeNames.indexOf(attrName) !== -1) { if (row.hasOwnProperty(attrName)) {
existingResult[attrName] = existingResult[attrName] || [] child = Object(row[attrName]) === row[attrName] && options.includeMap[attrName]
var attrRowExists = existingResult[attrName].some(function(attrRow) { if (child) {
return Utils._.isEqual(attrRow, row[attrName]) existingResult[attrName] = existingResult[attrName] || []
}) var attrRowExists = existingResult[attrName].some(function(attrRow) {
if (!attrRowExists) { return Utils._.isEqual(attrRow, row[attrName])
existingResult[attrName].push(row[attrName]) })
if (!attrRowExists) {
if (child.include) {
existingResult[attrName].push(groupJoinData([row[attrName]], child)[0])
} else {
existingResult[attrName].push(row[attrName])
}
}
} }
} }
} }
......
...@@ -659,7 +659,7 @@ module.exports = (function() { ...@@ -659,7 +659,7 @@ module.exports = (function() {
} }
var sql = this.QueryGenerator.selectQuery(tableName, options, factory) var sql = this.QueryGenerator.selectQuery(tableName, options, factory)
queryOptions = Utils._.extend({}, queryOptions, { include: options.include }) queryOptions = Utils._.extend({}, queryOptions, { include: options.include, includeNames: options.includeNames, includeMap: options.includeMap })
return queryAndEmit.call(this, [sql, factory, queryOptions], 'select') return queryAndEmit.call(this, [sql, factory, queryOptions], 'select')
} }
......
...@@ -25,7 +25,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -25,7 +25,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
Task.belongsTo(User) Task.belongsTo(User)
User.belongsTo(Group) User.belongsTo(Group)
this.sequelize.sync().then(function () { this.sequelize.sync({force: true}).done(function () {
async.auto({ async.auto({
task: function (callback) { task: function (callback) {
Task.create().done(callback) Task.create().done(callback)
...@@ -53,8 +53,12 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -53,8 +53,12 @@ describe(Support.getTestDialectTeaser("Include"), function () {
]} ]}
] ]
}).done(function (err, task) { }).done(function (err, task) {
console.log(err && err.sql)
expect(err).not.to.be.ok
expect(task.user).to.be.ok expect(task.user).to.be.ok
expect(task.user.group).to.be.ok expect(task.user.group).to.be.ok
}).on('sql', function (sql) {
console.log(sql)
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!