Comparing version 0.0.2 to 0.1.0
'use strict'; | ||
let defaultConfig = require( '../config' ).defaultOptions; | ||
const defaultConfig = require( '../config' ).defaultOptions; | ||
const methods = require( './methods' ); | ||
@@ -13,6 +14,13 @@ function Client( config ) { | ||
}; | ||
this.search = function( query, options ) { | ||
return methods._runQuery( this, query, options ); | ||
}; | ||
this.save = function( obj, options ) { | ||
return methods._saveQuery( this, obj, options ); | ||
}; | ||
}; | ||
require( './methods' )( Client ); | ||
module.exports = Client; |
'use strict'; | ||
module.exports = Client => { | ||
require( './private' )( Client ); | ||
require( './public' )( Client ); | ||
const _ = require( 'lodash' ); | ||
const request = require( 'request' ); | ||
function _assembleQ( query, option ) { | ||
let returnQ = '*'; | ||
if ( _.isString( query ) ) { | ||
returnQ = query; | ||
} else if ( _.isArray( query ) ) { | ||
let qArray = []; | ||
let delimiter = ' '; | ||
query.forEach( function ( subItem ) { | ||
qArray.push( _assembleQ( subItem ) ); | ||
} ); | ||
if ( option && option.delimiter ) { | ||
delimiter = option.delimiter; | ||
} | ||
returnQ = `(${qArray.join( ')' + delimiter + '(' )})`; | ||
} else if ( _.isObject( query ) ) { | ||
Object.keys( query ).forEach( function ( key ) { | ||
let delimiter = ' '; | ||
let qf = ''; | ||
if ( key === '$or' ) { | ||
delimiter = ' OR '; | ||
} else if ( key === '$and' ) { | ||
delimiter = ' AND '; | ||
} else { | ||
qf = `${ key }:`; | ||
} | ||
returnQ = `${ qf }${ _assembleQ( query[ key ], { delimiter } ) }`; | ||
} ); | ||
} else if ( _.isBoolean( query ) ) { | ||
returnQ = String( query ); | ||
} | ||
return returnQ; | ||
} | ||
const prepRequest = ( url, method, qs, options ) => { | ||
let requestParams = { | ||
url, method, qs, | ||
'useQuerystring' : true | ||
}; | ||
if ( options ) { | ||
if ( options.logging ) { | ||
let tempRequestParams = _.cloneDeep( requestParams ); | ||
delete tempRequestParams.useQuerystring; | ||
options.logging( tempRequestParams ); | ||
} | ||
if ( options.update ) { | ||
requestParams.json = true; | ||
requestParams.body = _.cloneDeep( qs ); | ||
delete requestParams.useQuerystring; | ||
delete requestParams.qs; | ||
} | ||
} | ||
return requestParams; | ||
}; | ||
function _assembleHostUrl( protocol, host, port ) { | ||
return `${ protocol }://${ host }:${ port }`; | ||
}; | ||
function _assemblePathUrl( rootPath, core, destination ) { | ||
return `/${ rootPath }/${ core }/${ destination || 'query' }?commit=true`; | ||
}; | ||
function _makeRequest( requestParams ) { | ||
return new Promise( ( resolve, reject ) => { | ||
request( requestParams, function ( requestError, response, body ) { | ||
if ( requestError ) { | ||
return callback( requestError ); | ||
} | ||
try { | ||
let tempObj = body; | ||
if ( !requestParams.body ) { | ||
tempObj = JSON.parse( body ); | ||
} | ||
if ( tempObj.error ) { | ||
return reject( tempObj.error ); | ||
} | ||
resolve( tempObj ); | ||
} catch ( caughtError ) { | ||
if ( body && body.indexOf( '<lst name="error">' ) > -1 ) { | ||
let tempMessage = body.split( '<lst name="error"><str name="msg">' )[ 1 ]; | ||
let tempCode = tempMessage.split( '</str><int name="code">' ); | ||
let solrError = { | ||
'message' : _.clone( tempCode[ 0 ] ), | ||
'code' : _.clone( tempCode[ 1 ].split( '</int></lst>' )[ 0 ] ) | ||
}; | ||
caughtError = { caughtError, solrError }; | ||
} | ||
reject( caughtError ); | ||
} | ||
} ); | ||
} ); | ||
}; | ||
function _runQuery( that, query, options ) { | ||
return new Promise( ( resolve, reject ) => { | ||
try { | ||
let qs = { | ||
'q' : _assembleQ( query ) | ||
}; | ||
let baseUrl = that._assembleHostUrl( that.config.protocol, that.config.host, that.config.port ); | ||
baseUrl += that._assemblePathUrl( that.config.rootPath, that.config.core ); | ||
if ( options && options.commonParams ) { | ||
if ( !_.isObject( options.commonParams ) ) { | ||
throw ( '`commonParams` must be an Object' ); | ||
} | ||
Object.keys( options.commonParams ).forEach( function ( param ) { | ||
qs[ param ] = options.commonParams[ param ]; | ||
} ); | ||
} | ||
return _makeRequest( prepRequest( baseUrl, 'GET', qs, options ) ).then( results => { | ||
resolve( results ); | ||
} ).catch( reject ); | ||
} catch( e ) { | ||
return reject( e ); | ||
} | ||
} ); | ||
}; | ||
function _saveQuery( that, obj, options ) { | ||
return new Promise( ( resolve, reject ) => { | ||
try { | ||
let cleanObj = _.clone( obj ); | ||
let baseUrl = _assembleHostUrl( that.config.protocol, that.config.host, that.config.port ); | ||
baseUrl += _assemblePathUrl( that.config.rootPath, that.config.core, 'update/json' ); | ||
return _makeRequest( prepRequest( baseUrl, 'POST', [ cleanObj ], { 'update' : true } ) ).then( results => { | ||
resolve( results ); | ||
} ).catch( reject ); | ||
} catch( e ) { | ||
return reject( e ); | ||
} | ||
} ); | ||
}; | ||
module.exports = { _saveQuery, _runQuery }; |
{ | ||
"name" : "solr-query", | ||
"version" : "0.0.2", | ||
"version" : "0.1.0", | ||
"description" : "An attempt to make solr searches a little more user-friendly", | ||
@@ -16,2 +16,7 @@ "main" : "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:mashu-daishi/solr-query.git" | ||
}, | ||
"dependencies" : { | ||
@@ -18,0 +23,0 @@ "lodash" : "4.17.4", |
@@ -7,3 +7,3 @@ # solr-query | ||
This is an in-progress project. For now, only Solr /search is supported. | ||
This is an in-progress project. For now, only Solr /search and /update is supported. | ||
@@ -22,2 +22,9 @@ ## Installing | ||
solrC.search( searchQuery, searchOptions ); | ||
newRequest.save( obj ) | ||
.then( data => { | ||
console.log( "data:", data ); | ||
} ).catch( err => { | ||
console.log( "err:", err ); | ||
} ); | ||
``` | ||
@@ -61,2 +68,13 @@ | ||
### .save( solrObj ) | ||
##### solrObj | ||
This currently accepts a json object that will be entered verbatim into Solr. | ||
```js | ||
solrObj = { | ||
"title" : "Kinda Cool", | ||
"uid" : "b5546172-713a-44f8-9105-761158ca75d9" | ||
} | ||
``` | ||
## Running the tests | ||
@@ -63,0 +81,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
11141
258
86
6
11
1