Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
MongooseFiltering class
Browse files Browse the repository at this point in the history
Cleaned up the implementation of the MongooseFiltering service class.

Previously, the service definition was inside an IIFE which returned the
function object representing the class. This way of doing it ins't
recommended.

This new implementation properly sets the prototype of the function
object used to declare the service.
  • Loading branch information
mleanos committed Sep 12, 2016
1 parent 56787c2 commit d344bdc
Showing 1 changed file with 23 additions and 23 deletions.
@@ -1,32 +1,32 @@
// Service for managing Filtering/Parameterized-Queries.

var service = (function () {
function service(query, options) {
this._query = query;
this._modifiedQuery = null;
this._totalDocumentCount = null;
this._page = options.page ? Math.max(0, options.page - 1) : null;
this._take = options.take && options.take >= 0 ? options.take : null;
this._filters = Array.isArray(options.filters) ? options.filters : [];
this._sorting = options.sorting && options.sorting.length ? options.sorting : null;

if (this._take > 300) {
// Provide a default take.
// This should probably be in the env configs.
this._take = 50;
}
// Service for managing filtering, sorting, and pagination of
// Mongoose queries.
function MongooseFiltering(query, options) {
options = options || {};

this._query = query || null;
this._modifiedQuery = null;
this._totalDocumentCount = null;
this._page = options.page ? Math.max(0, options.page - 1) : null;
this._take = options.take && options.take >= 0 ? options.take : null;
this._filters = Array.isArray(options.filters) ? options.filters : [];
this._sorting = options.sorting && options.sorting.length ? options.sorting : null;

if (this._take > 300) {
// Provide a default take.
// This should probably be in the env configs.
this._take = 50;
}
}

service.prototype.pageSortFilter = pageSortFilter;

return service;
}());
MongooseFiltering.prototype.pageSortFilter = pageSortFilter;

// Perform Pagination, Sorting, and Filtering on the current
// class instance. Uses the Mongoose query stored in the class
// instance's _query property.
function pageSortFilter(performCountQuery) {
var self = this;

return new Promise(function (resolve, reject) {

// Add sorting to the query if specified
self._modifiedQuery = sort(self._query, self._sorting);

Expand Down Expand Up @@ -100,4 +100,4 @@ function filter(query, filters) {
return query;
}

module.exports = service;
module.exports = MongooseFiltering;

0 comments on commit d344bdc

Please sign in to comment.