Created
March 30, 2017 05:42
-
-
Save avtaniket/7428aca2495999e58edb43a71ed0fb14 to your computer and use it in GitHub Desktop.
NodeJs MongoDB connection using mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @package [db] - Application db connection & schema loading | |
* @author [anikett] smartData Inc | |
*/ | |
'use strict'; | |
/* DB */ | |
var mongoose = require('mongoose'); | |
mongoose.Promise = Promise; | |
require('../api/models/Component'); | |
require('../api/models/User'); | |
require('../api/models/Category'); | |
/* database uri */ | |
var uri = 'mongodb://' + process.env.DB_HOST + ':' + process.env.DB_PORT + '/' + process.env.DB_NAME; | |
var options = { | |
user: process.env.DB_USERNAME, | |
pass: process.env.DB_PASSWORD, | |
server: { | |
socketOptions: { | |
keepAlive: 1, | |
connectTimeoutMS: 30000 | |
} | |
}, | |
replset: { | |
socketOptions: { | |
keepAlive: 1, | |
connectTimeoutMS: 30000 | |
} | |
} | |
} | |
mongoose.connect(uri, options); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.once('open', function(callback) { | |
console.log('Database connection successful!'); | |
}); | |
/* end DB */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment