在NodeJS应用程序中实现singleton mongDB DB对象

时间:2018-05-08 16:54:19

标签: javascript node.js mongodb express

目前在nodeJS中处理后端,该后端使用expresssocket.io,并将MongoDB作为其数据库。我读到,实现单个数据库连接是一个很好的做法,该连接将在整个应用程序中重用。我试图实现这个还没有找到解决方案。我尝试使用this SO answer of go-oleg

我复制了第一部分,外部mongoUtil.js文件,如下所示:

var MongoClient = require( 'mongodb' ).MongoClient;

var _db;

module.exports = {

  connectToServer: function( callback ) {
    MongoClient.connect( "mongodb://localhost:27017/marankings", function( err, db ) {
      _db = db;
      return callback( err );
    } );
  },

  getDb: function() {
    return _db;
  }
};

然后在我的server.js中我这样调用一次这个函数(我不需要回调,这是必需的吗?)。

var mongoUtil = require( 'mongoUtil' );

mongoUtil.connectToServer( function( err ) {
  // start the rest of your app here
} );

然后当我在应用程序的另一个模块中尝试以下内容时:

const db = mongoDB.getDb();

router.post('/', (req, res, next) => {
    console.log(db);
    next();
})
  

记录未定义

日志显示undefined,因此我没有数据库实例。

问题:

为什么这不起作用,如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

好的......所以,这可能是使用了一些过时的Mongo,但这就是我设法解决数据库的单例部分。免责声明:这是Mongo和Ecmascript 6.0的个人学习项目,所以不确定它是否遵循最佳实践,但它确实有效。

Fist,Db Connection:personnaDB.js

/************  Copyright ************/
/* Year: 2016
 * Author: David Espino
*/
"use strict"
// Imports
const url             = require('url'),
      connectionUrl   = process.env.CONNECTION_STRING || 'mongodb://localhost:27017/personna',
      parsedUrl       = url.parse(connectionUrl),
      Db              = require('mongodb').Db,
      Server          = require('mongodb').Server,
      Connection      = require('mongodb').Connection,
      Q               = require("q"),
      mongoose        = require("mongoose"),
      dao             = require('./personnaDao'),
      autoIncrement   = require( 'mongoose-auto-increment' );



// Symbol Keys
const _connKey = Symbol();
const _connInfoKey = Symbol();
const _monConnKey = Symbol();
const _dataModelsKey = Symbol();
const _autoIncrementKey = Symbol();

/**
 * This class represents the DB Connection
 */
class PersonnaDb {
  /**
   * Class Constructor
   * @return {[type]} [description]
   */
  constructor() {
    let mongoObject = null;
    this[_connInfoKey] = {
      host:     parsedUrl.hostname,
      port:     parseInt(parsedUrl.port, 10),
      name:     parsedUrl.pathname.substr(1),
      user:     parsedUrl.auth ? parsedUrl.auth.split(':')[0] : null,
      password: parsedUrl.auth ? parsedUrl.auth.split(':')[1] : null
    };
    this._connInstance = null;
  }

  /**
   * Opens the DB connection using regular mongo db access
   * @return {[type]} [description]
   */
  openConnection() {
    let deferred = Q.defer();
    if (this[_connKey]) {
      console.log('---> not need to create instance');
      deferred.resolve(this[_connInfoKey]);
    } else {
      let $this = this;
      const mongoObject = new Db('your-db', new Server(this[_connInfoKey].host, this[_connInfoKey].port, { auto_reconnect: true }));
      mongoObject.open(function(error, databaseConnection) {
        if (error) throw new Error(error);
        console.log('---> Succesfully CREATED connection');
        $this[_connKey] = databaseConnection;
        // Initialize auto increment
        autoIncrement.initialize(databaseConnection);
        $this[_autoIncrementKey] = autoIncrement;
        deferred.resolve($this);
      });
    }
    return deferred.promise;
  } 

  /**
   * Opens a Mongo db connection
   * @return {[type]} [description]
   */
  openMongooseConnection() {
    mongoose.connect(connectionUrl); 
    // set the identity plugin
    autoIncrement.initialize(mongoose.connection);
    this[_autoIncrementKey] = autoIncrement;

    // CONNECTION EVENTS
    // When successfully connected
    mongoose.connection.on('connected', function () {  
      console.log('Mongoose default connection open to ' + parsedUrl);
    }); 

    // If the connection throws an error
    mongoose.connection.on('error',function (err) {
      console.log('Mongoose default connection error: ' + err);
    });

    // When the connection is disconnected
    mongoose.connection.on('disconnected', function () {
      console.log('Mongoose default connection disconnected');
    });

    // If the Node process ends, close the Mongoose connection
    process.on('SIGINT', function() {
      mongoose.connection.close(function () {
        console.log('Mongoose default connection disconnected through app termination');
        process.exit(0);
      });
    });

    this[_dataModelsKey] = dao.PersonaDataModels.GetModels(this[_autoIncrementKey]);
   // require('../models/data/bodySectionModel');
  }

  dataModels() {
    return this[_dataModelsKey];
  }
}

module.exports.PersonnaDb = PersonnaDb;

第二...服务设置(我的接受dbconnection的层)

服务> include.js

const ModifierService  = require('./modifierService').ModifierService;
const BodySectionService  = require('./bodySectionService').BodySectionService;
module.exports = (dbConnection) => {
  return {
    Modifier: new ModifierService(dbConnection),
    BodySection: new BodySectionService(dbConnection),

  }
}

服务样本(它基本上是初始化mongo Model.BodySectionService.js

class BodySectionService extends BaseService {

  constructor(db) {
    super(db.dataModels().BodySection); // this is the mongo model with the schema etc
  }

然后进行DB初始化(并将DB Connection作为单例对象传递)

app.js

var express = require('express');
const PersonnaDb = require('./dao/personnaDb').PersonnaDb;
const dbConnection = new PersonnaDb();

var app = express();
// Open DB Connection
dbConnection.openMongooseConnection();
// get the services
const services = require('./services/include')(dbConnection);

// // This is the piece that I was not totally sure, making this available on the app
app.set('services', services);
app.set('dbAccess', dbConnection); 

这就是我使用它的方式:

bodySectionController.js

router.get('/', function(req, res, next) {
  const bodySectionProxy = req.app.get("services").BodySection;
  const logger = req.app.get("customLogger");
  var result = bodySectionProxy.getBodySections({}).then((result) => {
    res.json(result);
  })
  .fail((err) => {
    logger.logError(err);
    res.json(err.message);
  })
  .done();
});

module.exports = router;

我已经排除了关于如何在服务上设置模型的路径,因为您的问题仅询问如何设置数据库。但如果你需要我,我也可以用这个来扩展答案。

希望这能给出一个想法。