Skip to content

Commit

Permalink
dependencies: upgrade mongodb and connect-mongo
Browse files Browse the repository at this point in the history
mongodb has been upgraded from version 3.6.10 to
4.5.0 and connect-mongo from version 4.4.1 to
4.6.0.
  • Loading branch information
maxime-beguin committed Apr 11, 2022
1 parent 45de123 commit fb816dd
Show file tree
Hide file tree
Showing 5 changed files with 650 additions and 601 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 8.1.0 / YYYY-MM-DD

## DEPENDENCIES

- **connect-mongo** has been upgraded from 4.4.1 to **4.6.0**
- **mongodb** has been upgraded from 3.6.10 o **4.5.0**

# 8.0.4 / 2022-04-04

## BUG FIXES
Expand Down
173 changes: 75 additions & 98 deletions lib/storages/databases/mongodb/MongoDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ MongoDatabase.buildFilter = function(resourceFilter) {
}

resourceFilter.operations.forEach(function(operation) {
if (operation.field === '_id') {
var valueType = Object.prototype.toString.call(operation.value);

if (valueType === '[object String]') {
operation.value = new mongodb.ObjectId(operation.value);
} else if (valueType === '[object Array]') {
operation.value = operation.value.map(function(value) {
return new mongodb.ObjectId(value);
});
}
}

switch (operation.type) {
case ResourceFilter.OPERATORS.EQUAL:
if (!filter[operation.field]) filter[operation.field] = {};
Expand Down Expand Up @@ -228,7 +240,6 @@ MongoDatabase.buildSort = function(sort) {
* @param {callback} callback The function to call when connection to the database is established
*/
MongoDatabase.prototype.connect = function(callback) {
var self = this;
var name = encodeURIComponent(this.username);
var password = encodeURIComponent(this.password);
var connectionUrl = 'mongodb://' + name + ':' + password + '@' + this.host + ':' + this.port;
Expand All @@ -253,12 +264,12 @@ MongoDatabase.prototype.connect = function(callback) {

// Connection succeeded
if (!error) {
self.client = client;
self.db = client.db(self.name);
this.client = client;
this.db = client.db(this.name);
}

callback(error);
}
}.bind(this)
);
};

Expand All @@ -279,17 +290,27 @@ MongoDatabase.prototype.close = function(callback) {
* @param {module:storages/MongoDatabase~MongoDatabase~addCallback} callback The function to call when it's done
*/
MongoDatabase.prototype.add = function(collection, documents, callback) {
this.db.collection(collection, function(error, fetchedCollection) {
if (error)
return callback(error);
this.db.collection(collection).insertMany(documents, function(error, insertResult) {
if (error) return callback(error);

fetchedCollection.insertMany(documents, function(error, result) {
if (error)
callback(error);
else
callback(null, result.insertedCount, result.ops);
});
});
this.get(
collection,
new ResourceFilter().in(
'_id',
Object.values(insertResult.insertedIds).map(function(objectId) {
return objectId.toHexString();
})
),
null,
insertResult.insertedCount,
0,
null,
function(getError, insertedDocuments) {
if (getError) return callback(getError);
callback(null, insertResult.insertedCount, insertedDocuments);
}
);
}.bind(this));
};

/**
Expand All @@ -302,16 +323,11 @@ MongoDatabase.prototype.add = function(collection, documents, callback) {
MongoDatabase.prototype.remove = function(collection, filter, callback) {
filter = MongoDatabase.buildFilter(filter);

this.db.collection(collection, function(error, fetchedCollection) {
this.db.collection(collection).deleteMany(filter, function(error, result) {
if (error)
return callback(error);

fetchedCollection.deleteMany(filter, function(error, result) {
if (error)
callback(error);
else
callback(null, result.deletedCount);
});
callback(error);
else
callback(null, result.deletedCount);
});
};

Expand All @@ -331,16 +347,11 @@ MongoDatabase.prototype.removeField = function(collection, property, filter, cal
update['$unset'] = {};
update['$unset'][property] = '';

this.db.collection(collection, function(error, fetchedCollection) {
this.db.collection(collection).updateMany(filter, update, function(error, result) {
if (error)
return callback(error);

fetchedCollection.updateMany(filter, update, function(error, result) {
if (error)
callback(error);
else
callback(null, result.modifiedCount);
});
callback(error);
else
callback(null, result.modifiedCount);
});
};

Expand All @@ -356,15 +367,11 @@ MongoDatabase.prototype.updateOne = function(collection, filter, data, callback)
var update = {$set: data};
filter = MongoDatabase.buildFilter(filter);

this.db.collection(collection, function(error, fetchedCollection) {
if (error) return callback(error);

fetchedCollection.updateOne(filter, update, function(error, result) {
if (error)
callback(error);
else
callback(null, result.modifiedCount);
});
this.db.collection(collection).updateOne(filter, update, function(error, result) {
if (error)
callback(error);
else
callback(null, result.modifiedCount);
});
};

Expand All @@ -385,42 +392,38 @@ MongoDatabase.prototype.updateOne = function(collection, filter, data, callback)
* @param {module:storages/MongoDatabase~MongoDatabase~getCallback} callback The function to call when it's done
*/
MongoDatabase.prototype.get = function(collection, filter, fields, limit, page, sort, callback) {
this.db.collection(collection, function(error, fetchedCollection) {
if (error) return callback(error);
limit = limit || 10;
fields = fields || {};
page = page || 0;
filter = MongoDatabase.buildFilter(filter);
sort = MongoDatabase.buildSort(sort);
var projection = MongoDatabase.buildFields(fields.include || fields.exclude, fields.include ? true : false);
var skip = limit * page || 0;

limit = limit || 10;
fields = fields || {};
page = page || 0;
filter = MongoDatabase.buildFilter(filter);
sort = MongoDatabase.buildSort(sort);
var projection = MongoDatabase.buildFields(fields.include || fields.exclude, fields.include ? true : false);
var skip = limit * page || 0;

// Automatically add the textScore projection if sorting by textScore
for (var field in sort) {
if (Object.prototype.hasOwnProperty.call(sort[field], '$meta') && sort[field].$meta === 'textScore') {
projection[field] = sort[field];
break;
}
// Automatically add the textScore projection if sorting by textScore
for (var field in sort) {
if (Object.prototype.hasOwnProperty.call(sort[field], '$meta') && sort[field].$meta === 'textScore') {
projection[field] = sort[field];
break;
}
}

var cursor = fetchedCollection.find(filter).project(projection).sort(sort).skip(skip).limit(limit);
cursor.toArray(function(toArrayError, documents) {
if (toArrayError) return callback(toArrayError);
this.db.collection(collection).find(filter).project(projection).sort(sort).skip(skip).limit(limit).toArray(
function(error, documents) {
if (error) return callback(error);

cursor.count(false, null, function(countError, count) {
if (countError) callback(countError);
this.db.collection(collection).countDocuments(function(countError, count) {
if (countError) return callback(countError);

callback(error, documents || [], {
callback(null, documents || [], {
limit: limit,
page: page,
pages: Math.ceil(count / limit),
size: count
});
});
});

});
}.bind(this)
);
};

/**
Expand All @@ -440,10 +443,7 @@ MongoDatabase.prototype.getOne = function(collection, filter, fields, callback)
fields = fields || {};
var projection = MongoDatabase.buildFields(fields.include || fields.exclude, fields.include ? true : false);

this.db.collection(collection, function(error, fetchedCollection) {
if (error) return callback(error);
fetchedCollection.findOne(filter, projection, callback);
});
this.db.collection(collection).findOne(filter, projection, callback);
};

/**
Expand All @@ -453,12 +453,7 @@ MongoDatabase.prototype.getOne = function(collection, filter, fields, callback)
* @param {module:storages/MongoDatabase~MongoDatabase~getIndexesCallback} callback The function to call when it's done
*/
MongoDatabase.prototype.getIndexes = function(collection, callback) {
this.db.collection(collection, function(error, fetchedCollection) {
if (error)
return callback(error);

fetchedCollection.indexes(callback);
});
this.db.collection(collection).indexes(callback);
};

/**
Expand All @@ -470,12 +465,7 @@ MongoDatabase.prototype.getIndexes = function(collection, callback) {
* done
*/
MongoDatabase.prototype.createIndexes = function(collection, indexes, callback) {
this.db.collection(collection, function(error, fetchedCollection) {
if (error)
return callback(error);

fetchedCollection.createIndexes(indexes, callback);
});
this.db.collection(collection).createIndexes(indexes, callback);
};

/**
Expand All @@ -486,12 +476,7 @@ MongoDatabase.prototype.createIndexes = function(collection, indexes, callback)
* @param {module:storages/MongoDatabase~MongoDatabase~dropIndexCallback} callback The function to call when it's done
*/
MongoDatabase.prototype.dropIndex = function(collection, indexName, callback) {
this.db.collection(collection, function(error, fetchedCollection) {
if (error)
return callback(error);

fetchedCollection.dropIndex(indexName, callback);
});
this.db.collection(collection).dropIndex(indexName, callback);
};

/**
Expand All @@ -512,8 +497,6 @@ MongoDatabase.prototype.getStore = function(collection) {
* @param {callback} callback The function to call when it's done
*/
MongoDatabase.prototype.renameCollection = function(collection, target, callback) {
var self = this;

this.db.listCollections({name: collection}).toArray(function(error, collections) {
if (error) return callback(error);
if (!collections || !collections.length) {
Expand All @@ -522,14 +505,8 @@ MongoDatabase.prototype.renameCollection = function(collection, target, callback
);
}

self.db.collection(collection, function(error, fetchedCollection) {
if (error) return callback(error);

fetchedCollection.rename(target, function(error) {
callback(error);
});
});
});
this.db.collection(collection).rename(target, callback);
}.bind(this));
};

/**
Expand Down
Loading

0 comments on commit fb816dd

Please sign in to comment.