sails-native-criteria

1.1.2 • Public • Published

Sails.js logo

sails-native-criteria

This is a sailsjs library that converts requests to a format that native mongodb understands.

Get Started

This library helps you convert your sails query to a format that native mongodb understands. Hence you can directly pass your get request directly to your native mongodb $match stage or any other aggregation or native mongodb method that accepts a query.

Installation

With node installed:

npm i sails-native-criteria --save

Usage

In your controller.

        //Controller method to sum total BOQ
        sum: function (req, res) {
            var nativeCriteria = require('sails-native-criteria');
            var criteria = nativeCriteria.getNativeCriteria(req);
            BOQ.native(function (err, collection) {
                collection.aggregate([
                    {
                        $match: criteria
                    },
                    {
                        $group: {
                            _id: null,
                            sum: { $sum: "$total" }
                        }
                    }
                ],
                    function (err, response) {
                        if (err) {
                            return res.forbidden(err);
                        }
                        if (response.length !== 1) {
                            return res.ok({ sum: 0 });
                        }
                        return res.ok({ sum: response[0].sum });
                    }
                )
            });
        }

Your url would look like this --> /bog/sum?where={"createdAt":{"<":"2017-05-26T20:46:52.459Z"}}

NB: skip, limit are also allowed.

Feature Requests

If you have an idea for a new feature, please feel free to submit it as a pull request to this repository.

Contributing

  1. Fork it gently!
  2. Create your feature branch: git checkout -b feature-name
  3. Commit your changes: git commit -am 'Some commit message'
  4. Push to the branch: git push origin feature-name
  5. Submit a pull request :D
  6. Your name shows up in credits

Credits

License

MIT License Copyright © 2016

Package Sidebar

Install

npm i sails-native-criteria

Weekly Downloads

0

Version

1.1.2

License

MIT

Last publish

Collaborators

  • toniton