Просмотр исходного кода

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

Jakob Ackermann 6 лет назад
Родитель
Сommit
cb4a7516ac

+ 15 - 6
services/spelling/app.js

@@ -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

+ 0 - 5
services/spelling/app/js/DB.js

@@ -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'])

+ 4 - 4
services/spelling/app/js/LearnedWordsManager.js

@@ -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 - 0
services/spelling/app/js/mongodb.js

@@ -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
+}

Разница между файлами не показана из-за своего большого размера
+ 70 - 560
services/spelling/package-lock.json


+ 1 - 1
services/spelling/package.json

@@ -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",

+ 2 - 0
services/spelling/test/acceptance/js/Init.js

@@ -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)
 })

+ 6 - 6
services/spelling/test/unit/js/LearnedWordsManagerTests.js

@@ -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()

Некоторые файлы не были показаны из-за большого количества измененных файлов