From 4080784310a34aa2b5fe22d486fa003b133b32a0 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Thu, 27 Aug 2020 16:31:27 +0100 Subject: [PATCH] [misc] simplify mongodb collection access using a shared db construct Resolve the getCollection Promises once and store the result in a shared `db` object which can get imported by all the call-sites. The http server is starting only after a Promise of `waitForDb()` resolves. This covers the app code and the acceptance tests: REF: 586706a9439c3591fc9613dc877f055096ca073a REF: d026569d2eb4123e30c771a55a001b42d5ade72f --- .../js/Features/Messages/MessageManager.js | 80 +++++------- .../app/js/Features/Threads/ThreadManager.js | 120 ++++++++---------- services/chat/app/js/mongodb.js | 7 + 3 files changed, 94 insertions(+), 113 deletions(-) diff --git a/services/chat/app/js/Features/Messages/MessageManager.js b/services/chat/app/js/Features/Messages/MessageManager.js index 09da81fc3c..bc1afda0c2 100644 --- a/services/chat/app/js/Features/Messages/MessageManager.js +++ b/services/chat/app/js/Features/Messages/MessageManager.js @@ -13,12 +13,10 @@ * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ let MessageManager -const { ObjectId, getCollection } = require('../../mongodb') +const { db, ObjectId } = require('../../mongodb') const metrics = require('@overleaf/metrics') const logger = require('logger-sharelatex') -const messagesCollectionPromise = getCollection('messages') - module.exports = MessageManager = { createMessage(room_id, user_id, content, timestamp, callback) { if (callback == null) { @@ -31,15 +29,13 @@ module.exports = MessageManager = { timestamp } newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts) - messagesCollectionPromise.then((messages) => - messages.insertOne(newMessageOpts, function (error, confirmation) { - if (error) { - return callback(error) - } - newMessageOpts._id = confirmation.insertedId - callback(null, newMessageOpts) - }) - ) + db.messages.insertOne(newMessageOpts, function (error, confirmation) { + if (error) { + return callback(error) + } + newMessageOpts._id = confirmation.insertedId + callback(null, newMessageOpts) + }) }, getMessages(room_id, limit, before, callback) { @@ -51,39 +47,33 @@ module.exports = MessageManager = { query.timestamp = { $lt: before } } query = this._ensureIdsAreObjectIds(query) - messagesCollectionPromise.then((messages) => - messages - .find(query) - .sort({ timestamp: -1 }) - .limit(limit) - .toArray(callback) - ) + db.messages + .find(query) + .sort({ timestamp: -1 }) + .limit(limit) + .toArray(callback) }, findAllMessagesInRooms(room_ids, callback) { if (callback == null) { callback = function (error, messages) {} } - messagesCollectionPromise.then((messages) => - messages - .find({ - room_id: { $in: room_ids } - }) - .toArray(callback) - ) + db.messages + .find({ + room_id: { $in: room_ids } + }) + .toArray(callback) }, deleteAllMessagesInRoom(room_id, callback) { if (callback == null) { callback = function (error) {} } - messagesCollectionPromise.then((messages) => - messages.deleteMany( - { - room_id - }, - callback - ) + db.messages.deleteMany( + { + room_id + }, + callback ) }, @@ -95,17 +85,15 @@ module.exports = MessageManager = { _id: message_id, room_id }) - messagesCollectionPromise.then((messages) => - messages.updateOne( - query, - { - $set: { - content, - edited_at: timestamp - } - }, - callback - ) + db.messages.updateOne( + query, + { + $set: { + content, + edited_at: timestamp + } + }, + callback ) }, @@ -117,9 +105,7 @@ module.exports = MessageManager = { _id: message_id, room_id }) - messagesCollectionPromise.then((messages) => - messages.deleteOne(query, callback) - ) + db.messages.deleteOne(query, callback) }, _ensureIdsAreObjectIds(query) { diff --git a/services/chat/app/js/Features/Threads/ThreadManager.js b/services/chat/app/js/Features/Threads/ThreadManager.js index db1f994297..6c02767a55 100644 --- a/services/chat/app/js/Features/Threads/ThreadManager.js +++ b/services/chat/app/js/Features/Threads/ThreadManager.js @@ -12,12 +12,10 @@ * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ let ThreadManager -const { ObjectId, getCollection } = require('../../mongodb') +const { db, ObjectId } = require('../../mongodb') const logger = require('logger-sharelatex') const metrics = require('@overleaf/metrics') -const roomsCollectionPromise = getCollection('rooms') - module.exports = ThreadManager = { GLOBAL_THREAD: 'GLOBAL', @@ -50,58 +48,52 @@ module.exports = ThreadManager = { } } - roomsCollectionPromise.then((rooms) => - rooms.updateOne(query, { $set: update }, { upsert: true }, function ( - error - ) { - if (error != null) { - return callback(error) - } - rooms.findOne(query, callback) - }) - ) + db.rooms.updateOne(query, { $set: update }, { upsert: true }, function ( + error + ) { + if (error != null) { + return callback(error) + } + db.rooms.findOne(query, callback) + }) }, findAllThreadRooms(project_id, callback) { if (callback == null) { callback = function (error, rooms) {} } - roomsCollectionPromise.then((rooms) => - rooms - .find( - { - project_id: ObjectId(project_id.toString()), - thread_id: { $exists: true } - }, - { - thread_id: 1, - resolved: 1 - } - ) - .toArray(callback) - ) + db.rooms + .find( + { + project_id: ObjectId(project_id.toString()), + thread_id: { $exists: true } + }, + { + thread_id: 1, + resolved: 1 + } + ) + .toArray(callback) }, resolveThread(project_id, thread_id, user_id, callback) { if (callback == null) { callback = function (error) {} } - roomsCollectionPromise.then((rooms) => - rooms.updateOne( - { - project_id: ObjectId(project_id.toString()), - thread_id: ObjectId(thread_id.toString()) - }, - { - $set: { - resolved: { - user_id, - ts: new Date() - } + db.rooms.updateOne( + { + project_id: ObjectId(project_id.toString()), + thread_id: ObjectId(thread_id.toString()) + }, + { + $set: { + resolved: { + user_id, + ts: new Date() } - }, - callback - ) + } + }, + callback ) }, @@ -109,19 +101,17 @@ module.exports = ThreadManager = { if (callback == null) { callback = function (error) {} } - roomsCollectionPromise.then((rooms) => - rooms.updateOne( - { - project_id: ObjectId(project_id.toString()), - thread_id: ObjectId(thread_id.toString()) - }, - { - $unset: { - resolved: true - } - }, - callback - ) + db.rooms.updateOne( + { + project_id: ObjectId(project_id.toString()), + thread_id: ObjectId(thread_id.toString()) + }, + { + $unset: { + resolved: true + } + }, + callback ) }, @@ -136,18 +126,16 @@ module.exports = ThreadManager = { if (error != null) { return callback(error) } - roomsCollectionPromise.then((rooms) => - rooms.deleteOne( - { - _id: room._id - }, - function (error) { - if (error != null) { - return callback(error) - } - return callback(null, room._id) + db.rooms.deleteOne( + { + _id: room._id + }, + function (error) { + if (error != null) { + return callback(error) } - ) + return callback(null, room._id) + } ) }) } diff --git a/services/chat/app/js/mongodb.js b/services/chat/app/js/mongodb.js index 6dfb575b7e..928a3222d1 100644 --- a/services/chat/app/js/mongodb.js +++ b/services/chat/app/js/mongodb.js @@ -11,7 +11,14 @@ async function waitForDb() { await clientPromise } +const db = {} +waitForDb().then(async function () { + db.messages = await getCollection('messages') + db.rooms = await getCollection('rooms') +}) + module.exports = { + db, ObjectId, getCollection, waitForDb