Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Jan 10, 2020
1 parent ace629d commit 95178d1
Showing 1 changed file with 91 additions and 85 deletions.
176 changes: 91 additions & 85 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -1644,102 +1644,108 @@ DataAccessObject.find = function find(query, options, cb) {
}

const allCb = function(err, data) {
if (!err && Array.isArray(data)) {
async.map(data, function(item, next) {
const Model = self.lookupModel(item);
if (options.notify === false) {
buildResult(item, next);
} else {
withNotify(item, next);
}
if (err || !Array.isArray(data)) {
return cb(err, data || []);
}

function buildResult(data, callback) {
const ctorOpts = {
fields: query.fields,
applySetters: false,
persisted: true,
};
if (Model.settings.applyDefaultsOnReads === false) {
ctorOpts.applyDefaultValues = false;
}
let obj;
try {
obj = new Model(data, ctorOpts);
} catch (err) {
return callback(err);
}
function done(err, results) {
if (err) return cb(err);

if (query && query.include) {
if (query.collect) {
// The collect property indicates that the query is to return the
// standalone items for a related model, not as child of the parent object
// For example, article.tags
obj = obj.__cachedRelations[query.collect];
if (obj === null) {
obj = undefined;
}
} else {
// This handles the case to return parent items including the related
// models. For example, Article.find({include: 'tags'}, ...);
// Try to normalize the include
const includes = Inclusion.normalizeInclude(query.include || []);
includes.forEach(function(inc) {
let relationName = inc;
if (utils.isPlainObject(inc)) {
relationName = Object.keys(inc)[0];
}
// When applying query.collect, some root items may not have
// any related/linked item. We store `undefined` in the results
// array in such case, which is not desirable from API consumer's
// point of view.
results = results.filter(isDefined);

// Promote the included model as a direct property
let included = obj.__cachedRelations[relationName];
if (Array.isArray(included)) {
included = new List(included, null, obj);
}
if (included) obj.__data[relationName] = included;
});
delete obj.__data.__cachedRelations;
}
}
if (data && data.countBeforeLimit) {
results.countBeforeLimit = data.countBeforeLimit;
}
if (!supportsGeo && near) {
results = geo.filter(results, near);
}

callback(null, obj);
}
cb(err, results);
}

function withNotify(data, callback) {
const context = {
Model: Model,
data: data,
isNewInstance: false,
hookState: hookState,
options: options,
};
processQueryResults(data, done);
};

Model.notifyObserversOf('loaded', context, function(err) {
if (err) return callback(err);
buildResult(context.data, callback);
});
function processQueryResults(data, cb) {
async.map(data, function processSingleQueryResult(item, next) {
const Model = self.lookupModel(item);
if (options.notify === false) {
buildResult(item, next);
} else {
withNotify(item, next);
}

function buildResult(data, callback) {
const ctorOpts = {
fields: query.fields,
applySetters: false,
persisted: true,
};
if (Model.settings.applyDefaultsOnReads === false) {
ctorOpts.applyDefaultValues = false;
}
let obj;
try {
obj = new Model(data, ctorOpts);
} catch (err) {
return callback(err);
}
},
function(err, results) {
if (err) return cb(err);

// When applying query.collect, some root items may not have
// any related/linked item. We store `undefined` in the results
// array in such case, which is not desirable from API consumer's
// point of view.
results = results.filter(isDefined);
if (query && query.include) {
if (query.collect) {
// The collect property indicates that the query is to return the
// standalone items for a related model, not as child of the parent object
// For example, article.tags
obj = obj.__cachedRelations[query.collect];
if (obj === null) {
obj = undefined;
}
} else {
// This handles the case to return parent items including the related
// models. For example, Article.find({include: 'tags'}, ...);
// Try to normalize the include
const includes = Inclusion.normalizeInclude(query.include || []);
includes.forEach(function(inc) {
let relationName = inc;
if (utils.isPlainObject(inc)) {
relationName = Object.keys(inc)[0];
}

if (data && data.countBeforeLimit) {
results.countBeforeLimit = data.countBeforeLimit;
}
if (!supportsGeo && near) {
results = geo.filter(results, near);
// Promote the included model as a direct property
let included = obj.__cachedRelations[relationName];
if (Array.isArray(included)) {
included = new List(included, null, obj);
}
if (included) obj.__data[relationName] = included;
});
delete obj.__data.__cachedRelations;
}
}

cb(err, results);
});
} else {
cb(err, data || []);
}
};
callback(null, obj);
}

function withNotify(data, callback) {
const context = {
Model: Model,
data: data,
isNewInstance: false,
hookState: hookState,
options: options,
};

Model.notifyObserversOf('loaded', context, function(err) {
if (err) return callback(err);
buildResult(context.data, callback);
});
}
},
cb);
}

if (options.notify === false) {
invokeConnectorMethod(connector, 'all', self, [query], options, allCb);
Expand Down

0 comments on commit 95178d1

Please sign in to comment.