不同的数据库使用每个请求节点js mongodb
我有一个应用程序谁需要支持来自不同领域的请求。 所有域的代码需要是相同但不同的数据库。 我有一个Mongodb客户端的巫师给模型的连接。 有没有办法让这个类内的请求的域名,而不是从任何调用发送到该函数(我有很多不同的服务调用)
请查看getDb函数的注释以了解我的需求
谢谢
const MongoClient = require('mongodb') const assert = require('assert'); let MongoInstance = null; class Mongo { constructor(connectionString = null, connectionOptions = {}) { if (MongoInstance === null) { MongoInstance = this; } this.connectionString = connectionString; this.connectionOptions = connectionOptions; this._db = {}; return MongoInstance; } buildConnectionString() { if (this.connectionString !== null) { return; } this.connectionString = 'mongodb://'; this.connectionString += process.env.MONGODB_USERNAME ? `${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@` : ''; this.connectionString += process.env.MONGODB_HOST; this.connectionString += process.env.MONGODB_PORT ? process.env.MONGODB_PORT : ""; } connectToServer() { return new Promise((resolve, reject) => { this.buildConnectionString(); MongoClient.connect(this.connectionString, this.connectionOptions, (err, db) => { if (err) { return reject(err); } process.env.MONGODB_LIST.split(",").forEach(dbName =>{ this._db[dbName] = db.db(dbName); }) resolve(true) }); }) } getDb() { // how to discover the dbName without sending it to the function. (maybe a middleware)? return this._db[dbName]; } closeConnection() { this._db.close(); } } module.exports = Mongo;
- 我不能从Node控件和mongodb中使用get方法从html控件中获取数据
- Mongoose不能在socket.io调用中保存()
- Express JS路由器中间件 – 身体parsing器 – req.body有连字符
- 需要使用Nodejs示例的RESTful MongoDB
- Node.js中的MongoDb监听器
- 不确定如何解释MongoDB node.js的驱动程序callback参数对于collection.insert
- 通过Mongoose在MongoDB中存储另一个文档中embedded的文档的副本
- mongoose – 保存数组string
- 如何在mongoose中填充对象的embedded数组