[misc] migrate to the native mongo driver

This commit is contained in:
Jakob Ackermann 2020-08-21 16:30:18 +01:00
parent 23c120b8b8
commit 882db8484c
9 changed files with 177 additions and 698 deletions

View file

@ -11,6 +11,7 @@ const Settings = require('settings-sharelatex')
const logger = require('logger-sharelatex')
const express = require('express')
const bodyParser = require('body-parser')
const mongodb = require('./app/js/mongodb')
const Errors = require('./app/js/Errors')
const HttpController = require('./app/js/HttpController')
@ -48,12 +49,21 @@ const { host } = Settings.internal.contacts
if (!module.parent) {
// Called directly
app.listen(port, host, function (error) {
if (error != null) {
throw error
}
return logger.info(`contacts starting up, listening on ${host}:${port}`)
})
mongodb
.waitForDb()
.then(() => {
app.listen(port, host, function (err) {
if (err) {
logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
process.exit(1)
}
return logger.info(`contacts starting up, listening on ${host}:${port}`)
})
})
.catch((err) => {
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
process.exit(1)
})
}
module.exports = app

View file

@ -11,7 +11,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let ContactManager
const { db, ObjectId } = require('./mongojs')
const { db, ObjectId } = require('./mongodb')
const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex')
@ -31,7 +31,7 @@ module.exports = ContactManager = {
update.$inc[`contacts.${contact_id}.n`] = 1
update.$set[`contacts.${contact_id}.ts`] = new Date()
return db.contacts.update(
db.contacts.updateOne(
{
user_id
},

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.contacts = internalDb.collection('contacts')
}
module.exports = {
db,
ObjectId,
waitForDb
}

View file

@ -1,9 +0,0 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
const Settings = require('settings-sharelatex')
const mongojs = require('mongojs')
const db = mongojs(Settings.mongo.url, ['contacts'])
module.exports = {
db,
ObjectId: mongojs.ObjectId
}

File diff suppressed because it is too large Load diff

View file

@ -24,7 +24,7 @@
"express": "^4.17.1",
"logger-sharelatex": "^2.2.0",
"metrics-sharelatex": "^2.6.2",
"mongojs": "3.1.0",
"mongodb": "^3.6.0",
"request": "~2.88.2",
"settings-sharelatex": "^1.1.0",
"underscore": "~1.9.2"

View file

@ -12,6 +12,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const app = require('../../../app')
const { waitForDb } = require('../../../app/js/mongodb')
require('logger-sharelatex').logger.level('error')
module.exports = {
@ -26,9 +27,10 @@ module.exports = {
return callback()
} else if (this.initing) {
return this.callbacks.push(callback)
} else {
this.initing = true
this.callbacks.push(callback)
}
this.initing = true
this.callbacks.push(callback)
waitForDb().then(() => {
return app.listen(3036, 'localhost', (error) => {
if (error != null) {
throw error
@ -42,6 +44,6 @@ module.exports = {
return result
})()
})
}
})
}
}

View file

@ -15,7 +15,7 @@ const sinon = require('sinon')
const chai = require('chai')
chai.should()
const { expect } = chai
const { ObjectId } = require('mongojs')
const { ObjectId } = require('mongodb')
const request = require('request')
const async = require('async')
const ContactsApp = require('./ContactsApp')

View file

@ -15,7 +15,7 @@ const should = chai.should()
const { expect } = chai
const modulePath = '../../../app/js/ContactManager.js'
const SandboxedModule = require('sandboxed-module')
const { ObjectId } = require('mongojs')
const { ObjectId } = require('mongodb')
const tk = require('timekeeper')
describe('ContactManager', function () {
@ -23,7 +23,7 @@ describe('ContactManager', function () {
tk.freeze(Date.now())
this.ContactManager = SandboxedModule.require(modulePath, {
requires: {
'./mongojs': {
'./mongodb': {
db: (this.db = { contacts: {} }),
ObjectId
},
@ -42,7 +42,7 @@ describe('ContactManager', function () {
describe('touchContact', function () {
beforeEach(function () {
return (this.db.contacts.update = sinon.stub().callsArg(3))
this.db.contacts.updateOne = sinon.stub().callsArg(3)
})
describe('with a valid user_id', function () {
@ -55,7 +55,7 @@ describe('ContactManager', function () {
})
it('should increment the contact count and timestamp', function () {
return this.db.contacts.update
this.db.contacts.updateOne
.calledWith(
{
user_id: sinon.match(