Skip to content

Commit

Permalink
fix(backup): mongodb binaries for mongodb 4 windows #138
Browse files Browse the repository at this point in the history
chore(database): added support for MongoDB Atlas (shards)
  • Loading branch information
polonel committed Apr 11, 2019
1 parent 2286538 commit 052d6da
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/backup/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function runBackup (callback) {
})
})
} else {
callback(new Error('MongoDump falied with code ' + code))
callback(new Error('MongoDump failed with code ' + code))
}
})
}
Expand Down
7 changes: 6 additions & 1 deletion src/controllers/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ installController.mongotest = function (req, res) {
var CONNECTION_URI =
'mongodb://' + data.username + ':' + dbPassword + '@' + data.host + ':' + data.port + '/' + data.database

if (data.port === '---')
CONNECTION_URI = 'mongodb+srv://' + data.username + ':' + dbPassword + '@' + data.host + '/' + data.database

var child = require('child_process').fork(path.join(__dirname, '../../src/install/mongotest'), {
env: { FORK: 1, NODE_ENV: global.env, MONGOTESTURI: CONNECTION_URI }
})
Expand Down Expand Up @@ -124,6 +127,7 @@ installController.install = function (req, res) {

var dbPassword = encodeURIComponent(password)
var conuri = 'mongodb://' + username + ':' + dbPassword + '@' + host + ':' + port + '/' + database
if (port === '---') conuri = 'mongodb+srv://' + username + ':' + dbPassword + '@' + host + '/' + database

async.waterfall(
[
Expand Down Expand Up @@ -334,7 +338,8 @@ installController.install = function (req, res) {
port: port,
username: username,
password: password,
database: database
database: database,
shard: port === '---'
},
tokens: {
secret: chance.hash() + chance.md5(),
Expand Down
50 changes: 34 additions & 16 deletions src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,40 @@ var mongoConnectionUri = {
port: process.env.TD_MONGODB_PORT || nconf.get('mongo:port') || '27017',
username: process.env.TD_MONGODB_USERNAME || nconf.get('mongo:username'),
password: process.env.TD_MONGODB_PASSWORD || nconf.get('mongo:password'),
database: process.env.TD_MONGODB_DATABASE || nconf.get('mongo:database')
database: process.env.TD_MONGODB_DATABASE || nconf.get('mongo:database'),
shard: process.env.TD_MONGODB_SHARD || nconf.get('mongo:shard')
}

var CONNECTION_URI = ''
if (!mongoConnectionUri.username)
if (!mongoConnectionUri.username) {
CONNECTION_URI =
'mongodb://' + mongoConnectionUri.server + ':' + mongoConnectionUri.port + '/' + mongoConnectionUri.database
else {
if (mongoConnectionUri.shard === true)
CONNECTION_URI = 'mongodb+srv://' + mongoConnectionUri.server + '/' + mongoConnectionUri.database
} else {
mongoConnectionUri.password = encodeURIComponent(mongoConnectionUri.password)
CONNECTION_URI =
'mongodb://' +
mongoConnectionUri.username +
':' +
mongoConnectionUri.password +
'@' +
mongoConnectionUri.server +
':' +
mongoConnectionUri.port +
'/' +
mongoConnectionUri.database
if (mongoConnectionUri.shard === true)
CONNECTION_URI =
'mongodb+srv://' +
mongoConnectionUri.username +
':' +
mongoConnectionUri.password +
'@' +
mongoConnectionUri.server +
'/' +
mongoConnectionUri.database
else
CONNECTION_URI =
'mongodb://' +
mongoConnectionUri.username +
':' +
mongoConnectionUri.password +
'@' +
mongoConnectionUri.server +
':' +
mongoConnectionUri.port +
'/' +
mongoConnectionUri.database
}

if (process.env.TD_MONGODB_URI) CONNECTION_URI = process.env.TD_MONGODB_URI
Expand All @@ -56,6 +70,7 @@ var options = {
module.exports.init = function (callback, connectionString, opts) {
if (connectionString) CONNECTION_URI = connectionString
if (opts) options = opts
options.dbName = mongoConnectionUri.database

if (db.connection) {
return callback(null, db)
Expand All @@ -71,8 +86,11 @@ module.exports.init = function (callback, connectionString, opts) {
}

db.connection = mongoose.connection

return callback(null, db)
mongoose.connection.db.admin().command({ buildInfo: 1 }, function (err, info) {
if (err) winston.warn(err.message)
db.version = info.version
return callback(null, db)
})
})
.catch(function (e) {
winston.error('Oh no, something went wrong with DB! - ' + e.message)
Expand Down
6 changes: 5 additions & 1 deletion src/settings/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,12 @@ function createDirectories (callback) {
function downloadWin32MongoDBTools (callback) {
var http = require('http')
var os = require('os')
var semver = require('semver')
var dbVersion = require('../database').db.version || '3.6.9'
var fileVersion = semver(dbVersion).major + '.' + semver(dbVersion).minor

if (os.platform() === 'win32') {
var filename = 'mongodb-tools.3.6.9-win32x64.zip'
var filename = 'mongodb-tools.' + fileVersion + '-win32x64.zip'
var savePath = path.join(__dirname, '../backup/bin/win32/')
fs.ensureDirSync(savePath)
if (
Expand Down

0 comments on commit 052d6da

Please sign in to comment.