Explorar o código

Merge pull request #3186 from overleaf/jpa-mongodb-native-prep

[misc] mongodb: use findOne and find().toArray()

GitOrigin-RevId: 52457b1a73c1b49ff78ff9b3096dd80e330fcf25
Jakob Ackermann %!s(int64=6) %!d(string=hai) anos
pai
achega
12b407c18b

+ 2 - 2
services/web/app/src/Features/Project/ProjectGetter.js

@@ -116,7 +116,7 @@ const ProjectGetter = {
       return callback(err)
     }
 
-    return db.projects.find(query, projection, function(err, project) {
+    return db.projects.findOne(query, projection, function(err, project) {
       if (err != null) {
         OError.tag(err, 'error getting project', {
           query,
@@ -124,7 +124,7 @@ const ProjectGetter = {
         })
         return callback(err)
       }
-      return callback(null, project != null ? project[0] : undefined)
+      return callback(null, project)
     })
   },
 

+ 4 - 4
services/web/app/src/Features/User/UserGetter.js

@@ -103,7 +103,7 @@ const UserGetter = {
         $elemMatch: { email: { $in: emails }, confirmedAt: { $exists: true } }
       }
     }
-    db.users.find(query, projection, callback)
+    db.users.find(query, projection).toArray(callback)
   },
 
   getUsersByV1Ids(v1Ids, projection, callback) {
@@ -112,7 +112,7 @@ const UserGetter = {
       projection = {}
     }
     const query = { 'overleaf.id': { $in: v1Ids } }
-    db.users.find(query, projection, callback)
+    db.users.find(query, projection).toArray(callback)
   },
 
   getUsersByHostname(hostname, projection, callback) {
@@ -125,13 +125,13 @@ const UserGetter = {
       emails: { $exists: true },
       'emails.reversedHostname': reversedHostname
     }
-    db.users.find(query, projection, callback)
+    db.users.find(query, projection).toArray(callback)
   },
 
   getUsers(query, projection, callback) {
     try {
       query = normalizeQuery(query)
-      db.users.find(query, projection, callback)
+      db.users.find(query, projection).toArray(callback)
     } catch (err) {
       callback(err)
     }

+ 12 - 11
services/web/app/src/Features/User/UserOnboardingController.js

@@ -11,15 +11,17 @@ module.exports = {
 
     // find all the users with no onboardingEmailSentAt and
     // have signed up in the last 7 days
-    db.users.find(
-      {
-        onboardingEmailSentAt: null,
-        _id: {
-          $gt: ObjectId.createFromTime(Date.now() / 1000 - 7 * 24 * 60 * 60)
-        }
-      },
-      { email: 1 },
-      function(error, users) {
+    db.users
+      .find(
+        {
+          onboardingEmailSentAt: null,
+          _id: {
+            $gt: ObjectId.createFromTime(Date.now() / 1000 - 7 * 24 * 60 * 60)
+          }
+        },
+        { email: 1 }
+      )
+      .toArray(function(error, users) {
         if (error) {
           return next(error)
         }
@@ -34,8 +36,7 @@ module.exports = {
           logger.log('DONE SENDING ONBOARDING EMAILS')
           res.send(ids)
         })
-      }
-    )
+      })
   }
 }
 

+ 30 - 26
services/web/test/unit/src/Project/ProjectGetterTests.js

@@ -163,9 +163,9 @@ describe('ProjectGetter', function() {
   describe('getProject', function() {
     beforeEach(function() {
       this.project = { _id: (this.project_id = '56d46b0a1d3422b87c5ebcb1') }
-      return (this.db.projects.find = sinon
+      return (this.db.projects.findOne = sinon
         .stub()
-        .callsArgWith(2, null, [this.project]))
+        .callsArgWith(2, null, this.project))
     })
 
     describe('without projection', function() {
@@ -174,9 +174,11 @@ describe('ProjectGetter', function() {
           return this.ProjectGetter.getProject(this.project_id, this.callback)
         })
 
-        it('should call find with the project id', function() {
-          expect(this.db.projects.find.callCount).to.equal(1)
-          return expect(this.db.projects.find.lastCall.args[0]).to.deep.equal({
+        it('should call findOne with the project id', function() {
+          expect(this.db.projects.findOne.callCount).to.equal(1)
+          return expect(
+            this.db.projects.findOne.lastCall.args[0]
+          ).to.deep.equal({
             _id: ObjectId(this.project_id)
           })
         })
@@ -188,7 +190,7 @@ describe('ProjectGetter', function() {
         })
 
         it('should callback with error', function() {
-          expect(this.db.projects.find.callCount).to.equal(0)
+          expect(this.db.projects.findOne.callCount).to.equal(0)
           return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
         })
       })
@@ -208,14 +210,14 @@ describe('ProjectGetter', function() {
           )
         })
 
-        it('should call find with the project id', function() {
-          expect(this.db.projects.find.callCount).to.equal(1)
-          expect(this.db.projects.find.lastCall.args[0]).to.deep.equal({
+        it('should call findOne with the project id', function() {
+          expect(this.db.projects.findOne.callCount).to.equal(1)
+          expect(this.db.projects.findOne.lastCall.args[0]).to.deep.equal({
             _id: ObjectId(this.project_id)
           })
-          return expect(this.db.projects.find.lastCall.args[1]).to.deep.equal(
-            this.projection
-          )
+          return expect(
+            this.db.projects.findOne.lastCall.args[1]
+          ).to.deep.equal(this.projection)
         })
       })
 
@@ -225,7 +227,7 @@ describe('ProjectGetter', function() {
         })
 
         it('should callback with error', function() {
-          expect(this.db.projects.find.callCount).to.equal(0)
+          expect(this.db.projects.findOne.callCount).to.equal(0)
           return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
         })
       })
@@ -235,9 +237,9 @@ describe('ProjectGetter', function() {
   describe('getProjectWithoutLock', function() {
     beforeEach(function() {
       this.project = { _id: (this.project_id = '56d46b0a1d3422b87c5ebcb1') }
-      return (this.db.projects.find = sinon
+      return (this.db.projects.findOne = sinon
         .stub()
-        .callsArgWith(2, null, [this.project]))
+        .callsArgWith(2, null, this.project))
     })
 
     describe('without projection', function() {
@@ -249,9 +251,11 @@ describe('ProjectGetter', function() {
           )
         })
 
-        it('should call find with the project id', function() {
-          expect(this.db.projects.find.callCount).to.equal(1)
-          return expect(this.db.projects.find.lastCall.args[0]).to.deep.equal({
+        it('should call findOne with the project id', function() {
+          expect(this.db.projects.findOne.callCount).to.equal(1)
+          return expect(
+            this.db.projects.findOne.lastCall.args[0]
+          ).to.deep.equal({
             _id: ObjectId(this.project_id)
           })
         })
@@ -263,7 +267,7 @@ describe('ProjectGetter', function() {
         })
 
         it('should callback with error', function() {
-          expect(this.db.projects.find.callCount).to.equal(0)
+          expect(this.db.projects.findOne.callCount).to.equal(0)
           return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
         })
       })
@@ -283,14 +287,14 @@ describe('ProjectGetter', function() {
           )
         })
 
-        it('should call find with the project id', function() {
-          expect(this.db.projects.find.callCount).to.equal(1)
-          expect(this.db.projects.find.lastCall.args[0]).to.deep.equal({
+        it('should call findOne with the project id', function() {
+          expect(this.db.projects.findOne.callCount).to.equal(1)
+          expect(this.db.projects.findOne.lastCall.args[0]).to.deep.equal({
             _id: ObjectId(this.project_id)
           })
-          return expect(this.db.projects.find.lastCall.args[1]).to.deep.equal(
-            this.projection
-          )
+          return expect(
+            this.db.projects.findOne.lastCall.args[1]
+          ).to.deep.equal(this.projection)
         })
       })
 
@@ -300,7 +304,7 @@ describe('ProjectGetter', function() {
         })
 
         it('should callback with error', function() {
-          expect(this.db.projects.find.callCount).to.equal(0)
+          expect(this.db.projects.findOne.callCount).to.equal(0)
           return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
         })
       })

+ 2 - 1
services/web/test/unit/src/User/UserGetterTests.js

@@ -26,7 +26,8 @@ describe('UserGetter', function() {
       ]
     }
     this.findOne = sinon.stub().callsArgWith(2, null, this.fakeUser)
-    this.find = sinon.stub().callsArgWith(2, null, [this.fakeUser])
+    this.findToArrayStub = sinon.stub().yields(null, [this.fakeUser])
+    this.find = sinon.stub().returns({ toArray: this.findToArrayStub })
     this.Mongo = {
       db: {
         users: {

+ 7 - 1
services/web/test/unit/src/User/UserOnboardingControllerTests.js

@@ -22,7 +22,13 @@ describe('UserOnboardingController', function() {
     ]
 
     this.mongojs = {
-      db: { users: { find: sinon.stub().callsArgWith(2, null, this.users) } },
+      db: {
+        users: {
+          find: sinon
+            .stub()
+            .returns({ toArray: sinon.stub().yields(null, this.users) })
+        }
+      },
       ObjectId: ObjectId
     }