Note that there are some explanatory texts on larger screens.

plurals
  1. POSharing a Mongoose instance between multiple NPM packages
    text
    copied!<p>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?</p> <p>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:</p> <pre><code>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)); </code></pre> <p>Now, since the <code>events</code>, <code>tickets</code> and <code>discussions</code> apps (separate NPM packages pulled in via the host <code>package.json</code>) use Mongoose, as do the host application itself, we figured we would pass in the host Mongoose instance through some kind of <code>environment</code> object that also include other stuff that the host wants to share with the mounted apps.</p> <p>Do you see any obvious flaws with this approach? The mounted apps in this case would <strong>not</strong> specify Mongoose as a dependency in their respective <code>package.json</code>, and they would <strong>not</strong> <code>require('mongoose')</code> as normally done but instead get the Mongoose instance from the host which is responsible for connecting it to MongoDB.</p> <p>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?</p> <p>Some background info:</p> <ul> <li>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.</li> <li>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.</li> <li>We realize that reusing the same Mongoose instance between multiple mounted apps will have them share the same model namespace.</li> </ul> <p><strong>Edit:</strong> To clarify the package structure after all has been <code>npm install</code>ed:</p> <pre><code>host/ assets/ models/ routes/ node_modules/ express/ ... mongoose/ ... events/ assets/ ... models/ ... routes/ ... tickets/ assets/ ... models/ ... routes/ ... discussions/ assets/ ... models/ ... routes/ ... </code></pre> <p>That is, the <code>events</code>, <code>tickets</code>, and <code>discussions</code> 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.</p> <p>We're assuming here that an NPM package like <code>tickets</code> cannot simply <code>require</code> stuff from the parent, right?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload