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

Export .promises from MongoManager

Simon Detheridge 6 лет назад
Родитель
Сommit
9724407e31

+ 9 - 13
services/docstore/app/js/DocArchiveManager.js

@@ -1,5 +1,5 @@
-const { promisify, callbackify } = require('util')
-const MongoManager = require('./MongoManager')
+const { callbackify } = require('util')
+const MongoManager = require('./MongoManager').promises
 const Errors = require('./Errors')
 const logger = require('logger-sharelatex')
 const settings = require('settings-sharelatex')
@@ -29,7 +29,7 @@ module.exports = {
 }
 
 async function archiveAllDocs(projectId) {
-  const docs = await promisify(MongoManager.getProjectsDocs)(
+  const docs = await MongoManager.getProjectsDocs(
     projectId,
     { include_deleted: true },
     { lines: true, ranges: true, rev: true, inS3: true }
@@ -76,11 +76,11 @@ async function archiveDoc(projectId, doc) {
   await PersistorManager.sendStream(settings.docstore.bucket, key, stream, {
     sourceMd5: md5
   })
-  await promisify(MongoManager.markDocAsArchived)(doc._id, doc.rev)
+  await MongoManager.markDocAsArchived(doc._id, doc.rev)
 }
 
 async function unArchiveAllDocs(projectId) {
-  const docs = await promisify(MongoManager.getArchivedProjectDocs)(projectId)
+  const docs = await MongoManager.getArchivedProjectDocs(projectId)
   if (!docs) {
     throw new Errors.NotFoundError(`No docs for project ${projectId}`)
   }
@@ -131,16 +131,12 @@ async function unarchiveDoc(projectId, docId) {
   } else {
     throw new Error("I don't understand the doc format in s3")
   }
-  await promisify(MongoManager.upsertIntoDocCollection)(
-    projectId,
-    docId,
-    mongoDoc
-  )
+  await MongoManager.upsertIntoDocCollection(projectId, docId, mongoDoc)
   await PersistorManager.deleteObject(settings.docstore.bucket, key)
 }
 
 async function destroyAllDocs(projectId) {
-  const docs = await promisify(MongoManager.getProjectsDocs)(
+  const docs = await MongoManager.getProjectsDocs(
     projectId,
     { include_deleted: true },
     { _id: 1 }
@@ -157,7 +153,7 @@ async function destroyDoc(projectId, docId) {
     { project_id: projectId, doc_id: docId },
     'removing doc from mongo and persistor'
   )
-  const doc = await promisify(MongoManager.findDoc)(projectId, docId, {
+  const doc = await MongoManager.findDoc(projectId, docId, {
     inS3: 1
   })
   if (!doc) {
@@ -170,7 +166,7 @@ async function destroyDoc(projectId, docId) {
       `${projectId}/${docId}`
     )
   }
-  await promisify(MongoManager.destroyDoc)(docId)
+  await MongoManager.destroyDoc(docId)
 }
 
 async function _streamToString(stream) {

+ 8 - 10
services/docstore/app/js/MongoManager.js

@@ -14,6 +14,7 @@ let MongoManager
 const { db, ObjectId } = require('./mongojs')
 const logger = require('logger-sharelatex')
 const metrics = require('metrics-sharelatex')
+const { promisify } = require('util')
 
 module.exports = MongoManager = {
   findDoc(project_id, doc_id, filter, callback) {
@@ -162,14 +163,11 @@ module.exports = MongoManager = {
     )
   }
 }
-;[
-  'findDoc',
-  'getProjectsDocs',
-  'getArchivedProjectDocs',
-  'upsertIntoDocCollection',
-  'markDocAsArchived',
-  'getDocVersion',
-  'setDocVersion'
-].map((method) =>
+
+const methods = Object.getOwnPropertyNames(MongoManager)
+
+module.exports.promises = {}
+for (const method of methods) {
   metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
-)
+  module.exports.promises[method] = promisify(module.exports[method])
+}

+ 36 - 38
services/docstore/test/unit/js/DocArchiveManagerTests.js

@@ -123,17 +123,19 @@ describe('DocArchiveManager', function () {
     }
 
     MongoManager = {
-      markDocAsArchived: sinon.stub().yields(),
-      upsertIntoDocCollection: sinon.stub().yields(),
-      getProjectsDocs: sinon.stub().yields(null, mongoDocs),
-      getArchivedProjectDocs: sinon.stub().yields(null, archivedDocs),
-      findDoc: sinon.stub().yields(),
-      destroyDoc: sinon.stub().yields()
+      promises: {
+        markDocAsArchived: sinon.stub().resolves(),
+        upsertIntoDocCollection: sinon.stub().resolves(),
+        getProjectsDocs: sinon.stub().resolves(mongoDocs),
+        getArchivedProjectDocs: sinon.stub().resolves(archivedDocs),
+        findDoc: sinon.stub().resolves(),
+        destroyDoc: sinon.stub().resolves()
+      }
     }
     for (const mongoDoc of mongoDocs) {
-      MongoManager.findDoc
-        .withArgs(projectId, mongoDoc._id, sinon.match.any)
-        .yields(null, mongoDoc)
+      MongoManager.promises.findDoc
+        .withArgs(projectId, mongoDoc._id)
+        .resolves(mongoDoc)
     }
 
     DocArchiveManager = SandboxedModule.require(modulePath, {
@@ -223,7 +225,7 @@ describe('DocArchiveManager', function () {
 
     it('should mark the doc as archived', async function () {
       await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0])
-      expect(MongoManager.markDocAsArchived).to.have.been.calledWith(
+      expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
         mongoDocs[0]._id,
         mongoDocs[0].rev
       )
@@ -270,7 +272,7 @@ describe('DocArchiveManager', function () {
     it('should update the doc lines in mongo', async function () {
       await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
       expect(
-        MongoManager.upsertIntoDocCollection
+        MongoManager.promises.upsertIntoDocCollection
       ).to.have.been.calledWith(projectId, docId, { lines: mongoDocs[0].lines })
     })
 
@@ -297,11 +299,9 @@ describe('DocArchiveManager', function () {
 
         it('should return the docs lines', async function () {
           await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
-          expect(MongoManager.upsertIntoDocCollection).to.have.been.calledWith(
-            projectId,
-            docId,
-            mongoDoc
-          )
+          expect(
+            MongoManager.promises.upsertIntoDocCollection
+          ).to.have.been.calledWith(projectId, docId, mongoDoc)
         })
       })
 
@@ -322,11 +322,9 @@ describe('DocArchiveManager', function () {
 
         it('should return the doc lines and ranges', async function () {
           await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
-          expect(MongoManager.upsertIntoDocCollection).to.have.been.calledWith(
-            projectId,
-            docId,
-            mongoDoc
-          )
+          expect(
+            MongoManager.promises.upsertIntoDocCollection
+          ).to.have.been.calledWith(projectId, docId, mongoDoc)
         })
       })
 
@@ -345,11 +343,9 @@ describe('DocArchiveManager', function () {
 
         it('should return only the doc lines', async function () {
           await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
-          expect(MongoManager.upsertIntoDocCollection).to.have.been.calledWith(
-            projectId,
-            docId,
-            mongoDoc
-          )
+          expect(
+            MongoManager.promises.upsertIntoDocCollection
+          ).to.have.been.calledWith(projectId, docId, mongoDoc)
         })
       })
 
@@ -435,27 +431,27 @@ describe('DocArchiveManager', function () {
     it('should archive all project docs which are not in s3', async function () {
       await DocArchiveManager.promises.archiveAllDocs(projectId)
       // not inS3
-      expect(MongoManager.markDocAsArchived).to.have.been.calledWith(
+      expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
         mongoDocs[0]._id
       )
-      expect(MongoManager.markDocAsArchived).to.have.been.calledWith(
+      expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
         mongoDocs[1]._id
       )
-      expect(MongoManager.markDocAsArchived).to.have.been.calledWith(
+      expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
         mongoDocs[4]._id
       )
 
       // inS3
-      expect(MongoManager.markDocAsArchived).not.to.have.been.calledWith(
-        mongoDocs[2]._id
-      )
-      expect(MongoManager.markDocAsArchived).not.to.have.been.calledWith(
-        mongoDocs[3]._id
-      )
+      expect(
+        MongoManager.promises.markDocAsArchived
+      ).not.to.have.been.calledWith(mongoDocs[2]._id)
+      expect(
+        MongoManager.promises.markDocAsArchived
+      ).not.to.have.been.calledWith(mongoDocs[3]._id)
     })
 
     it('should return error if the project has no docs', async function () {
-      MongoManager.getProjectsDocs.yields(null, null)
+      MongoManager.promises.getProjectsDocs.resolves(null)
 
       await expect(
         DocArchiveManager.promises.archiveAllDocs(projectId)
@@ -481,7 +477,7 @@ describe('DocArchiveManager', function () {
     })
 
     it('should return error if the project has no docs', async function () {
-      MongoManager.getArchivedProjectDocs.yields(null, null)
+      MongoManager.promises.getArchivedProjectDocs.resolves(null)
 
       await expect(
         DocArchiveManager.promises.unArchiveAllDocs(projectId)
@@ -519,7 +515,9 @@ describe('DocArchiveManager', function () {
       await DocArchiveManager.promises.destroyAllDocs(projectId)
 
       for (const mongoDoc of mongoDocs) {
-        expect(MongoManager.destroyDoc).to.have.been.calledWith(mongoDoc._id)
+        expect(MongoManager.promises.destroyDoc).to.have.been.calledWith(
+          mongoDoc._id
+        )
       }
     })
   })

+ 3 - 0
services/docstore/test/unit/js/MongoManagerTests.js

@@ -29,6 +29,9 @@ describe('MongoManager', function () {
         },
         'metrics-sharelatex': { timeAsyncMethod: sinon.stub() },
         'logger-sharelatex': { log() {} }
+      },
+      globals: {
+        console
       }
     })
     this.project_id = ObjectId().toString()