javascript - 在 Node JS 应用程序中实现单例 mongoDB DB 对象

标签 javascript node.js mongodb express

目前正在使用 express 的 NodeJS 后端进行工作, socket.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();
})

Logs undefined

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

问题:

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

最佳答案

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

拳头,数据库连接: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;

第二...服务设置(我的层采用数据库连接)

服务> 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 模型。BodySectionService.js

class BodySectionService extends BaseService {

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

然后数据库初始化(并将数据库连接作为单例对象传递)

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;

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

希望这能提供一个想法。

关于javascript - 在 Node JS 应用程序中实现单例 mongoDB DB 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50238708/

相关文章:

javascript - 什么是双箭头函数?

javascript - 如何从另一个可重用组件和容器组件访问可重用组件

javascript - 使用 pg-promise 进行多行插入

php - Unix_Timestamp 到 mongo

javascript - 如何在同一个大括号 ({{ }}) 内动态渲染 javascript 数据和 html?

javascript - 使用 DatePicker(仅月日,无年份)、Moment.js 和 JSON 来填充事件或报价

mongodb - 如何使用Golang和MongoDriver解决 "command find requires authentication"

javascript - 如何使用 Mongoose 从集合中删除所有文档?

node.js - 如何将 angular 4 添加到现有的 node.js 应用程序

node.js - Node.js 中的 eBay API 调用返回 'Input transfer has been terminated because your request timed out'