Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-query-params

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-query-params - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

lib/mongo.js

77

lib/params.js
'use strict';
var utils = require('./utils');
var moment = require('moment');

@@ -8,10 +9,4 @@

var parseDate = function(date) {
var dto = moment(date, options.dateFormat).format();
return options.format === 'sql' ? "DATE('" + dto + "')" : dto;
};
var options = {
parseNum: parseFloat,
parseDate: parseDate,
dateFormat: moment.ISO_8601,

@@ -22,72 +17,10 @@ typeCast: true,

var typeCast = function(val) {
if (!options.typeCast) return val;
if (val === String(options.parseNum(val))) return options.parseNum(val);
if (moment(val, options.dateFormat, true).isValid()) return options.parseDate(val);
return val;
};
options.parseDate = utils.parseDate(options);
Parsers.mongodb = function(query) {
var parsed = {};
Parsers.mongodb = require('./mongo');
Parsers.sql = require('./sql');
for (var p in query) {
if (query[p].indexOf('*') > -1) {
parsed[p] = new RegExp(query[p].split('*').join(''));
} else if (query[p].indexOf('...') > -1) {
var range = query[p].split('...');
parsed[p] = { $gte: typeCast(range[0]), $lte: typeCast(range[1]) };
} else if (query[p].indexOf('>') > -1) {
var equal = query[p].indexOf('>=') > -1;
var operator = equal ? '$gte' : '$gt';
parsed[p] = {};
parsed[p][operator] = typeCast(query[p].split(equal ? '>=' : '>').join(''));
} else if (query[p].indexOf('<') > -1) {
var equal = query[p].indexOf('<=') > -1;
var operator = equal ? '$lte' : '$lt';
parsed[p] = {};
parsed[p][operator] = typeCast(query[p].split(equal ? '<=' : '<').join(''));
} else if (query[p] === 'true' || query[p] === 'false') {
parsed[p] = query[p] === 'true' ? true : false;
} else if (query[p][0] === '^') {
parsed[p] = new RegExp(query[p] + '$', 'i');
} else {
parsed[p] = query[p];
}
}
return parsed;
};
Parsers.sql = function(query) {
var parsed = '';
for (var p in query) {
parsed += (parsed === '') ? '' : ' AND ';
if (query[p].indexOf('*') > -1) {
parsed += p + ' LIKE `%' + query[p].split('*').join('') + '%`';
} else if (query[p].indexOf('...') > -1) {
var range = query[p].split('...');
parsed += p + ' >= ' + typeCast(range[0]) + ' AND ' + p + ' <= ' + typeCast(range[1]);
} else if (query[p].indexOf('>') > -1) {
var equal = query[p].indexOf('>=') > -1;
var operator = equal ? '>=' : '>';
parsed += p + ' ' + operator + ' ' + typeCast(query[p].split(operator).join(''));
} else if (query[p].indexOf('<') > -1) {
var equal = query[p].indexOf('<=') > -1;
var operator = equal ? '<=' : '<';
parsed += p + ' ' + operator + ' ' + typeCast(query[p].split(operator).join(''));
} else if (query[p][0] === '^') {
parsed += p + ' ILIKE `' + query[p].slice(1) + '`';
} else {
parsed += p + ' = `' + query[p] + '`';
}
}
return parsed;
};
var middleware = function(req, res, next) {
if (!req.query || req.query === {}) return next();
req.parsedQuery = Parsers[options.format](req.query);
req.parsedQuery = Parsers[options.format](req.query, options);
return next();

@@ -94,0 +27,0 @@ };

{
"name": "express-query-params",
"version": "0.3.0",
"version": "0.4.0",
"description": "Express.js middleware implementing the API Query Spec, converting the params to SQL or a Mongo query",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc