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

Commit cfd212c0 by Sascha Depold

correctly group data

1 parent 0c39fcc8
Showing with 73 additions and 15 deletions
......@@ -53,11 +53,6 @@ module.exports = (function() {
AbstractQuery.prototype.formatResults = function(data) {
var result = this.callee
/*if(queryResultHasJoin.call(this, data)) {
console.log(data)
console.log(this.sql)
}*/
if (isInsertQuery.call(this, data)) {
handleInsertQuery.call(this, data)
}
......@@ -150,7 +145,7 @@ module.exports = (function() {
if (this.options.raw) {
result = results
} else if (queryResultHasJoin(results)) {
result = results.map(function(result) {
result = groupDataByCalleeFactory.call(this, results).map(function(result) {
// let's build the actual dao instance first...
var dao = this.callee.build(result[this.callee.tableName], { isNewRecord: false })
......@@ -177,27 +172,90 @@ module.exports = (function() {
return result
}
var buildAssociatedDaoInstances = function(tableName, associatedTableName, dao) {
var buildAssociatedDaoInstances = function(tableName, associationData, dao) {
var associatedDao = this.sequelize.daoFactoryManager.getDAO(tableName, { attribute: 'tableName' })
, association = this.callee.getAssociation(associatedDao)
, accessor = Utils._.camelize(associatedDao.tableName)
, daoInstance = associatedDao.build(associatedTableName, { isNewRecord: false })
// downcase the first char
accessor = accessor.slice(0,1).toLowerCase() + accessor.slice(1)
if (['BelongsTo', 'HasOne'].indexOf(association.associationType) > -1) {
accessor = Utils.singularize(accessor)
dao[accessor] = daoInstance
} else {
dao[accessor] = dao[accessor] || []
dao[accessor].push(daoInstance)
}
associationData.forEach(function(data) {
var daoInstance = associatedDao.build(data, { isNewRecord: false })
if (['BelongsTo', 'HasOne'].indexOf(association.associationType) > -1) {
accessor = Utils.singularize(accessor)
dao[accessor] = daoInstance
} else {
dao[accessor] = dao[accessor] || []
dao[accessor].push(daoInstance)
}
})
}
var isShowOrDescribeQuery = function() {
return (this.sql.indexOf('SHOW') === 0) || (this.sql.indexOf('DESCRIBE') === 0)
}
/**
The function takes the result of the query execution and groups
the associated data by the callee.
Example:
groupDataByCalleeFactory([
{
callee: { some: 'data', id: 1 },
association: { foo: 'bar', id: 1 }
}, {
callee: { some: 'data', id: 1 },
association: { foo: 'bar', id: 2 }
}, {
callee: { some: 'data', id: 1 },
association: { foo: 'bar', id: 3 }
}
])
Result:
Something like this:
[
{
callee: { some: 'data', id: 1 },
association: [
{ foo: 'bar', id: 1 },
{ foo: 'bar', id: 2 },
{ foo: 'bar', id: 3 }
]
}
]
*/
var groupDataByCalleeFactory = function(data) {
var result = []
, calleeTableName = this.callee.tableName
data.forEach(function(row) {
var calleeData = row[calleeTableName]
, existingEntry = result.filter(function(groupedRow) {
return Utils._.isEqual(groupedRow[calleeTableName], calleeData)
})[0]
if (!existingEntry) {
existingEntry = {}
result.push(existingEntry)
existingEntry[calleeTableName] = calleeData
}
for (var attrName in row) {
if (row.hasOwnProperty(attrName) && (attrName !== calleeTableName)) {
existingEntry[attrName] = existingEntry[attrName] || []
existingEntry[attrName].push(row[attrName])
}
}
})
return result
}
return AbstractQuery
})()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!