Skip to content

Commit

Permalink
fix(deps): Migrate from MongoDB 2.2.36 to 3.1.13 (#356)
Browse files Browse the repository at this point in the history
* chore(deps): bump mongodb from 2.2.36 to 3.1.13

Bumps [mongodb](https://github.com/mongodb/node-mongodb-native) from 2.2.36 to 3.1.13.
- [Release notes](https://github.com/mongodb/node-mongodb-native/releases)
- [Changelog](https://github.com/mongodb/node-mongodb-native/blob/master/CHANGES_3.0.0.md)
- [Commits](mongodb/node-mongodb-native@v2.2.36...v3.1.13)

Signed-off-by: dependabot[bot] <support@github.com>

* fix "the options [useUnifiedTopology] is not supported"

* fix "collection.count is deprecated"

* fix "collection.update is deprecated."

* fix "collection.find option [fields] is deprecated" for notif tests

* fix "collection.remove is deprecated."

* fix "Third parameter to `find()` must be a callback or undefined"

* fix "Cursor.each is deprecated."

* fix use of cursor.forEach()

* fix timeout on clearing notifications

* fix auth api tests: cursor.nextObject() --> next()

* fix aggregate for post api tests

* fix "exports.updateOne is not a function"

* codacy fixes

* url-encode auth from connection string
cf https://github.com/mongodb/node-mongodb-native/blob/3.6/CHANGES_3.0.0.md#connection-string

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Adrien Joly <531781+adrienjoly@users.noreply.github.com>
  • Loading branch information
dependabot[bot] and adrienjoly committed Nov 1, 2020
1 parent 1c89c45 commit 75fa55c
Show file tree
Hide file tree
Showing 27 changed files with 267 additions and 254 deletions.
5 changes: 4 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ function makeMongoUrl(params) {
const user = params.mongoDbAuthUser;
const password = params.mongoDbAuthPassword;
const db = params.mongoDbDatabase; // ?w=0
const auth = user || password ? `${user}:${password}@` : '';
const auth =
user || password
? `${encodeURIComponent(user)}:${encodeURIComponent(password)}@`
: '';
return `mongodb://${auth}${host}:${port}/${db}`;
}

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ exports.controller = function (request, reqParams, response) {

/*
var countRecomFollows = function (callback) {
mongodb.collections['follow'].count({recom:true}, function(err,count) {
mongodb.collections['follow'].countDocuments({recom:true}, function(err,count) {
report["Number of recommended topics followed"] = count;
report["Average number of recommended topics followed by user"] = count / users.length;
callback();
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function refreshIndex(type, cb, preprocess) {
console.log('index: iterating on collection:', indexCol[type]);
mongodb.collections[indexCol[type]].find({}, options, function (err, cursor) {
(function next() {
cursor.nextObject(function (err, u) {
cursor.next(function (err, u) {
if (err)
console.log('[ERR] admin.index.refreshIndex, db.nextObject: ', err);
if (u != null) {
Expand Down Expand Up @@ -140,7 +140,7 @@ function countDbUsersAndPlaylists(cb) {
{ fields: { pl: 1 } },
function (err, cursor) {
(function nextUser() {
cursor.nextObject(function (err, user) {
cursor.next(function (err, user) {
if (!user) cb(result);
else {
++result.dbUsers;
Expand All @@ -155,7 +155,10 @@ function countDbUsersAndPlaylists(cb) {

function countItems(cb) {
countDbUsersAndPlaylists(function (p) {
mongodb.collections[indexCol['post']].count(function (err, dbPosts) {
mongodb.collections[indexCol['post']].countDocuments(function (
err,
dbPosts
) {
p.dbPosts = dbPosts;
searchModel.countDocs('user', function (idxUsers) {
p.idxUsers = idxUsers;
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/plTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ var fileGenerators = {
},

nbTracksInPlaylists: function (p, cb) {
mongodb.collections['post'].count({}, function (err, nbTracks) {
mongodb.collections['post'].count(
mongodb.collections['post'].countDocuments({}, function (err, nbTracks) {
mongodb.collections['post'].countDocuments(
{ 'pl.id': { $exists: true } },
function (err, nbTracksInPlaylists) {
cb(
Expand Down
13 changes: 8 additions & 5 deletions app/controllers/admin/recom.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ function gatherTrackAnalytics(cb) {
{},
{ batchSize: 1000, fields: { _id: 0, eId: 1, lov: 1, name: 1 } },
function (err, cursor) {
cursor.each(function (err, track) {
cursor.forEach((err, track) => {
++nb;
if (!track)
// we're done
cb(eIds, nb);
if (!track) return;
else if (!track.eId) console.error('warning: null track eid');
else {
eIds[track.eId] = eIds[track.eId] || {
Expand All @@ -56,7 +54,12 @@ function gatherTrackAnalytics(cb) {
eIds[track.eId].posts++;
eIds[track.eId].likes += (track.lov || []).length;
}
});
}),
() => {
++nb;
// we're done
cb(eIds, nb);
};
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/testNotifs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.controller = function (request, reqParams, response) {
var user = request.checkLogin(response);
if (!user) return;

// reset prefs: db.user.update({_id:ObjectId("4d94501d1f78ac091dbc9b4d")},{$unset:{"pref":1}});
// reset prefs: db.user.updateOne({_id:ObjectId("4d94501d1f78ac091dbc9b4d")},{$unset:{"pref":1}});

var gilles = {
id: '4d7fc1969aa9db130e000003',
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var ACTIONS = {
},
listBySource: function (p, cb) {
var list = [];
mongodb.collections['track'].count(function (err, total) {
mongodb.collections['track'].countDocuments(function (err, total) {
function whenDone() {
cb(
JSON.stringify(
Expand Down Expand Up @@ -48,7 +48,7 @@ var ACTIONS = {
function whenDone() {
console.log('countPerConfidence', countPerConfidence);
}
mongodb.collections['track'].count(function (err, count) {
mongodb.collections['track'].countDocuments(function (err, count) {
var i = 0;
cb({ total: count });
mongodb.forEach2(
Expand Down
19 changes: 11 additions & 8 deletions app/controllers/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ var processData = {
else {
var uid = '' + userList[i].id;
console.log('fetchNextUserStats', i, uid);
mongodb.collections['follow'].count({ tId: uid }, function (
mongodb.collections['follow'].countDocuments({ tId: uid }, function (
err,
nbSubscribers
) {
Expand All @@ -174,20 +174,23 @@ var processData = {
{ uId: uid },
{ limit: 9999999, fields: { _id: 0, lov: 1, nbP: 1 } },
function (err, cursor) {
cursor.each(function (err, f) {
if (!f) {
mongodb.collections['post'].count(
cursor.forEach(
(err, f) => {
if (f) {
if (f.lov) userList[i].nbLikes += f.lov.length;
if (f.nbP) userList[i].nbPlays += f.nbP;
}
},
() => {
mongodb.collections['post'].countDocuments(
{ 'repost.uId': uid },
function (err, nbAdds) {
userList[i].nbAdds += nbAdds;
fetchNextUserStats(cb, i + 1);
}
);
} else {
if (f.lov) userList[i].nbLikes += f.lov.length;
if (f.nbP) userList[i].nbPlays += f.nbP;
}
});
);
}
);
});
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/subdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports.controller = function (request, reqParams, response) {
const { controller } = require(safeCtrPath);
controller(request, reqParams, response);
} catch (err) {
console.error('[subdir] no controller found at ' + request.url);
console.error('[subdir] error while contacting ' + request.url, err);
response.notFound();
}
};
6 changes: 3 additions & 3 deletions app/models/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exports.add = function (d, callback) {
};

exports.remove = function (q, callback) {
getCol().remove(q, function (err, result) {
getCol().deleteOne(q, function (err, result) {
callback && callback(result || err);
});
};
Expand All @@ -47,7 +47,7 @@ exports.remove = function (q, callback) {

/*
exports.countUserLikes = function(uid, callback) {
getCol().count({"like.pId":{$exists: true}, "id":""+uid}, function(err, count) {
getCol().countDocuments({"like.pId":{$exists: true}, "id":""+uid}, function(err, count) {
callback(count);
});
}
Expand Down Expand Up @@ -124,5 +124,5 @@ exports.addLikeByPid = function (pId, liker, callback) {
};

exports.removeLike = function (pId, likerUid, callback) {
exports.remove({ 'like.pId': pId, id: '' + likerUid }, callback);
exports.deleteOne({ 'like.pId': pId, id: '' + likerUid }, callback);
};
2 changes: 1 addition & 1 deletion app/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ exports.delete = function (p, cb) {
cb && cb({ error: 'you are not allowed to delete this comment' });
return;
}
getCol().remove(q, combineResult(cb));
getCol().deleteOne(q, combineResult(cb));
}
);
})
Expand Down
19 changes: 14 additions & 5 deletions app/models/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,26 @@ exports.add = function (followObj, dbHandler) {
};
var collection = mongodb.collections[COLNAME];
//collection.save(obj, dbHandler);
collection.update(req, followObj, { upsert: true }, function (err, result) {
collection.updateOne(req, followObj, { upsert: true }, function (
err,
result
) {
// to avoid duplicates
if (err) dbHandler(err, result);
else collection.findOne(req, dbHandler);
});
};

exports.remove = function (uId, tId, dbHandler) {
mongodb.collections[COLNAME].remove({ uId: uId, tId: tId }, dbHandler);
mongodb.collections[COLNAME].deleteOne({ uId: uId, tId: tId }, dbHandler);
};

exports.fetch = function (q, options, callback) {
fetchArray(q, options, callback);
};

exports.remove = function (uId, tId, dbHandler) {
mongodb.collections[COLNAME].remove({ uId: uId, tId: tId }, dbHandler);
mongodb.collections[COLNAME].deleteOne({ uId: uId, tId: tId }, dbHandler);
};

// wraps a cb(result) callback into a cb(err,res) callback
Expand All @@ -93,11 +96,17 @@ function wrapCallback(cb) {
}

exports.countSubscriptions = function (uId, cb) {
mongodb.collections[COLNAME].count({ uId: '' + uId }, wrapCallback(cb));
mongodb.collections[COLNAME].countDocuments(
{ uId: '' + uId },
wrapCallback(cb)
);
};

exports.countSubscribers = function (uId, cb) {
mongodb.collections[COLNAME].count({ tId: '' + uId }, wrapCallback(cb));
mongodb.collections[COLNAME].countDocuments(
{ tId: '' + uId },
wrapCallback(cb)
);
};

exports.fetchUserSubscriptions = function (uid, callback) {
Expand Down
3 changes: 2 additions & 1 deletion app/models/mongodb-shell-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function buildContext(db, nextCommand, callback) {
!err && {
dropIndex: wrapCollectionMethod(col, 'dropIndex', colName),
ensureIndex: wrapCollectionMethod(col, 'ensureIndex', colName),
update: wrapCollectionMethod(col, 'update', colName),
updateOne: wrapCollectionMethod(col, 'updateOne', colName),
updateMany: wrapCollectionMethod(col, 'updateMany', colName),
}
);
});
Expand Down
31 changes: 18 additions & 13 deletions app/models/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ exports.cacheUser = function (user) {
exports.cacheUsers = function (callback) {
console.log('Caching users ...');
userModel = userModel || require('./user.js');
userModel.fetchMulti({}, { fields: USER_CACHE_FIELDS }, function (results) {
userModel.fetchMulti({}, { projection: USER_CACHE_FIELDS }, function (
results
) {
for (let i in results) exports.cacheUser(results[i]);
console.log('Caching users: done!');
if (callback) callback();
Expand All @@ -108,10 +110,12 @@ exports.forEach = function (colName, params, handler, cb, cbParam) {
delete params.q;
}
exports.collections[colName].find(q, params, function (err, cursor) {
cursor.each(function (err, item) {
if (item) handler(item);
else if (cb) cb(cbParam);
});
cursor.forEach(
(err, item) => {
if (item) handler(item);
},
cb ? () => cb(cbParam) : undefined
);
});
};

Expand All @@ -128,7 +132,7 @@ exports.forEach2 = function (colName, params, handler) {
q._id = { $lt: exports.ObjectId('' + params.after) };
exports.collections[colName].find(q, params, function (err, cursor) {
(function next() {
cursor.nextObject(function (err, item) {
cursor.next(function (err, item) {
if (err) {
console.error('mongodb.forEach2 ERROR', err);
handler({ error: err });
Expand Down Expand Up @@ -160,7 +164,7 @@ exports.cacheCollections = function (callback) {
});
};
})();
collections[i].count(queryHandler);
collections[i].countDocuments(queryHandler);
}
}
});
Expand All @@ -176,7 +180,7 @@ exports.clearCollections = async function () {
throw new Error('allowed on test database only');
} else {
for (const name in exports.collections) {
await exports.collections[name].remove({}, { multi: true });
await exports.collections[name].deleteMany({}, { multi: true });
}
}
};
Expand Down Expand Up @@ -221,7 +225,12 @@ exports.init = function (readyCallback) {
var authPassword = process.appParams.mongoDbAuthPassword;

var authStr = '';
if (authUser && authPassword) authStr = authUser + ':' + authPassword + '@';
if (authUser && authPassword)
authStr =
encodeURIComponent(authUser) +
':' +
encodeURIComponent(authPassword) +
'@';

var url = 'mongodb://' + authStr + host + ':' + port + '/' + dbName; // + "?w=1";

Expand All @@ -230,15 +239,11 @@ exports.init = function (readyCallback) {
var options = {
native_parser: true,
useNewUrlParser: true,
useUnifiedTopology: true,
//strict: false,
//safe: false,
w: 'majority', // write concern: (value of > -1 or the string 'majority'), where < 1 means no write acknowlegement
};

//var dbserver = new mongodb.Server(host, port, {auto_reconnect:true});
//var db = new mongodb.Db(dbName, dbserver, options);

mongodb.MongoClient.connect(url, options, function (err, client) {
if (err) throw err;

Expand Down
Loading

0 comments on commit 75fa55c

Please sign in to comment.