mongoose-with-queries

1.0.1 • Public • Published

Mongoose With Queries

Build your own domain specific language around your models using chainable mongoose queries.

build status npm version MIT license we're hiring

Usage

npm install mongoose-with-queries
var withQueries = require('mongoose-with-queries');
 
// Define your model
var Product = mongoose.model('Product', new mongoose.Schema({
    category: String,
    quantity: Number,
    price: Number
  })
);
 
// Define your queries
withQueries(Product, {
  category: function (query, category) {
    return query.where('category').equals(category);
  },
 
  quantity: function (query, quantity) {
    return query.where('quantity').equals(quantity);
  },
 
  isFruit: function (query) {
    return query.where('category').equals('fruit');
  },
 
  isSoldOut: function (query) {
    return query.quantity(0);
  },
 
  isAvailable: function (query) {
    return query.where('quantity').gt(0);
  },
 
  isBargain: function (query) {
    // Anything under $2.00
    return query.where('price').lt(200);
  }
 
});
 
// Use your queries
 
// Bakery products available for sale
// Product.find().where('category').equals('bakery').where('quantity').gt(0).exec()
Product.query.category('bakery').isAvailable().exec()
 
 
// Bargain produce we've already sold out of.
// Product.find().where('category').equals('fruit').where('quantity').equals(0).where('price').lt(200).exec()
Product.query.isSoldOut().isFruit().isBargain().exec()

Contributing

Please follow our Code of Conduct when contributing to this project.

We currently test against a local mongodb so make sure you have mongod running.

$ git clone https://github.com/goodeggs/mongoose-with-queries && cd mongoose-with-queries
$ npm install
$ npm test

Module scaffold generated by generator-goodeggs-npm.

Package Sidebar

Install

npm i mongoose-with-queries

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • goodeggs-admin
  • adborden