Wrangle up some Mongoose and plugins
Mongoose Wrangler takes the guesswork out of managing mongoose-powered connections to MongoDB. Mongoose provides auto-reconnect and other niceties, but there is still some setup involved, especially with multiple connections. Wrangler attempts to automate as much as possible, including loading of mongoose model definition files for each connection.
Mongoose Wrangler can also optionally register commonly-used mongoose plugins such as mongoose-datatable and gridfs-stream. Other plugins are easily added, so send in those PRs.
$ npm install mongoose-wrangler
MongooseWrangler = require 'mongoose-wrangler'
# initialize with options
mw = new MongooseWrangler
debug: true # default: false
address: "192.168.0.2" # default: "127.0.0.1"
db: "mydb" # default: "test"
datatable: true # default: false
gridfs: true # default: false
modelPath: "./db-models" # default: "./models"
additional: [ # optional array of additional connections
address: "127.0.0.1"
db: "otherDb"
modelPath: "./other" # models to load specifically for additional connection
]
# manually disconnect
mw.disconnect()
Depending on the project setup, using mongoose = require('mongoose')
may point to a different instance of mongoose. To access the mongoose instance used by mongoose-wrangler:
mongoose = require('mongoose-wrangler').mongoose
Mongoose Wrangler handles multiple-connections with a few extra steps:
- Model files should export a function
model(connection)
which Mongoose Wrangler can call with the additional connection after it has been set up. i.e.:
exports.model = (connection) ->
connection.model "user", schema
- Recall the model for an additional connection using the connection's index in the
additional
array:
MongooseWrangler.additional[0].model("my-model")
Take advantage of Node.js streaming with GridFS and Express.js. Requested files are streamed to client a chunk at a time instead of loading completely into memory.
mw = require('mongoose-wrangler')
gridfs = mw.gridfs
# or for one of the "additional" connections:
# gridfs = mw.Grid(mw.additional[0].db, mw.mongoose.mongo)
exports.getFile = (req, res) ->
options =
filename: req.params.filename
# first check if file exists because createReadStream on non-existing file bombs hard
gridfs.exist options, (err, found) ->
handleError(res, err) if err
if found
# set up a read stream and pipe to express response for efficiency
readstream = gridfs.createReadStream options
readstream.pipe res
else
res.send 404
Using the debug: true
option will print out a little more information such as each model as it is loaded.
$ npm test