db_manager.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Created by Tony on 2017/3/9.
  3. */
  4. var mg = require('mongoose');
  5. //var Promise = require('bluebird');
  6. mg.Promise = require('bluebird');
  7. //mg.connect('mongodb://localhost/Demo');
  8. module.exports = {
  9. getConnection : function(server, port, dbName) {
  10. //*/
  11. var dbURL = 'mongodb://' + server + ":" + port + '/' + dbName;
  12. return mg.createConnection(dbURL);
  13. /*/
  14. if (port) {
  15. mg.connect('mongodb://' + server + ":" + port + '/' + dbName);
  16. } else {
  17. mg.connect('mongodb://' + server + '/' + dbName);
  18. }
  19. return mg;
  20. //*/
  21. },
  22. getQAConnection: function(dbName) {
  23. //*/
  24. return mg.createConnection("mongodb://192.168.1.184:60666/" + dbName);
  25. /*/
  26. mg.connect('mongodb://192.168.1.184:60666/' + dbName);
  27. return mg;
  28. //*/
  29. },
  30. getLocalConnection: function(dbName) {
  31. //*/
  32. return mg.createConnection("mongodb://localhost/" + dbName);
  33. /*/
  34. mg.connect('mongodb://localhost:27017/' + dbName);
  35. return mg;
  36. //*/
  37. },
  38. getCfgConnection: function(dbName) {
  39. var config = require("../config.js");
  40. //*/
  41. var port = config.current.port;
  42. var dbURL = 'mongodb://' + config.current.server + ":" + config.current.port + '/' + dbName;
  43. return mg.createConnection(dbURL);
  44. /*/
  45. mg.connect('mongodb://' + config.current.server + ":" + config.current.port + '/' + dbName);
  46. return mg;
  47. //*/
  48. },
  49. connect:function () {
  50. var config = require("../config.js");
  51. var dbURL = 'mongodb://' + config.current.server + ":" + config.current.port + '/scConstruct';
  52. var db = mg.connect(dbURL, config.options, function(err) {
  53. if (err) {
  54. console.log('Could not connect to MongoDB!');
  55. console.log(err);
  56. }
  57. });
  58. mg.connection.on('error', function(err) {
  59. console.log('MongoDB connection error:', err);
  60. process.exit(-1);
  61. });
  62. }
  63. };