sails-disk
Advanced tools
Comparing version 0.9.0 to 0.9.1
161
index.js
@@ -72,3 +72,2 @@ /*--------------------------------------------------------------- | ||
find: function (collectionName, options, cb) { | ||
// read(); | ||
@@ -83,3 +82,163 @@ | ||
}); | ||
// If we're grouping | ||
if(options.groupBy || options.sum || options.average || options.min || options.max) { | ||
// Check if we have calculations to do | ||
if(!options.sum && !options.average && !options.min && !options.max) { | ||
return cb(new Error('Cannot groupBy without a calculation')); | ||
} | ||
// First we groupBy | ||
// grouped results is our current resultSet, split up by group | ||
var groupedResults = []; | ||
// finished results is our generated results (with sums, evgs, etc) | ||
var finishedResults = []; | ||
if(options.groupBy) { | ||
var groups = []; | ||
var groupCollector = {}; | ||
// Go through the results | ||
resultSet.forEach(function(item){ | ||
var key = ''; | ||
options.groupBy.forEach(function(groupKey){ | ||
key += item[groupKey] + '---'; | ||
}); | ||
if(groupCollector[key]) { | ||
groupCollector[key].push(item); | ||
} else { | ||
groupCollector[key] = [item]; | ||
} | ||
}); | ||
for(var key in groupCollector) { | ||
groups.push(groupCollector[key]); | ||
} | ||
groupedResults = groups; | ||
// Then we generate stub objects for adding/averaging | ||
groups.forEach(function(group){ | ||
var stubResult = {}; | ||
// Groupresult will look like this: { type: 'count', a2: 'test' } | ||
options.groupBy.forEach(function(groupKey) { | ||
// Set the grouped by value to the value of the first results | ||
stubResult[groupKey] = group[0][groupKey]; | ||
}); | ||
finishedResults.push(stubResult); | ||
}); | ||
} else { | ||
groupedResults = [resultSet]; | ||
finishedResults = [{}]; | ||
} | ||
// sum all the things (specified) | ||
if(options.sum) { | ||
// fill in our stub object with those keys, set to sum 0 | ||
options.sum.forEach(function(sumKey) { | ||
finishedResults.forEach(function(stub) { | ||
stub[sumKey] = 0; | ||
}); | ||
}); | ||
// iterate over all groups of data | ||
groupedResults.forEach(function(group, i) { | ||
// sum for each item | ||
group.forEach(function(item) { | ||
options.sum.forEach(function(sumKey) { | ||
if(typeof item[sumKey] === 'number') { | ||
finishedResults[i][sumKey]+=item[sumKey]; | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
if(options.average) { | ||
// fill in our stub object with those keys, set to sum 0 | ||
options.average.forEach(function(sumKey) { | ||
finishedResults.forEach(function(stub) { | ||
stub[sumKey] = 0; | ||
}); | ||
}); | ||
// iterate over all groups of data | ||
groupedResults.forEach(function(group, i) { | ||
options.average.forEach(function(sumKey) { | ||
// count up how many numbers we have, so we know how much to divide by | ||
var cnt = 0; | ||
// average for each item | ||
group.forEach(function(item) { | ||
if(typeof item[sumKey] === 'number') { | ||
finishedResults[i][sumKey]+=item[sumKey]; | ||
cnt+=1; | ||
} | ||
}); | ||
finishedResults[i][sumKey]/=cnt; | ||
}); | ||
}); | ||
} | ||
if(options.min) { | ||
// iterate over all groups of data | ||
groupedResults.forEach(function(group, i) { | ||
options.min.forEach(function(sumKey) { | ||
// keep track of current minimum | ||
var min = Infinity; | ||
// update min | ||
group.forEach(function(item) { | ||
if(typeof item[sumKey] === 'number') { | ||
if(item[sumKey] < min) { | ||
min = item[sumKey]; | ||
} | ||
} | ||
}); | ||
finishedResults[i][sumKey] = isFinite(min) ? min : null; | ||
}); | ||
}); | ||
} | ||
if(options.max) { | ||
// iterate over all groups of data | ||
groupedResults.forEach(function(group, i) { | ||
options.max.forEach(function(sumKey) { | ||
// keep track of current maximum | ||
var max = -Infinity; | ||
// update max | ||
group.forEach(function(item) { | ||
if(typeof item[sumKey] === 'number') { | ||
if(item[sumKey] > max) { | ||
max = item[sumKey]; | ||
} | ||
} | ||
}); | ||
finishedResults[i][sumKey] = isFinite(max) ? max : null; | ||
}); | ||
}); | ||
} | ||
resultSet = finishedResults; | ||
} | ||
cb(null, resultSet); | ||
@@ -86,0 +245,0 @@ }, |
{ | ||
"name": "sails-disk", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "Persistent non-production adapter for Sails.js / Waterline", | ||
@@ -31,4 +31,4 @@ "main": "index.js", | ||
"mocha": "~1.10.0", | ||
"waterline-adapter-tests": "0.9.0" | ||
"waterline-adapter-tests": "~0.9.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12415
282