JSPM

mongoose-populate-options

1.1.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 189
  • Score
    100M100P100Q88860F
  • License MIT

A Mongoose plugin for applying query options to every layer of population.

Package Exports

  • mongoose-populate-options

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mongoose-populate-options) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

mongoose-populate-options

Build Status

A Mongoose plugin for applying query options to every layer of population.

Getting Started

Installing

npm install mongoose-populate-options --save

Usage

Typical Case

const setPopulateOptions = require('mongoose-populate-options');

MySchema.plugin(setPopulateOptions);

MyModel.find()
  .populate('friends')
  .populate('teachers')
  .setPopulateOptions({ limit: 2 });

This will set mognoose options on the two populate calls, as if you'd defined options inline on each one.

Note: Just like setOptions, order matters. setPopulateOptions will affect all populate calls before it, but not after it. Options from setPopulateOptions will overwrite options that have alread been set.

Note: This method does not set options on the query itself.

Resticting Options

You can restrict which options are allowed to be set in the options of populate commands. This can be useful when handling API requests where any arbitrary option can be sent by the client.

MyModel.find()
  .populate('friends')
  .setPopulateOptions({ limit: 2 }, ['limit', 'skip']);

Submodule

You can also load the submodule that will give will take a populate value (e.g. friends or { path: 'friends', options: { limit: 10 } } and return an object that has the new options inserted at every level. This may be useful when manually dealing with options, like within an API.

const populateObfForElement = require('mongoose-populate-options/populateObfForElement');

const newPopulate = populateObfForElement(myOptions.populate, { limit: 5 });

Contributing

Running the tests

npm test

Style

./node_modules/.bin/eslint .

Author

Makinde Adeagbo

License

This project is licensed under the MIT License - see the LICENSE file for details