entities.js
* @module greppy/helper/db/sql/entities
* @author Hermann Mayer <hermann.mayer92@gmail.com>
var EntitiesHelper = function()
* Fetch all models of the given models in parallel.
* @param {Array} models - Entities to fetch
* @param {Object} [criteria] - Criteria to use for filtering
* @param {Function} callback - Function to call on finish
EntitiesHelper.prototype.fetchAll = function(models, criteria, callback)
if (!models || 0 === models.length) {
throw new Error('No models given.');
if ('function' === typeof criteria) {
async.map(models, function(model, callback) {
model.findAll(criteria).success(function(records) {
callback && callback(null, records);
return callback && callback(err);
models.forEach(function(item, idx) {
result[item.tableName] = results[idx];
callback && callback(undefined, result);
* Fetch all by defined groups in parallel.
* @param {Array} groups - Groups to fetch
* @param {Function} callback - Function to call on finish
EntitiesHelper.prototype.fetchGroups = function(groups, callback)
async.map(groups, function(group, callback) {
self.fetchAll(group.models, group.criteria || null, function(err, records) {
return callback && callback(err);
callback && callback(null, records);
return callback && callback(err);
groups.forEach(function(group, idx) {
result[group.name] = results[idx];
callback && callback(undefined, result);