/**
 * Created by Tony on 2017/3/9.
 */
var mg = require('mongoose');
//var Promise = require('bluebird');
mg.Promise = require('bluebird');
//mg.connect('mongodb://localhost/Demo');
module.exports = {
    getConnection : function(server, port, dbName) {
        //*/
        var dbURL = 'mongodb://' + server + ":" + port + '/' + dbName;
        return mg.createConnection(dbURL);
        /*/
         if (port) {
         mg.connect('mongodb://' + server + ":" + port + '/' + dbName);
         } else {
         mg.connect('mongodb://' + server + '/' + dbName);
         }
         return mg;
         //*/
    },
    getQAConnection: function(dbName) {
        //*/
        return mg.createConnection("mongodb://192.168.1.184:60666/" + dbName);
        /*/
         mg.connect('mongodb://192.168.1.184:60666/' + dbName);
         return mg;
         //*/
    },
    getLocalConnection: function(dbName) {
        //*/
        return mg.createConnection("mongodb://localhost/" + dbName);
        /*/
         mg.connect('mongodb://localhost:27017/' + dbName);
         return mg;
         //*/
    },
    getCfgConnection: function(dbName) {
        var config = require("../config.js");
        //*/
        var port = config.current.port;
        var dbURL = 'mongodb://' + config.current.server + ":" + config.current.port + '/' + dbName;
        return mg.createConnection(dbURL);
        /*/
         mg.connect('mongodb://' + config.current.server + ":" + config.current.port + '/' + dbName);
         return mg;
         //*/
    },
    connect:function (env="local") {
        var config = require("../config.js");
        var dbURL = 'mongodb://' + config[env].server + ":" + config[env].port + '/scConstruct';
        if(config[env].dbURL){
            mg.connect(config[env].dbURL,{connectTimeoutMS: 100000,useMongoClient: true});
        } else if(config[env].options){
            mg.connect(dbURL,config[env].options);
        }else {
            mg.connect(dbURL,{connectTimeoutMS: 100000,useMongoClient: true});//useMongoClient': true*! //报 DeprecationWarning: `open()` is deprecated in mongoose这个错
        }
        var db = mg.connection;
        db.on("error",function (err) {
            console.log('Could not connect to MongoDB!');
            console.log(err);
            process.exit(-1);
        })
        return mg;
    }
};