单个 node.js 项目中的 Mongoose 和多个数据库 [英] Mongoose and multiple database in single node.js project

查看:36
本文介绍了单个 node.js 项目中的 Mongoose 和多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个包含子项目的 Node.js 项目.一个子项目将有一个 Mongodb 数据库,Mongoose 将用于包装和查询数据库.但问题是

I'm doing a Node.js project that contains sub projects. One sub project will have one Mongodb database and Mongoose will be use for wrapping and querying db. But the problem is

  • Mongoose 不允许在单个 mongoose 实例中使用多个数据库,因为模型建立在一个连接上.
  • 为了使用多个 mongoose 实例,Node.js 不允许多个模块实例,因为它在 require() 中有缓存系统.我知道在 Node.js 中禁用模块缓存,但我认为这不是一个好的解决方案,因为它只需要 mongoose.

  • Mongoose doesn't allow to use multiple databases in single mongoose instance as the models are build on one connection.
  • To use multiple mongoose instances, Node.js doesn't allow multiple module instances as it has caching system in require(). I know disable module caching in Node.js but I think it is not the good solution as it is only need for mongoose.

我尝试在 mongoose 中使用 createConnection()openSet(),但这不是解决方案.

I've tried to use createConnection() and openSet() in mongoose, but it was not the solution.

我尝试深度复制猫鼬实例 (http://blog.imagea.com/deep-copy-in-javascript/) 将新的 mongoose 实例传递给子项目,但它抛出 RangeError: Maximum call stack size exceeded.

I've tried to deep copy the mongoose instance (http://blog.imaginea.com/deep-copy-in-javascript/) to pass new mongoose instances to the sub project, but it throwing RangeError: Maximum call stack size exceeded.

我想知道是否可以使用带有 mongoose 的多个数据库或解决此问题的任何解决方法?因为我认为猫鼬非常容易和快速.或者任何其他模块作为推荐?

I want to know is there anyways to use multiple database with mongoose or any workaround for this problem? Because I think mongoose is quite easy and fast. Or any other modules as recommendations?

推荐答案

您可以做的一件事是,您可以为每个项目设置子文件夹.因此,请在该子文件夹中安装 mongoose,并从每个子应用程序的自己的文件夹中安装 require() mongoose.不是来自项目根或全局.所以一个子项目,一个 mongoose 安装和一个 mongoose 实例.

One thing you can do is, you might have subfolders for each projects. So, install mongoose in that subfolders and require() mongoose from own folders in each sub applications. Not from the project root or from global. So one sub project, one mongoose installation and one mongoose instance.

-app_root/
--foo_app/
---db_access.js
---foo_db_connect.js
---node_modules/
----mongoose/
--bar_app/
---db_access.js
---bar_db_connect.js
---node_modules/
----mongoose/

在 foo_db_connect.js 中

In foo_db_connect.js

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/foo_db');
module.exports = exports = mongoose;

在 bar_db_connect.js 中

In bar_db_connect.js

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/bar_db');
module.exports = exports = mongoose;

在 db_access.js 文件中

In db_access.js files

var mongoose = require("./foo_db_connect.js"); // bar_db_connect.js for bar app

现在,您可以使用 mongoose 访问多个数据库.

Now, you can access multiple databases with mongoose.

这篇关于单个 node.js 项目中的 Mongoose 和多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆