2014-02-04 15:30:05 -05:00
|
|
|
config = require '../../server_config'
|
|
|
|
winston = require 'winston'
|
|
|
|
mongoose = require 'mongoose'
|
|
|
|
Grid = require 'gridfs-stream'
|
|
|
|
|
|
|
|
testing = '--unittest' in process.argv
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.connect = () ->
|
2014-02-10 13:30:08 -05:00
|
|
|
address = module.exports.generateMongoConnectionString()
|
2014-02-10 16:18:39 -05:00
|
|
|
winston.info "Connecting to Mongo with connection string #{address}"
|
2014-02-10 13:30:08 -05:00
|
|
|
|
|
|
|
mongoose.connect address
|
2014-02-10 16:18:39 -05:00
|
|
|
mongoose.connection.once 'open', -> Grid.gfs = Grid(mongoose.connection.db, mongoose.mongo)
|
2014-02-10 13:30:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
module.exports.generateMongoConnectionString = ->
|
2014-05-05 20:33:47 -04:00
|
|
|
if not testing and config.mongo.mongoose_replica_string
|
2014-02-04 15:30:05 -05:00
|
|
|
address = config.mongo.mongoose_replica_string
|
|
|
|
else
|
|
|
|
dbName = config.mongo.db
|
|
|
|
dbName += '_unittest' if testing
|
|
|
|
address = config.mongo.host + ":" + config.mongo.port
|
|
|
|
if config.mongo.username and config.mongo.password
|
|
|
|
address = config.mongo.username + ":" + config.mongo.password + "@" + address
|
|
|
|
address = "mongodb://#{address}/#{dbName}"
|
2014-02-10 13:30:08 -05:00
|
|
|
|
2014-05-05 20:33:47 -04:00
|
|
|
return address
|