Merge pull request #69 from overleaf/jpa-mongodb-native

[misc] migrate to the native mongo driver
This commit is contained in:
Simon Detheridge 2020-09-18 09:52:15 +01:00 committed by GitHub
commit 57a0c5dbe0
9 changed files with 195 additions and 768 deletions

View file

@ -16,6 +16,7 @@ if ((Settings.sentry != null ? Settings.sentry.dsn : undefined) != null) {
}
metrics.memory.monitor(logger)
const mongodb = require('./app/js/mongodb')
const SpellingAPIController = require('./app/js/SpellingAPIController')
const express = require('express')
const app = express()
@ -44,12 +45,20 @@ const port = settings && settings.port ? settings.port : 3005
if (!module.parent) {
// application entry point, called directly
app.listen(port, host, function (error) {
if (error != null) {
throw error
}
return logger.info(`spelling starting up, listening on ${host}:${port}`)
})
mongodb
.waitForDb()
.then(() => {
app.listen(port, host, function (error) {
if (error != null) {
throw error
}
return logger.info(`spelling starting up, listening on ${host}:${port}`)
})
})
.catch((err) => {
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
process.exit(1)
})
}
module.exports = app

View file

@ -1,5 +0,0 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
const MongoJS = require('mongojs')
const Settings = require('settings-sharelatex')
module.exports = MongoJS(Settings.mongo.url, ['spellingPreferences'])

View file

@ -1,4 +1,4 @@
const db = require('./DB')
const { db } = require('./mongodb')
const mongoCache = require('./MongoCache')
const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex')
@ -11,7 +11,7 @@ const LearnedWordsManager = {
callback = () => {}
}
mongoCache.del(userToken)
return db.spellingPreferences.update(
return db.spellingPreferences.updateOne(
{
token: userToken
},
@ -30,7 +30,7 @@ const LearnedWordsManager = {
callback = () => {}
}
mongoCache.del(userToken)
return db.spellingPreferences.update(
return db.spellingPreferences.updateOne(
{
token: userToken
},
@ -78,7 +78,7 @@ const LearnedWordsManager = {
if (callback == null) {
callback = () => {}
}
db.spellingPreferences.remove({ token: userToken }, callback)
db.spellingPreferences.deleteOne({ token: userToken }, callback)
}
}

View file

@ -0,0 +1,28 @@
const Settings = require('settings-sharelatex')
const { MongoClient, ObjectId } = require('mongodb')
const clientPromise = MongoClient.connect(
Settings.mongo.url,
Settings.mongo.options
)
let setupDbPromise
async function waitForDb() {
if (!setupDbPromise) {
setupDbPromise = setupDb()
}
await setupDbPromise
}
const db = {}
async function setupDb() {
const internalDb = (await clientPromise).db()
db.spellingPreferences = internalDb.collection('spellingPreferences')
}
module.exports = {
db,
ObjectId,
waitForDb
}

View file

@ -9,6 +9,10 @@ module.exports = {
},
mongo: {
options: {
useUnifiedTopology:
(process.env.MONGO_USE_UNIFIED_TOPOLOGY || 'true') === 'true'
},
url:
process.env.MONGO_CONNECTION_STRING ||
`mongodb://${process.env.MONGO_HOST || 'localhost'}/sharelatex`

File diff suppressed because it is too large Load diff

View file

@ -32,7 +32,7 @@
"logger-sharelatex": "^2.2.0",
"lru-cache": "^5.1.1",
"metrics-sharelatex": "^2.6.2",
"mongojs": "3.1.0",
"mongodb": "^3.6.0",
"node-statsd": "0.1.1",
"request": "^2.88.2",
"settings-sharelatex": "^1.1.0",

View file

@ -1,6 +1,8 @@
const { waitForDb } = require('../../../app/js/mongodb')
const App = require('../../../app.js')
const { PORT } = require('./helpers/request')
before(waitForDb)
before(function (done) {
return App.listen(PORT, 'localhost', done)
})

View file

@ -13,7 +13,7 @@ describe('LearnedWordsManager', function () {
this.callback = sinon.stub()
this.db = {
spellingPreferences: {
update: sinon.stub().yields()
updateOne: sinon.stub().yields()
}
}
this.cache = {
@ -26,7 +26,7 @@ describe('LearnedWordsManager', function () {
console: console
},
requires: {
'./DB': this.db,
'./mongodb': { db: this.db },
'./MongoCache': this.cache,
'logger-sharelatex': {
log() {},
@ -49,7 +49,7 @@ describe('LearnedWordsManager', function () {
it('should insert the word in the word list in the database', function () {
expect(
this.db.spellingPreferences.update.calledWith(
this.db.spellingPreferences.updateOne.calledWith(
{
token: this.token
},
@ -76,7 +76,7 @@ describe('LearnedWordsManager', function () {
it('should remove the word from the word list in the database', function () {
expect(
this.db.spellingPreferences.update.calledWith(
this.db.spellingPreferences.updateOne.calledWith(
{
token: this.token
},
@ -150,12 +150,12 @@ describe('LearnedWordsManager', function () {
describe('deleteUsersLearnedWords', function () {
beforeEach(function () {
this.db.spellingPreferences.remove = sinon.stub().callsArgWith(1)
this.db.spellingPreferences.deleteOne = sinon.stub().callsArgWith(1)
})
it('should get the word list for the given user', function (done) {
this.LearnedWordsManager.deleteUsersLearnedWords(this.token, () => {
this.db.spellingPreferences.remove
this.db.spellingPreferences.deleteOne
.calledWith({ token: this.token })
.should.equal(true)
done()