Allow ENV variable as MongoDB conn string

pull/73/head
NGPixel 8 years ago
parent d13085ac1b
commit 191f00cc89

@ -28,7 +28,7 @@ winston.info('[AGENT] Background Agent is initializing...')
let appconf = require(CORE_PATH + 'core-libs/config')()
global.appconfig = appconf.config
global.appdata = appconf.data
global.db = require(CORE_PATH + 'core-libs/mongodb').init()
global.db = require('./libs/db').init()
global.upl = require('./libs/uploads-agent').init()
global.git = require('./libs/git').init()
global.entries = require('./libs/entries').init()

@ -105,6 +105,7 @@ admin: admin@company.com
# ---------------------------------------------------------------------
# Database Connection String
# ---------------------------------------------------------------------
# You can also use an ENV variable by using $ENV_VAR_NAME as the value
db: mongodb://localhost:27017/wiki

@ -0,0 +1,70 @@
'use strict'
/* global ROOTPATH, appconfig, winston */
const modb = require('mongoose')
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
/**
* MongoDB module
*
* @return {Object} MongoDB wrapper instance
*/
module.exports = {
/**
* Initialize DB
*
* @return {Object} DB instance
*/
init () {
let self = this
global.Mongoose = modb
let dbModelsPath = path.resolve(ROOTPATH, 'models')
modb.Promise = require('bluebird')
// Event handlers
modb.connection.on('error', err => {
winston.error('Failed to connect to MongoDB instance.')
return err
})
modb.connection.once('open', function () {
winston.log('Connected to MongoDB instance.')
})
// Store connection handle
self.connection = modb.connection
self.ObjectId = modb.Types.ObjectId
// Load DB Models
fs
.readdirSync(dbModelsPath)
.filter(function (file) {
return (file.indexOf('.') !== 0)
})
.forEach(function (file) {
let modelName = _.upperFirst(_.camelCase(_.split(file, '.')[0]))
self[modelName] = require(path.join(dbModelsPath, file))
})
// Using ENV variable?
if (_.startsWith(appconfig.db, '$')) {
appconfig.db = process.env[appconfig.db.slice(1)]
}
// Connect
self.onReady = modb.connect(appconfig.db)
return self
}
}

@ -33,7 +33,7 @@ winston.info('[SERVER] Wiki.js is initializing...')
// ----------------------------------------
global.lcdata = require('./libs/local').init()
global.db = require(CORE_PATH + 'core-libs/mongodb').init()
global.db = require('./libs/db').init()
global.entries = require('./libs/entries').init()
global.git = require('./libs/git').init(false)
global.lang = require('i18next')

Loading…
Cancel
Save