Skip to content

Latest commit

 

History

History
78 lines (58 loc) · 2.62 KB

README.md

File metadata and controls

78 lines (58 loc) · 2.62 KB

Mono

MongoDB module for Mono

npm version Travis Coverage license

Installation

npm install --save mono-mongodb

Then, in your configuration file of your Mono application (example: conf/application.js):

module.exports = {
  mono: {
    modules: ['mono-mongodb']
  }
}

Configuration

Mono-MongoDB will use the mongodb property of your configuration (example: conf/development.js):

module.exports = {
  mono: {
    mongodb: {
      // url is required
      url: 'mongodb://localhost:27017',
      dbName: 'my-db',
      // Drop database at launch (default: false)
      dropDatabase: true,
      // Used in utils find
      findLimitDefault: 20, // default value
      findLimitMax: 100, // default value
      // options property for node mongodb driver
      options: {
        // See http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
      }
    }
  }
}

You can set mongodb.dropDatabase: true to drop the database when connected (useful for tests).

Usage

In your modules files, you can access db instance like this:

const { db, oid } = require('mono-mongodb')

const collection = db.collection('users')

collection.findOne({ _id: oid('554ab...' }))

Utils

const { oid, findValidation, getFindOptions, FindStream } = require('mono-mongodb')
  • oid(id: string): ObjectID
  • findValidation: Object: Joi object used for route validation inside Mono
  • getFindOptions(req.query): Object: Method to transform req.query into a compatible object for MongoDB find
  • new FindStream({ total, limit, offset, res?, key = 'items' }): TransformStream: Used for streaming MongoDB find cursor back to the server response

The last 3 methods are useful to create easily listing routes with pagination, sorting and fields restriction, best used in combination with mongodb-utils find().

You can see an example of how to use it in test/fixtures/utils/src/utils.routes.js.