mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
[misc] migrate the app to the native mongo driver, drop mongojs
This commit is contained in:
parent
6177cf7e51
commit
cb4a7516ac
8 changed files with 188 additions and 768 deletions
|
@ -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
|
||||
|
|
|
@ -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'])
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
25
services/spelling/app/js/mongodb.js
Normal file
25
services/spelling/app/js/mongodb.js
Normal 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
|
||||
}
|
881
services/spelling/package-lock.json
generated
881
services/spelling/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue