Skip to content

Commit

Permalink
Merge pull request #233 from flyvictor/PO-3204-mongo-upgrade-is-coming
Browse files Browse the repository at this point in the history
chore(PO-3204): fixed
  • Loading branch information
EugeneKostrikov authored Apr 8, 2022
2 parents ce7a773 + 834734a commit 05ccd3f
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
58 changes: 56 additions & 2 deletions lib/adapters/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ adapter.delete = function (model, id) {
});
};


/**
*
* @param model {Model}
Expand All @@ -348,7 +349,9 @@ adapter.delete = function (model, id) {
*/
adapter.find = function(model, query, projection){
if (!_.isObject(query)) query = {id: query};

projection = projection || {};
projection.select = removeUselessProjectionSelect(projection.select);
projection.limit = 1;
return new Promise(function(resolve, reject){
return adapter.findMany(model, query, projection).then(function(resources){
Expand Down Expand Up @@ -505,7 +508,7 @@ adapter._findMany = function(model, query, projection, count) {
}

projection = projection || {};
projection.select = projection.select || '';
projection.select = removeUselessProjectionSelect(projection.select);
projection.skip = 0;

if (projection.page && projection.page > 0) {
Expand Down Expand Up @@ -1110,7 +1113,6 @@ adapter.aggregate = function(model, options) {

// expose mongoose
adapter.mongoose = mongoose;

module.exports = adapter;

//Helpers
Expand All @@ -1133,3 +1135,55 @@ function buildQueryObject(key, value){
temp[key] = value;
return temp;
}

function removeUselessProjectionSelect (select, model) {
if (!select) return '';

const isSelectArray = _.isArray(select);
const selectKeys = isSelectArray
? select
: _.keys(select);

const {
nonNestedSelect,
nestedSelect,
} = _.reduce(selectKeys, function (acc, key) {
if (_.includes(key, '.')) {
acc.nestedSelect.push(key);
} else {
acc.nonNestedSelect.push(key);
}
return acc;
}, {
nonNestedSelect: [],
nestedSelect: [],
});

if (!nonNestedSelect.length || !nestedSelect.length) {
return select;
}

const keysToDelete = _.reduce(nonNestedSelect, function (acc, nonNestedKey) {
const nestedKey = nestedSelect.find((nestedKey) => nestedKey.startsWith(nonNestedKey)
&& nestedKey[nonNestedKey.length] === '.');
if (nestedKey) acc.push(nonNestedKey);
return acc;
}, []);

const collectionName = model.collection.collectionName;
keysToDelete.forEach((key) => {
console.warn(`[fortune::mongodb-adapter] Useless "${key}" select key required for model ${collectionName}.`);

if (isSelectArray) {
select = _.filter(select, el => el !== key);
} else {
delete select[key];
}
});

return select;
};

module.exports._helpers = {
removeUselessProjectionSelect,
};
92 changes: 92 additions & 0 deletions test/fortune-mongodb/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require('should');
const adapter = require('../../lib/adapters/mongodb');

module.exports = function(){
describe('MongoDB adapter helpers', function(){
describe('removeUselessProjectionSelect', function () {
const removeUselessProjectionSelect = adapter._helpers.removeUselessProjectionSelect;
const model = {
collection: {
collectionName: 'collection'
}
};
describe('object input', () => {
it('should return empty object if inpur projection is empty object', () => {
removeUselessProjectionSelect({
}).should.eql({
});
});

it('should not delete any projection key if all non nested keys are not part of nested keys', () => {
removeUselessProjectionSelect({
'nested.first': 1,
'nested.second': 1,
'first': 1,
'another': 1,
}, model).should.eql({
'nested.first': 1,
'nested.second': 1,
'first': 1,
'another': 1,
});
});
it('should delete any projection key that starts on atleast one non nested ke', () => {
removeUselessProjectionSelect({
'nested.first': 1,
'nested.second': 1,
'another.second': 1,
'another.third': 1,
'nested': 1,
'anoth': 1,
'nonNested': 1,
}, model).should.eql({
'nested.first': 1,
'nested.second': 1,
'another.second': 1,
'another.third': 1,
'nonNested': 1,
'anoth': 1,
});
});
});

describe('array input', () => {
it('should return empty array if inpur projection is empty array', () => {
removeUselessProjectionSelect([]).should.eql([]);
});

it('should not delete any projection key if all non nested keys are not part of nested keys', () => {
removeUselessProjectionSelect([
'nested.first',
'nested.second',
'first',
'another',
], model).should.eql([
'nested.first',
'nested.second',
'first',
'another',
]);
});
it('should delete any projection key that starts on atleast one non nested ke', () => {
removeUselessProjectionSelect([
'nested.first',
'nested.second',
'another.second',
'another.third',
'nested',
'anoth',
'nonNested',
], model).should.eql([
'nested.first',
'nested.second',
'another.second',
'another.third',
'anoth',
'nonNested',
]);
});
});
});
});
};
1 change: 1 addition & 0 deletions test/fortune-mongodb/mongodb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ module.exports = function(options){
});
});
});

describe('find', function(){
beforeEach(function(done){
adapter.update('person', adapter.preupdate('person', ids.people[0]), {$push: {pets: ids.pets[0]}})
Expand Down
1 change: 1 addition & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('Fortune test runner', function(){

require('./fortune/all')(options);
require('./fortune-mongodb/mongodb.spec.js')(options);
require('./fortune-mongodb/helpers.spec')();
require('./querytree')(options);


Expand Down

0 comments on commit 05ccd3f

Please sign in to comment.