/mongoose

Mongoose helper for Lad

Primary LanguageJavaScriptMIT LicenseMIT

@ladjs/mongoose

build status code coverage code style styled with prettier made with lass license

Mongoose helper for Lad

Table of Contents

Install

npm:

npm install @ladjs/mongoose mongoose

yarn:

yarn add @ladjs/mongoose mongoose

Requires mongoose v5 to be a dependency in your project.

Usage

This package is a mongoose connection helper that sets up some sensible default connection options for mongoose and event dubugging helpers.

The default options are outlined below.

This package also provides reconnection logic on the initial connection. This is especcially convenient as this is not baked into mongo or mongoose natively. Mongoose will, however, take over reconnection in the event there is a disconnect event.

Usage:

const Mongoose = require('@ladjs/mongoose');
const mongoose = new Mongoose({
  // ...
});
mongoose
  .connect()
  .then()
  .catch(console.error);

Now require and use the mongoose singleton in the rest of your project as you normally would.

const mongoose = require('mongoose');

Options

Option Type Default Description
mongo Object configuration object configuration object
mongo.options Object reconnectTries: Number.MAX_VALUE
reconnectInterval: 1000
useNewUrlParser: true
any valid mongoose configuration object
which is passed to mongoose.connect()
see mongoose options docs
debug Boolean false set with mongoose.set
useCreateIndex Boolean true set with mongoose.set
bufferCommands Boolean undefined set with mongoose.set
useFindAndModify Boolean undefined set with mongoose.set
useNewUrlParser Boolean undefined set with mongoose.set
cloneSchemas Boolean undefined set with mongoose.set
applyPluginsToDiscriminators Boolean undefined set with mongoose.set
applyPluginsToChildSchemas Boolean undefined set with mongoose.set
objectIdGetter Boolean undefined set with mongoose.set
runValidators Boolean undefined set with mongoose.set
toObject Boolean undefined set with mongoose.set
toJSON Boolean undefined set with mongoose.set
strict Boolean undefined set with mongoose.set
selectPopulatedPaths Boolean undefined set with mongoose.set
maxTimeMS Boolean undefined set with mongoose.set
autoIndex Boolean undefined set with mongoose.set

A note on useUnifiedTopology

In mongoose 5.7 the mongo driver version was bumped which caused some deprecation warnings when using the option useUnifiedTopology with the options recconectTries and reconnectInterval. @ladjs/mongoose uses both of these options internally for initial connection retries if needed. However, we detect version under the hood and omit all problematic options automatically from the connection if needed. We encourage users to continue setting these options even if they are using the new unified topology.

Contributors

Name Website
Nick Baugh http://niftylettuce.com

License

MIT © Nick Baugh