feathers-solr-node
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -1,17 +0,17 @@ | ||
const _ = require('./util'); | ||
const errors = require('@feathersjs/errors'); | ||
const _ = require("./util"); | ||
const errors = require("@feathersjs/errors"); | ||
class QueryParser { | ||
constructor(options) { | ||
this.mapKey = { | ||
$select: 'fields', | ||
$search: 'query', | ||
$limit: 'limit', | ||
$skip: 'offset', | ||
$sort: 'sort', | ||
$facet: 'facet', | ||
$params: 'params' | ||
$select: "fields", | ||
$search: "query", | ||
$limit: "limit", | ||
$skip: "offset", | ||
$sort: "sort", | ||
$facet: "facet", | ||
$params: "params" | ||
//$qf: "qf" | ||
//'$or':'filter' | ||
}; | ||
this.METHODS = ['$or']; | ||
this.METHODS = ["$or"]; | ||
this.setDefaultQuery(options); | ||
@@ -22,13 +22,13 @@ } | ||
this.defaultQuery = { | ||
query: '*:*', // TODO: score | ||
query: "*:*", // TODO: score | ||
//filter: [], | ||
// sort: '', | ||
fields: _.get(options, 'query.$select') || '*', | ||
fields: _.get(options, "query.$select") || "*", | ||
limit: | ||
_.get(options, 'paginate.default') || | ||
_.get(options, 'paginate.max') || | ||
_.get(options, "paginate.default") || | ||
_.get(options, "paginate.max") || | ||
10, | ||
offset: 0 | ||
}; | ||
this.rowsMax = _.get(options, 'paginate.max'); | ||
this.rowsMax = _.get(options, "paginate.max"); | ||
} | ||
@@ -43,3 +43,4 @@ | ||
queryObj.filter = []; | ||
Object.keys(query).every((item) => { | ||
let keys = Object.keys(query); | ||
for (let item of keys) { | ||
if (this.mapKey[item]) { | ||
@@ -50,20 +51,18 @@ let value = this[item](item, query[item]); | ||
let $filter = this.filterBuilder(item, query[item]); | ||
if($filter) { | ||
if ($filter) { | ||
queryObj.filter.push(...$filter); | ||
} else { | ||
return new errors.BadRequest(`undefined field ${item}`); | ||
} | ||
else { | ||
queryObj = new errors.BadRequest(`undefined field ${item}`); | ||
return false; | ||
} | ||
} | ||
}); | ||
} | ||
return queryObj; | ||
} | ||
filterBuilder(item, param){ | ||
if (item === '$or') { | ||
filterBuilder(item, param) { | ||
if (item === "$or") { | ||
let value = this[item](item, param); | ||
return [value]; | ||
} | ||
if (item[0] === '$') { | ||
if (item[0] === "$") { | ||
return false; | ||
@@ -78,3 +77,3 @@ } | ||
Object.keys(param).forEach(f => { | ||
if (f[0] === '$' && typeof this[f] !== 'undefined') { | ||
if (f[0] === "$" && typeof this[f] !== "undefined") { | ||
let condition = this[f](field, param[f]); | ||
@@ -86,7 +85,7 @@ $filter.push(condition); | ||
if (Array.isArray(param)) { | ||
param = '(' + param.join(' OR ') + ')'; | ||
param = "(" + param.join(" OR ") + ")"; | ||
} | ||
$filter.push(field + ':' + param); | ||
$filter.push(field + ":" + param); | ||
} else { | ||
$filter.push(field + ':' + param); | ||
$filter.push(field + ":" + param); | ||
} | ||
@@ -101,4 +100,3 @@ return $filter; | ||
$limit(field, param) { | ||
return parseInt(param) > this.rowsMax ? | ||
this.rowsMax : param; | ||
return parseInt(param) > this.rowsMax ? this.rowsMax : param; | ||
} | ||
@@ -113,5 +111,5 @@ | ||
Object.keys(param).forEach(name => { | ||
order.push(name + (parseInt(param[name]) === 1 ? ' asc' : ' desc')); | ||
order.push(name + (parseInt(param[name]) === 1 ? " asc" : " desc")); | ||
}); | ||
return order.join(','); | ||
return order.join(","); | ||
} | ||
@@ -121,3 +119,3 @@ | ||
if (Array.isArray(param)) { | ||
param = param.join(','); | ||
param = param.join(","); | ||
} | ||
@@ -138,3 +136,3 @@ return param; | ||
} | ||
return '!' + field + ':("' + param + '")'; | ||
return "!" + field + ':("' + param + '")'; | ||
} | ||
@@ -150,15 +148,15 @@ | ||
$lt(field, param) { | ||
return field + ':[* TO ' + param + '}'; | ||
return field + ":[* TO " + param + "}"; | ||
} | ||
$lte(field, param) { | ||
return field + ':[* TO ' + param + ']'; | ||
return field + ":[* TO " + param + "]"; | ||
} | ||
$gt(field, param) { | ||
return field + ':{' + param + ' TO *]'; | ||
return field + ":{" + param + " TO *]"; | ||
} | ||
$gte(field, param) { | ||
return field + ':[' + param + ' TO *]'; | ||
return field + ":[" + param + " TO *]"; | ||
} | ||
@@ -170,3 +168,3 @@ | ||
} | ||
return '!' + field + ':"' + param + '"'; | ||
return "!" + field + ':"' + param + '"'; | ||
} | ||
@@ -178,5 +176,5 @@ | ||
if (Array.isArray(param)) { | ||
param.forEach((item) => { | ||
param.forEach(item => { | ||
var f = Object.keys(item)[0]; | ||
if (f[0] === '$' && typeof this[f] !== 'undefined') { | ||
if (f[0] === "$" && typeof this[f] !== "undefined") { | ||
let condition = this[f](f, item[f]); | ||
@@ -189,4 +187,4 @@ $filter.push(condition); | ||
} else { | ||
Object.keys(param).forEach((item) => { | ||
if (item[0] === '$' && typeof param[item] !== 'undefined') { | ||
Object.keys(param).forEach(item => { | ||
if (item[0] === "$" && typeof param[item] !== "undefined") { | ||
let condition = this[item](item, param[item]); | ||
@@ -201,3 +199,3 @@ $filter.push(condition); | ||
if ($filter.length > 0) { | ||
return '(' + $filter.join(' OR ') + ')'; | ||
return "(" + $filter.join(" OR ") + ")"; | ||
} | ||
@@ -223,18 +221,18 @@ return null; | ||
parseDelete(id, params) { | ||
if(id === '*' || id === '*:*'){ | ||
return { delete: { query: '*:*' } }; | ||
}else if(id){ | ||
return { delete: {id: id} }; | ||
}else if (_.isObject(params)) { | ||
if (id === "*" || id === "*:*") { | ||
return { delete: { query: "*:*" } }; | ||
} else if (id) { | ||
return { delete: { id: id } }; | ||
} else if (_.isObject(params)) { | ||
let query = []; | ||
Object.keys(params).forEach((field) => { | ||
Object.keys(params).forEach(field => { | ||
let filter = this.filterBuilder(field, params[field]); | ||
query.push(...filter); | ||
}); | ||
return { delete: { query: query.join(' AND ') } }; | ||
return { delete: { query: query.join(" AND ") } }; | ||
} | ||
return { delete: { query: '*:*' } }; | ||
return { delete: { query: "*:*" } }; | ||
} | ||
} | ||
module.exports = QueryParser; | ||
module.exports = QueryParser; |
@@ -10,3 +10,3 @@ { | ||
"description": "Solr Adapter for Feathersjs. Base on Solr-client, so can also used as a Solr-client", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"repository": { | ||
@@ -13,0 +13,0 @@ "type": "git", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
106725
19
3071