Skip to content

Commit

Permalink
refactor: ♻️ ensure select field with data
Browse files Browse the repository at this point in the history
  • Loading branch information
rijkerd committed Jun 5, 2021
1 parent b2dfdf1 commit d3f9727
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,35 +420,31 @@ export const populate = (options, done) => {
async.waterfall(
[
// parse field selections
function parseSelect(next) {
(next) => {
select(query, next);
},

// normalize populations
function normalize(selections, next) {
populations = _.map(populations, (population) => {
return (cb) => {
// obtain population selected fields
const fields = selections
? selections[population.path]
: undefined;
if (fields) {
// eslint-disable-next-line no-param-reassign
population = _.merge({}, population, { select: fields });
}

// parse path specific select
if (population.select) {
select(population, (error, data) => {
cb(error, _.merge({}, population, { data }));
});
}

// continue
else {
cb(null, population);
}
};
(selections, next) => {
populations = _.map(populations, (population) => (cb) => {
// obtain population selected fields
const fields = selections ? selections[population.path] : undefined;
if (fields) {
// eslint-disable-next-line no-param-reassign
population = _.merge({}, population, { select: fields });
}

// parse path specific select
if (population.select) {
select(population, (error, data) => {
cb(error, _.merge({}, population, { select: data }));
});
}

// continue
else {
cb(null, population);
}
});

async.parallel(populations, next);
Expand Down

0 comments on commit d3f9727

Please sign in to comment.