Skip to content

Commit

Permalink
mergeProjectionAndPopulation: fix bug where projection on nested fiel…
Browse files Browse the repository at this point in the history
…ds where wrongly removed
  • Loading branch information
loris committed May 21, 2019
1 parent 6d16122 commit 0a15edb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
36 changes: 13 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,11 @@ const getProjection = projection => {
return fields;
};

const getPopulation = population => {
let paths = population.split(',');

paths = paths.map(path => {
const getPopulation = population =>
population.split(',').map(path => {
return { path };
});

return paths;
};

const getSort = sort => parseUnaries(sort);

const getSkip = skip => Number(skip);
Expand Down Expand Up @@ -221,29 +216,24 @@ const getFilter = (filter, params, options) => {
};

const mergeProjectionAndPopulation = result => {
const projection = result.projection || {};
let population = result.population || [];

Object.keys(projection).forEach(key => {
// if field contains a .
if (key.indexOf('.') > -1) {
// Loop the population rows
population = population.map(row => {
const prefix = `${row.path}.`;
const unprefixedKey = key.replace(prefix, '');
if (result.projection && result.population) {
// Loop the population rows
result.population.forEach(row => {
const prefix = `${row.path}.`;
Object.keys(result.projection).forEach(key => {
// If field start with the name of the path, we add it to the `select` property
if (key.startsWith(prefix)) {
const unprefixedKey = key.replace(prefix, '');
row.select = {
...row.select,
[unprefixedKey]: projection[key],
[unprefixedKey]: result.projection[key],
};
// Remove field with . from the projection
delete result.projection[key];
}
return row;
});
// Remove field with . from the projection
delete projection[key];
}
});
});
}
};

const operators = [
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ test('populate', t => {
});

test('populate and projection', t => {
const res = aqp('populate=a,b,c&fields=j,k,l,a.x,a.y,a.z,b.x,b.y');
const res = aqp('populate=a,b,c&fields=j,k,l,a.x,a.y,a.z,b.x,b.y,foo.bar');
t.truthy(res);
t.deepEqual(res.projection, { j: 1, k: 1, l: 1 });
t.deepEqual(res.projection, { j: 1, k: 1, l: 1, 'foo.bar': 1 });
t.deepEqual(res.population, [
{
path: 'a',
Expand Down

0 comments on commit 0a15edb

Please sign in to comment.