db.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. import mongoose from 'mongoose';
  3. import config from 'config-lite';
  4. mongoose.connect(config.url);
  5. mongoose.Promise = global.Promise;
  6. const db = mongoose.connection;
  7. db.once('open' ,() => {
  8. console.log('连接数据成功')
  9. })
  10. export default db;
  11. // var mongoose = require('mongoose');
  12. // var util = require("util");
  13. // function MongooseKeeper() {
  14. // this.db = mongoose.createConnection();
  15. // this.open_count = 0;
  16. // }
  17. // MongooseKeeper.prototype.config = function(conf) {
  18. // // body...
  19. // var options = {
  20. // db: { native_parser: true },
  21. // server: {
  22. // poolSize:4
  23. // }
  24. // };
  25. // var constr = "";
  26. // if(process.env.MONGO_DB_STR){
  27. // constr = process.env.MONGO_DB_STR ;
  28. // }
  29. // else{
  30. // //'mongodb://user:pass@localhost:port/database'
  31. // constr = util.format('mongodb://%s:%s@%s:%d/%s', conf.userid,conf.password,conf.host,conf.port,conf.database);
  32. // }
  33. // this.dbUri = constr;
  34. // this.options = options;
  35. // }
  36. // MongooseKeeper.prototype.open =function() {
  37. // this.open_count++;
  38. // if(this.open_count ==1 && this.db.readyState == 0)
  39. // {
  40. // this.db.open(this.dbUri,this.options,function() {
  41. // // body...
  42. // console.log("db opened");
  43. // });
  44. // }
  45. // }
  46. // MongooseKeeper.prototype.close =function() {
  47. // this.open_count--;
  48. // if(this.open_count == 0 )
  49. // {
  50. // this.db.close(function(){
  51. // console.log("db closed");
  52. // });
  53. // }
  54. // }
  55. // MongooseKeeper.prototype.use = function(action,callback) {
  56. // //OPEN
  57. // var self = this;
  58. // self.open();
  59. // action.call(null,function() {
  60. // //CLOSE
  61. // console.log("正在访问的数据库请求量"+self.open_count);
  62. // self.close();
  63. // callback.apply(null, arguments);
  64. // //DONE
  65. // self =null;
  66. // })
  67. // };
  68. // exports = module.exports = new MongooseKeeper();