mongodb-query-parser
Advanced tools
Comparing version
132
lib/index.js
const EJSON = require('mongodb-extended-json'); | ||
const bson = require('bson'); | ||
const debug = require('debug')('mongodb-query-parser'); | ||
const ms = require('ms'); | ||
const Context = require('context-eval'); | ||
@@ -8,7 +8,13 @@ const toJavascriptString = require('javascript-stringify'); | ||
const queryLanguage = require('mongodb-language-model'); | ||
const getMongoDBType = require('mongodb-extended-json/lib/types').type; | ||
const debug = require('debug')('mongodb-query-parser'); | ||
// const LRU = require('lru-cache'); | ||
const DEFAULT_FILTER = {}; | ||
const DEFAULT_SORT = null; | ||
const DEFAULT_LIMIT = 0; | ||
const DEFAULT_SKIP = 0; | ||
const DEFAULT_PROJECT = null; | ||
const DEFAULT_MAX_TIME_MS = ms('10 seconds'); | ||
const QUERY_PROPERTIES = ['filter', 'project', 'sort', 'skip', 'limit']; | ||
const getMongoDBType = require('mongodb-extended-json/lib/types').type; | ||
const BSON_TO_JS_STRING = { | ||
@@ -70,2 +76,17 @@ ObjectID: function(v) { | ||
function isEmpty(input) { | ||
const s = _.trim(input); | ||
if (s === '{}') { | ||
return true; | ||
} | ||
return _.isEmpty(s); | ||
} | ||
function isNumberValid(input) { | ||
if (isEmpty(input)) { | ||
return 0; | ||
} | ||
return /^\d+$/.test(input) ? parseInt(input, 10) : false; | ||
} | ||
// TODO Cache parsing. | ||
@@ -130,3 +151,8 @@ // const createLRUCache = require('lru-cache'); | ||
module.exports = function(filter = '', project = '') { | ||
module.exports = function(filter, project = DEFAULT_PROJECT) { | ||
if (arguments.length === 1) { | ||
if (_.isString(filter)) { | ||
return parseFilter(filter); | ||
} | ||
} | ||
return { | ||
@@ -139,4 +165,4 @@ filter: parseFilter(filter), | ||
module.exports.parseFilter = function(input) { | ||
if (_.trim(input) === '' || input === '{}') { | ||
return {}; | ||
if (isEmpty(input)) { | ||
return DEFAULT_FILTER; | ||
} | ||
@@ -150,5 +176,4 @@ return parseFilter(input); | ||
* | ||
* @param {String} input the input to validate. | ||
* | ||
* @return {Boolean|Object} false if not valid, otherwise the cleaned-up filter. | ||
* @param {String} input | ||
* @return {Boolean|Object} false if not valid, or the parsed filter. | ||
*/ | ||
@@ -167,5 +192,9 @@ module.exports.isFilterValid = function(input) { | ||
function isValueOkforProject(val) { | ||
return _.isNumber(val) && (val === 0 || val === 1); | ||
} | ||
module.exports.parseProject = function(input) { | ||
if (_.trim(input) === '' || input === '{}') { | ||
return null; | ||
if (isEmpty(input)) { | ||
return DEFAULT_PROJECT; | ||
} | ||
@@ -175,13 +204,11 @@ return parseProject(input); | ||
function isValueOkforProject(val) { | ||
return _.isNumber(val) && (val === 0 || val === 1); | ||
} | ||
function isValueOkforSort(val) { | ||
return _.isNumber(val) && (val === 1 || val === -1); | ||
} | ||
/** | ||
* Validation function for a query `project`. Must only have 0 or 1 as values. | ||
* | ||
* @param {String} input | ||
* @return {Boolean|Object} false if not valid, otherwise the parsed project. | ||
*/ | ||
module.exports.isProjectValid = function(input) { | ||
if (_.trim(input) === '' || input === '{}') { | ||
return null; | ||
if (isEmpty(input)) { | ||
return DEFAULT_PROJECT; | ||
} | ||
@@ -202,5 +229,9 @@ | ||
function isValueOkforSort(val) { | ||
return _.isNumber(val) && (val === 1 || val === -1); | ||
} | ||
module.exports.parseSort = function(input) { | ||
if (_.trim(input) === '' || input === '{}') { | ||
return null; | ||
if (isEmpty(input)) { | ||
return DEFAULT_SORT; | ||
} | ||
@@ -210,3 +241,13 @@ return parseSort(input); | ||
/** | ||
* validation function for a query `sort`. Must only have -1 or 1 as values. | ||
* | ||
* @param {String} input | ||
* @return {Boolean|Object} false if not valid, otherwise the cleaned-up sort. | ||
*/ | ||
module.exports.isSortValid = function(input) { | ||
if (isEmpty(input)) { | ||
return DEFAULT_SORT; | ||
} | ||
try { | ||
@@ -225,2 +266,37 @@ var parsed = parseSort(input); | ||
/** | ||
* Validation function for a query `skip`. Must be digits only. | ||
* | ||
* @param {String} input | ||
* @return {Boolean|Number} false if not valid, otherwise the cleaned-up skip. | ||
*/ | ||
module.exports.isSkipValid = function(input) { | ||
if (isEmpty(input)) { | ||
return DEFAULT_SKIP; | ||
} | ||
return isNumberValid(input); | ||
}; | ||
/** | ||
* Validation function for a query `limit`. Must be digits only. | ||
* | ||
* @param {String} input | ||
* @return {Boolean|Number} false if not valid, otherwise the cleaned-up limit. | ||
*/ | ||
module.exports.isLimitValid = function(input) { | ||
if (isEmpty(input)) { | ||
return DEFAULT_LIMIT; | ||
} | ||
return isNumberValid(input); | ||
}; | ||
module.exports.validate = function(what, input) { | ||
const validator = module.exports[`is${_.capitalize(what)}Valid`]; | ||
if (!validator) { | ||
debug('Do not know how to validate `%s`. Returning false.', what); | ||
return false; | ||
} | ||
return validator(input); | ||
}; | ||
module.exports.stringify = function(obj) { | ||
@@ -239,1 +315,9 @@ return toJavascriptString( | ||
}; | ||
module.exports.QUERY_PROPERTIES = QUERY_PROPERTIES; | ||
module.exports.DEFAULT_FILTER = DEFAULT_FILTER; | ||
module.exports.DEFAULT_SORT = DEFAULT_SORT; | ||
module.exports.DEFAULT_LIMIT = DEFAULT_LIMIT; | ||
module.exports.DEFAULT_SKIP = DEFAULT_SKIP; | ||
module.exports.DEFAULT_PROJECT = DEFAULT_PROJECT; | ||
module.exports.DEFAULT_MAX_TIME_MS = DEFAULT_MAX_TIME_MS; |
{ | ||
"name": "mongodb-query-parser", | ||
"description": "Parse MongoDB queries", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"scripts": { | ||
@@ -28,3 +28,4 @@ "fmt": "mongodb-js-fmt", | ||
"mongodb-extended-json": "^1.9.0", | ||
"mongodb-language-model": "^1.1.0" | ||
"mongodb-language-model": "^1.1.0", | ||
"ms": "^2.0.0" | ||
}, | ||
@@ -31,0 +32,0 @@ "devDependencies": { |
21279
11.6%283
38.05%10
11.11%