Javascript 在NodeJS应用程序中实现singleton mongDB对象
目前正在nodeJS的后端上工作,该后端使用Javascript 在NodeJS应用程序中实现singleton mongDB对象,javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,目前正在nodeJS的后端上工作,该后端使用express,socket.io,并使用MongoDB作为其数据库。我了解到,实现一个将在整个应用程序中重用的单例DB连接是一个很好的实践。我试图实现这一点,但还没有找到解决方案。我试着用 我复制了第一部分,外部mongoUtil.js文件,如下所示: var MongoClient = require( 'mongodb' ).MongoClient; var _db; module.exports = { connectToServer
express
,socket.io
,并使用MongoDB作为其数据库。我了解到,实现一个将在整个应用程序中重用的单例DB连接是一个很好的实践。我试图实现这一点,但还没有找到解决方案。我试着用
我复制了第一部分,外部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中,我像这样调用这个函数一次(我不做任何回调,这是必需的吗?)
然后,当我在应用程序的另一个模块中尝试以下操作时:
const db = mongoDB.getDb();
router.post('/', (req, res, next) => {
console.log(db);
next();
})
未定义日志
日志显示未定义
,因此我没有DB实例
问题:
为什么这不起作用?我如何解决这个问题?好的。。。所以,这可能是使用了一些过时的Mongo,但这就是我如何解决DB的单例部分的方法。免责声明:这是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;
第二。。。服务设置(采用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连接作为单例对象传递)
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;
我已经排除了关于如何在服务上设置模型的路径,因为您的问题只询问如何设置DB。但是如果您需要我,我也可以用它来扩展答案
希望这能给你一个想法。我在一个个人项目中解决了这个问题,但我不知道这是否是一个最佳实践……我的代码使用的是ecmascript 6.0标准……如果你感兴趣……我可以把它分成几部分……太多问题。尝试一些最新的教程。你所指的答案假设你对节点a有一些基本的经验很遗憾,nd已经过时了。返回的是客户端,而不是驱动程序v3之后的db。是的,我会通知您,谢谢!
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;