[misc] migrate the app to the native mongo driver, drop mongojs

This commit is contained in:
Jakob Ackermann 2020-09-03 17:05:13 +01:00
parent 6177cf7e51
commit cb4a7516ac
8 changed files with 188 additions and 768 deletions

View file

@ -16,6 +16,7 @@ if ((Settings.sentry != null ? Settings.sentry.dsn : undefined) != null) {
} }
metrics.memory.monitor(logger) metrics.memory.monitor(logger)
const mongodb = require('./app/js/mongodb')
const SpellingAPIController = require('./app/js/SpellingAPIController') const SpellingAPIController = require('./app/js/SpellingAPIController')
const express = require('express') const express = require('express')
const app = express() const app = express()
@ -44,12 +45,20 @@ const port = settings && settings.port ? settings.port : 3005
if (!module.parent) { if (!module.parent) {
// application entry point, called directly // application entry point, called directly
mongodb
.waitForDb()
.then(() => {
app.listen(port, host, function (error) { app.listen(port, host, function (error) {
if (error != null) { if (error != null) {
throw error throw error
} }
return logger.info(`spelling starting up, listening on ${host}:${port}`) 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 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 mongoCache = require('./MongoCache')
const logger = require('logger-sharelatex') const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex') const metrics = require('metrics-sharelatex')
@ -11,7 +11,7 @@ const LearnedWordsManager = {
callback = () => {} callback = () => {}
} }
mongoCache.del(userToken) mongoCache.del(userToken)
return db.spellingPreferences.update( return db.spellingPreferences.updateOne(
{ {
token: userToken token: userToken
}, },
@ -30,7 +30,7 @@ const LearnedWordsManager = {
callback = () => {} callback = () => {}
} }
mongoCache.del(userToken) mongoCache.del(userToken)
return db.spellingPreferences.update( return db.spellingPreferences.updateOne(
{ {
token: userToken token: userToken
}, },
@ -78,7 +78,7 @@ const LearnedWordsManager = {
if (callback == null) { if (callback == null) {
callback = () => {} callback = () => {}
} }
db.spellingPreferences.remove({ token: userToken }, callback) db.spellingPreferences.deleteOne({ token: userToken }, callback)
} }
} }

View file

@ -0,0 +1,25 @@
const Settings = require('settings-sharelatex')
const { MongoClient, ObjectId } = require('mongodb')
const clientPromise = MongoClient.connect(Settings.mongo.url)
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
}

File diff suppressed because it is too large Load diff

View file

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

View file

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

View file

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