在多个NPM包之间共享一个Mongoose实例 [英] Sharing a Mongoose instance between multiple NPM packages

查看:288
本文介绍了在多个NPM包之间共享一个Mongoose实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将大型现有的Node + Express + Mongoose
应用程序模块化成多个可安装的应用程序,每个应用程序都以
独立的NPM包开发,我们想知道是否共享一个
我们有一套NPM软件包,每个包含客户端资产,
Mongoose模型和一个REST -API通过Express实现。他们确实分享了
的一些共同特征,但基本上被认为是单独的
可重用的人工制品。主机应用程序,也是基于Express的,在各种根URI下安装这些

  var discussion = require('讨论'),
票= require('ticket'),
events = require('events'),
express = require('express'),
app = express ();

var environment = {... see below ...};

...

app.use('/ events-api',events(environment));
app.use('/ ticket-api',门票(环境));
app.use('/ discussion-api',discussion(environment));

现在,由于事件讨论应用程序(单独的NPM包
通过主机拉入 package.json )使用Mongoose,和
主机应用程序本身一样,我们认为我们将通过某种环境 c $ c>对象,还包括主机想要与挂载的应用程序共享的其他
东西。



您是否看到这种方法有任何明显的缺陷?在这种情况下,已安装的应用程序
在其
中分别指定Mongoose作为依赖关系 package.json ,而他们将 require('mongoose')
正常完成,而是从主机
获取Mongoose实例,负责将其连接到MongoDB。



如果这是一个坏主意,并且您建议每个子应用程序自己声明一个依赖关系
对于Mongoose,每个NPM包将获得自己的
副本的Mongoose,并且每个都必须连接到MongoDB,对吗?



一些背景信息:




  • 我们真的希望将应用程序包含在主机应用程序中,在单个进程中运行
    ,而不是具有多个Node实例。
    主机包含用于身份验证和其他内容的中间件。

  • 我们希望将应用程序作为独立开发的包含
    的NPM软件包,作为各种主机应用程序的版本依赖我们构建了
    ,而不仅仅是将其源代码复制到主机应用程序。

  • 我们意识到,在多个
    加载的应用程序之间重用相同的Mongoose实例将使它们共享相同的模型命名空间。



编辑: c> npm安装 ed:

  host / 
assets /
models /
routes /
node_modules /
express / ...
mongoose / ...
events /
assets / ...
型号/ ...
路线/ ...
门票/
资产/ ...
型号/ ...
路线/ ...
讨论/
资产/ ...
模型/ ...
路线/ ...
事件
票据

code>和讨论应用程序不包括自己的
Mongoose(或Express),但旨在依赖于始终存在的
提供这些依赖关系的主机应用程序。



我们假设这样一个NPM软件包,如不能简单地$

解决方案

如果您想要在其他NPM软件包之间重用Mongoose软件包,最好的方法是在顶级应用程序中安装共享软件包,然后使用它来初始化其他NPM软件包。



在顶层:

  var db = require('myMongooseDb'),
events = require('events')(db),
...

然后你的事件包只需要导出一个将db作为参数的函数。


In an attempt to modularize a large existing Node+Express+Mongoose application into multiple mountable apps, each developed as a separate NPM package, we're wondering whether sharing a single Mongoose instance between them is a good idea?

Let's say we have a suite of NPM packages each containing client-side assets, Mongoose models, and a REST-API implemented with Express. They do share a few common traits but are essentially to be considered separate reusable artefacts. A host application, also Express-based, mounts these under various root URIs:

var discussions = require('discussions'),
    tickets     = require('tickets'),
    events      = require('events'),
    express     = require('express'),
    app         = express();

var environment = { ...see below... };

...

app.use('/events-api', events(environment));
app.use('/tickets-api', tickets(environment));
app.use('/discussions-api', discussions(environment));

Now, since the events, tickets and discussions apps (separate NPM packages pulled in via the host package.json) use Mongoose, as do the host application itself, we figured we would pass in the host Mongoose instance through some kind of environment object that also include other stuff that the host wants to share with the mounted apps.

Do you see any obvious flaws with this approach? The mounted apps in this case would not specify Mongoose as a dependency in their respective package.json, and they would not require('mongoose') as normally done but instead get the Mongoose instance from the host which is responsible for connecting it to MongoDB.

If this is a bad idea and you suggest each sub-app declare a dependency towards Mongoose on their own, each NPM package would get their own copy of Mongoose and would each have to connect to MongoDB, right?

Some background info:

  • We really do want to include the apps in a host application, running in a single process, rather that having multiple Node instances. The host contains middleware for authentication and other things.
  • We do want to have the apps as separately developed NPM packages included as versioned dependencies of the various host applications that we build, rather than just copying their source to a host application.
  • We realize that reusing the same Mongoose instance between multiple mounted apps will have them share the same model namespace.

Edit: To clarify the package structure after all has been npm installed:

host/
  assets/
  models/
  routes/
  node_modules/
    express/ ...
    mongoose/ ...
    events/
      assets/ ...
      models/ ...
      routes/ ...
    tickets/
      assets/ ...
      models/ ...
      routes/ ...
    discussions/
      assets/ ...
      models/ ...
      routes/ ...

That is, the events, tickets, and discussions apps do not include Mongoose (or Express) of their own but are designed to rely on an always-present host application that suppliesd those dependencies.

We're assuming here that an NPM package like tickets cannot simply require stuff from the parent, right?

解决方案

If you want to reuse your Mongoose package between other NPM packages, the best way to do it is to install the shared package at the top level app and then use it to initialize the other NPM packages.

In the top level:

var db = require('myMongooseDb'),
    events = require('events')(db),
    ...

Then your events package just needs to export a function that takes the db as a parameter.

这篇关于在多个NPM包之间共享一个Mongoose实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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