lints buildQuery and paginate

This commit is contained in:
James
2019-12-23 15:36:09 -05:00
parent 8f0426fbc8
commit b9019677db
2 changed files with 47 additions and 40 deletions

View File

@@ -1,7 +1,9 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-param-reassign */
/* eslint-disable no-use-before-define */
export default function buildQueryPlugin(schema) {
schema.statics.apiQuery = function (rawParams, locale, cb) {
function apiQuery(rawParams, locale, cb) {
const model = this;
const params = paramParser(this, rawParams, locale);
@@ -12,7 +14,9 @@ export default function buildQueryPlugin(schema) {
}
return params.searchParams;
};
}
schema.statics.apiQuery = apiQuery;
}
export function paramParser(model, rawParams, locale) {
@@ -22,18 +26,18 @@ export function paramParser(model, rawParams, locale) {
};
// Construct searchParams
for (const key in rawParams) {
Object.keys(rawParams).forEach((key) => {
const separatedParams = rawParams[key]
.match(/{\w+}(.[^{}]*)/g);
if (separatedParams === null) {
query = parseParam(key, rawParams[key], model, query, locale);
} else {
for (let i = 0; i < separatedParams.length; ++i) {
query = parseParam(key, separatedParams[i], model, query, locale);
}
separatedParams.forEach((param) => {
query = parseParam(key, param, model, query, locale);
});
}
}
});
return query;
}
@@ -48,9 +52,9 @@ function convertToBoolean(str) {
function addSearchParam(query, key, value) {
if (typeof query.searchParams[key] !== 'undefined') {
for (const i in value) {
value.forEach((i) => {
query.searchParams[key][i] = value[i];
}
});
} else {
query.searchParams[key] = value;
}
@@ -62,7 +66,7 @@ function parseParam(key, val, model, query, locale) {
let operator = val.match(/\{(.*)\}/);
val = val.replace(/\{(.*)\}/, '');
if (operator) operator = operator[1];
if (operator) [, operator] = operator;
if (val === '') {
return {};
@@ -84,6 +88,8 @@ function parseParam(key, val, model, query, locale) {
} else query.searchParams._id = { $ne: val };
} else if (lcKey === 'locale') {
// Do nothing
} else if (lcKey === 'depth') {
query.maxDepth = val;
} else {
query = parseSchemaForKey(model.schema, query, '', lcKey, val, operator, locale);
}
@@ -149,7 +155,7 @@ function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator, local
newParam[`$${operator}`] = val;
query = addSearchParam(query, key, newParam);
} else {
query = addSearchParam(query, key, parseInt(val));
query = addSearchParam(query, key, parseInt(val, 0));
}
}
} else if (paramType === 'String') {

View File

@@ -1,3 +1,6 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-prototype-builtins */
/* eslint-disable no-param-reassign */
import { createAutopopulateOptions } from './createAutopopulateOptions';
/**
@@ -24,34 +27,34 @@ function paginatePlugin(dbQuery, options, callback) {
options = Object.assign({}, paginatePlugin.options, options);
options.customLabels = options.customLabels ? options.customLabels : {};
let defaultLimit = 10;
const defaultLimit = 10;
let select = options.select;
let sort = options.sort;
let collation = options.collation || {};
let populate = options.populate;
let lean = options.lean || false;
let leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true;
let limit = options.hasOwnProperty('limit') ? parseInt(options.limit) : defaultLimit;
const { select } = options;
const { sort } = options;
const collation = options.collation || {};
const { populate } = options;
const lean = options.lean || false;
const leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true;
const limit = options.hasOwnProperty('limit') ? parseInt(options.limit, 0) : defaultLimit;
let skip;
let offset;
let page;
// Custom Labels
let labelTotal = options.customLabels.totalDocs ? options.customLabels.totalDocs : 'totalDocs';
let labelLimit = options.customLabels.limit ? options.customLabels.limit : 'limit';
let labelPage = options.customLabels.page ? options.customLabels.page : 'page';
let labelTotalPages = options.customLabels.totalPages ? options.customLabels.totalPages : 'totalPages';
let labelDocs = options.customLabels.docs ? options.customLabels.docs : 'docs';
let labelNextPage = options.customLabels.nextPage ? options.customLabels.nextPage : 'nextPage';
let labelPrevPage = options.customLabels.prevPage ? options.customLabels.prevPage : 'prevPage';
let labelPagingCounter = options.customLabels.pagingCounter ? options.customLabels.pagingCounter : 'pagingCounter';
const labelTotal = options.customLabels.totalDocs ? options.customLabels.totalDocs : 'totalDocs';
const labelLimit = options.customLabels.limit ? options.customLabels.limit : 'limit';
const labelPage = options.customLabels.page ? options.customLabels.page : 'page';
const labelTotalPages = options.customLabels.totalPages ? options.customLabels.totalPages : 'totalPages';
const labelDocs = options.customLabels.docs ? options.customLabels.docs : 'docs';
const labelNextPage = options.customLabels.nextPage ? options.customLabels.nextPage : 'nextPage';
const labelPrevPage = options.customLabels.prevPage ? options.customLabels.prevPage : 'prevPage';
const labelPagingCounter = options.customLabels.pagingCounter ? options.customLabels.pagingCounter : 'pagingCounter';
if (options.hasOwnProperty('offset')) {
offset = parseInt(options.offset);
offset = parseInt(options.offset, 0);
skip = offset;
} else if (options.hasOwnProperty('page')) {
page = parseInt(options.page);
page = parseInt(options.page, 0);
skip = (page - 1) * limit;
} else {
offset = 0;
@@ -92,11 +95,10 @@ function paginatePlugin(dbQuery, options, callback) {
return Promise.all([count, docs])
.then((values) => {
let result = {
const result = {
[labelDocs]: values[1],
[labelTotal]: values[0],
[labelLimit]: limit
[labelLimit]: limit,
};
if (offset !== undefined) {
@@ -104,7 +106,6 @@ function paginatePlugin(dbQuery, options, callback) {
}
if (page !== undefined) {
const pages = Math.ceil(values[0] / limit) || 1;
result.hasPrevPage = false;
@@ -134,23 +135,23 @@ function paginatePlugin(dbQuery, options, callback) {
// Adding support for callbacks if specified.
if (typeof callback === 'function') {
return callback(null, result);
} else {
return Promise.resolve(result);
}
return Promise.resolve(result);
}).catch((reject) => {
if (typeof callback === 'function') {
return callback(reject);
} else {
return Promise.reject(reject);
}
return Promise.reject(reject);
});
}
function bindPaginate(schema) {
schema.statics.paginate = paginatePlugin;
}
/**
* @param {Schema} schema
*/
module.exports = function (schema) {
schema.statics.paginate = paginatePlugin;
};
module.exports = bindPaginate;
module.exports.paginate = paginatePlugin;