feathers-solr-node
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -1,6 +0,5 @@ | ||
const queryParser = require("./utils/queryParser"); | ||
const solrClient = require("./solr-client/solr"); | ||
const errors = require("feathers-errors"); | ||
const util = require("util"); | ||
const _ = require("./utils/util"); | ||
const queryParser = require('./utils/queryParser'); | ||
const solrClient = require('./solr-client/solr'); | ||
const errors = require('feathers-errors'); | ||
const _ = require('./utils/util'); | ||
@@ -26,7 +25,7 @@ class Service { | ||
this.solrConfig = { | ||
host: options.host || "172.0.0.1", | ||
port: options.port || "8983", | ||
path: options.path || "/solr", | ||
adminPath: options.adminPath || "/solr", | ||
core: options.core || "gettingstarted", | ||
host: options.host || '172.0.0.1', | ||
port: options.port || '8983', | ||
path: options.path || '/solr', | ||
adminPath: options.adminPath || '/solr', | ||
core: options.core || 'gettingstarted', | ||
get_max_request_entity_size: 1 | ||
@@ -44,9 +43,10 @@ }; | ||
createClient(config) { | ||
if (config) { | ||
solrClient.createClient(config); | ||
} | ||
getClient() { | ||
return this.solrClient; | ||
} | ||
createClient(config) { | ||
return solrClient.createClient(config); | ||
} | ||
find(params) { | ||
@@ -73,7 +73,7 @@ let query = this.queryParser.parseQuery(params.query); | ||
} | ||
let data = _.get(res, "response.docs") ; | ||
let data = _.get(res, 'response.docs'); | ||
if(data && data.length){ | ||
let row = data[0]; | ||
if(this.formatDataRow){ | ||
row = this.buildDataRow(data[0], this.formatDataRow) | ||
row = this.buildDataRow(data[0], this.formatDataRow); | ||
} | ||
@@ -113,4 +113,5 @@ return resolve(row); | ||
remove(id, params) { | ||
let query = this.queryParser.parseDelete(id,params.query); | ||
return new Promise((resolve, reject) => { | ||
this.solrClient.delete(this.primaryKey,id, (err, res) => { | ||
this.solrClient.update(query, (err, res) => { | ||
if (err) { | ||
@@ -120,3 +121,3 @@ return reject(new errors.BadRequest(err.message)); | ||
if (res.responseHeader.status === 0) { | ||
resolve(data); | ||
resolve(res); | ||
} else { | ||
@@ -134,3 +135,2 @@ return reject(new errors.BadRequest(res)); | ||
var field = formatDataRow[key]; | ||
//console.log('buildDataRow :', key); | ||
if (field instanceof Array) { | ||
@@ -147,3 +147,3 @@ if (item[field[0]]) { | ||
} | ||
if (value !== "" && value != undefined) { | ||
if (value !== '' && value != undefined) { | ||
result[key] = value; | ||
@@ -157,7 +157,7 @@ } | ||
let response = { | ||
total: _.get(res, "response.numFound") || 0, | ||
total: _.get(res, 'response.numFound') || 0, | ||
limit: parseInt(query.limit), | ||
skip: parseInt(query.offset) | ||
}; | ||
const data = _.get(res, "response.docs") || []; | ||
const data = _.get(res, 'response.docs') || []; | ||
if (this.formatDataRow) { | ||
@@ -172,2 +172,3 @@ let dataRows = []; | ||
} | ||
if (res.facets) { | ||
@@ -180,3 +181,3 @@ response.facet = res.facets; | ||
} | ||
if (res.nextCursorMark) { | ||
@@ -183,0 +184,0 @@ response.nextCursorMark = res.nextCursorMark; |
@@ -1,17 +0,16 @@ | ||
var _ = require("./util"); | ||
const _ = require('./util'); | ||
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 +21,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'); | ||
} | ||
@@ -47,9 +46,4 @@ | ||
queryObj[this.mapKey[item]] = value; | ||
} else if (item === "$or") { | ||
let value = this[item](item, query[item]); | ||
queryObj.filter.push(value); | ||
} | ||
if (item[0] === "$") { | ||
} else { | ||
let $filter = this.filter(item, query[item]); | ||
let $filter = this.filterBuilder(item, query[item]); | ||
queryObj.filter.push(...$filter); | ||
@@ -61,2 +55,12 @@ } | ||
filterBuilder(item, param){ | ||
if (item === '$or') { | ||
let value = this[item](item, param); | ||
return [value]; | ||
} | ||
if (item[0] !== '$') { | ||
return this.filter(item, param); | ||
} | ||
} | ||
filter(field, param) { | ||
@@ -66,3 +70,3 @@ let $filter = []; | ||
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]); | ||
@@ -74,7 +78,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); | ||
} | ||
@@ -84,26 +88,26 @@ return $filter; | ||
$search(field, param, queryObj) { | ||
$search(field, param) { | ||
return param; | ||
} | ||
$limit(field, param, queryObj) { | ||
$limit(field, param) { | ||
return parseInt(param) > this.rowsMax ? | ||
this.rowsMax : param ; | ||
this.rowsMax : param; | ||
} | ||
$skip(field, param, queryObj) { | ||
$skip(field, param) { | ||
return param; | ||
} | ||
$sort(field, param, queryObj) { | ||
$sort(field, param) { | ||
let order = []; | ||
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(','); | ||
} | ||
$select(field, param, queryObj) { | ||
$select(field, param) { | ||
if (Array.isArray(param)) { | ||
param = param.join(","); | ||
param = param.join(','); | ||
} | ||
@@ -113,3 +117,3 @@ return param; | ||
$in(field, param, queryObj) { | ||
$in(field, param) { | ||
if (Array.isArray(param)) { | ||
@@ -121,10 +125,10 @@ param = param.join('" OR "'); | ||
$nin(field, param, queryObj) { | ||
$nin(field, param) { | ||
if (Array.isArray(param)) { | ||
param = param.join('" OR "'); | ||
} | ||
return "!" + field + ':("' + param + '")'; | ||
return '!' + field + ':("' + param + '")'; | ||
} | ||
$between(field, param, queryObj) { | ||
$between(field, param) { | ||
if (Array.isArray(param)) { | ||
@@ -136,16 +140,16 @@ param = param.join('" TO "'); | ||
$lt(field, param, queryObj) { | ||
return field + ":[* TO " + param + "}"; | ||
$lt(field, param) { | ||
return field + ':[* TO ' + param + '}'; | ||
} | ||
$lte(field, param, queryObj) { | ||
return field + ":[* TO " + param + "]"; | ||
$lte(field, 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 *]'; | ||
} | ||
@@ -157,3 +161,3 @@ | ||
} | ||
return "!" + field + ':"' + param + '"'; | ||
return '!' + field + ':"' + param + '"'; | ||
} | ||
@@ -167,3 +171,3 @@ | ||
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]); | ||
@@ -177,3 +181,3 @@ $filter.push(condition); | ||
Object.keys(param).forEach((item, index) => { | ||
if (item[0] === "$" && typeof Query[item] !== "undefined") { | ||
if (item[0] === '$' && typeof Query[item] !== 'undefined') { | ||
let condition = this[item](item, param[item]); | ||
@@ -188,3 +192,3 @@ $filter.push(condition); | ||
if ($filter.length > 0) { | ||
return "(" + $filter.join(" OR ") + ")"; | ||
return '(' + $filter.join(' OR ') + ')'; | ||
} | ||
@@ -208,4 +212,20 @@ return null; | ||
} | ||
parseDelete(id, 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) => { | ||
let filter = this.filterBuilder(field, params[field]); | ||
query.push(...filter); | ||
}); | ||
return { delete: { query: query.join(' AND ') } }; | ||
} | ||
return { delete: { query: '*:*' } }; | ||
} | ||
} | ||
module.exports = QueryParser; | ||
module.exports = QueryParser; |
const util = {}; | ||
util.has = function(obj, key) { | ||
return key.split(".").every(function(x) { | ||
if (typeof obj !== "object" || obj === null || !(x in obj)) { | ||
return key.split('.').every(function(x) { | ||
if (typeof obj !== 'object' || obj === null || !(x in obj)) { | ||
return false; | ||
@@ -14,4 +14,4 @@ } | ||
util.get = function get(obj, key) { | ||
return key.split(".").reduce(function(o, x) { | ||
return typeof o === "undefined" || o === null ? o : o[x]; | ||
return key.split('.').reduce(function(o, x) { | ||
return typeof o === 'undefined' || o === null ? o : o[x]; | ||
}, obj); | ||
@@ -21,5 +21,5 @@ }; | ||
util.isObject = function(item) { | ||
return item && typeof item === "object" && !Array.isArray(item); | ||
return item && typeof item === 'object' && !Array.isArray(item); | ||
}; | ||
module.exports = util; |
@@ -10,3 +10,3 @@ { | ||
"description": "Solr Adapter for Feathersjs. Base on Solr-client, so can also used as a Solr-client", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"repository": { | ||
@@ -22,2 +22,3 @@ "type": "git", | ||
], | ||
"license": "MIT", | ||
"requires": true, | ||
@@ -24,0 +25,0 @@ "lockfileVersion": 1, |
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
106557
30
19
3079