@ladjs/mongoose

Mongoose helper for Lad, which is used as an alternative to the default `mongoose.connect()` for multiple connection management. See the Forward Email codebase for further insight into setup.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@ladjs/mongoose
306.0.1a year ago7 years agoMinified + gzip package size for @ladjs/mongoose in KB

Readme

build status code style styled with prettier made with lass license
Mongoose helper for Lad, which is used as an alternative to the default mongoose.connect() for multiple connection management. See the Forward Emailforward-email codebase for further insight into setup.

Table of Contents

Install

Requires mongoose v6+ to be a dependency in your project.

npm install @ladjs/mongoose mongoose
## Usage

```js
const Mongoose = require('@ladjs/mongoose');
const { Schema } = require('mongoose');

(async () => {
  const m = new Mongoose({
    mongo: {
      options: { heartbeatFrequencyMS: 100, serverSelectionTimeoutMS: 1000 }
    }
  });

  const conn1 = await m.createConnection('mongodb://server-one.example.com/database-name').asPromise();
  const conn2 = await m.createConnection('mongodb://server-two.example.com/database-name').asPromise();

  const UserSchema = new Schema({ name: String });
  const LogSchema = new Schema({ name: String });

  const Users = conn1.model('Users', UserSchema);
  const Logs = conn2.model('Logs', LogSchema);

  // write to the server-one.example.com database
  await Users.create({ name: 'test' });

  // write to the server-two.example.com database
  await Logs.create({ name: 'test' });
})();

Note that instances of this class Mongoose only have the method createConnection. It is not 1:1 with mongoose normal singleton.
You should use @ladjs/gracefullad-graceful to manage process SIGHUP and graceful exits.

Options

| Option | Type | Default | Description | | -------------------------------- | ------------------------- | ------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | logger | Object | console | Custom logger function, see cabin or axe as a default if desired. | | mongo | Object | --- | See below uri and options properties. | | mongo.uri | String | undefined | mongo connection URI
which is passed as the first argument to mongoose.createConnection
see mongo options docs | | mongo.options | Object | undefined | mongo connection options
which is passed as the second argument to mongoose.createConnection
see mongo options docs | | bindEvents | Boolean | true | Whether or not to bind default events to the Mongoose connection using provided logger. | | hideMeta | String or false Boolean | "hide_meta" | Appends a true boolean property to a property with this value in logs, e.g. console.log('mongoose disconnected', { hide_meta: true }); which is useful for preventing metadata object from being invoked as the second argument (this is meant for usage with Cabin and Axe and made for Forward Emailforward-email). If you pass a false value then this property will not get populated. | | --- | --- | --- | --- | | allowDiskUse | Boolean | undefined | set with mongoose.setmongoose.set | | applyPluginsToChildSchemas | Boolean | undefined | set with mongoose.setmongoose.set | | applyPluginsToDiscriminators | Boolean | undefined | set with mongoose.setmongoose.set | | autoCreate | Boolean | undefined | set with mongoose.setmongoose.set | | autoIndex | Boolean | undefined | set with mongoose.setmongoose.set | | bufferCommands | Boolean | undefined | set with mongoose.setmongoose.set | | bufferTimeoutMS | Boolean | undefined | set with mongoose.setmongoose.set | | debug | Boolean | If process.env.MONGOOSE_DEBUG is truthy, then it is true, otherwise false | set with mongoose.setmongoose.set | | id | Boolean | undefined | set with mongoose.setmongoose.set | | timestamps.createdAt.immutable | Boolean | undefined | set with mongoose.setmongoose.set | | maxTimeMS | Number | 10000 | set with mongoose.setmongoose.set | | objectIdGetter | Boolean | undefined | set with mongoose.setmongoose.set | | overwriteModels | Boolean | undefined | set with mongoose.setmongoose.set | | returnOriginal | Boolean | undefined | set with mongoose.setmongoose.set | | runValidators | Boolean | undefined | set with mongoose.setmongoose.set | | sanitizeFilter | Boolean | undefined | set with mongoose.setmongoose.set | | selectPopulatedPaths | Boolean | undefined | set with mongoose.setmongoose.set | | strict | Boolean | true | set with mongoose.setmongoose.set | | strictQuery | Boolean | true | set with mongoose.setmongoose.set | | toJSON | Boolean | undefined | set with mongoose.setmongoose.set | | toObject | Boolean | undefined | set with mongoose.setmongoose.set |

Contributors

| Name | Website | | ------------------ | ------------------------- | | Nick Baugh | | | Spencer Snyder | |

License

MIT © Nick Baugh
#