22/**
33 * Support for type checking and intellisense in vscode:
44 * @typedef {import("./solr").SolrConfig } SolrConfig
5+ * @typedef {import("./solr").ConfigRequest } ConfigRequest
56 * @typedef {import("./solr").SolrData } SolrData
67 * @typedef {import("./solr").SolrDocument } SolrDocument
78 * @typedef {import("./solr").SolrResponse } SolrResponse
8- * @typedef {import("./solr").DeleteQuery } DeleteQuery
9+ * @typedef {import("./solr").DeleteRequest } DeleteRequest
910 * @typedef {import("./solr").FieldProperties } FieldProperties
1011 * @typedef {import("./solr").FieldTypeProperties } FieldTypeProperties
11- * @typedef {import("./solr").SolrQuery } SolrQuery
12+ * @typedef {import("./solr").QueryRequest } QueryRequest
1213 */
1314
1415const url = require ( "url" )
1516const { default : axios } = require ( "axios" )
17+ const { mergeConfig, ensureArray, isEmptyObject } = require ( "./utils" )
1618
1719/** @type {SolrConfig } */
1820const defaultConfig = {
@@ -30,12 +32,6 @@ const defaultConfig = {
3032 apiPrefix : "api/cores"
3133}
3234
33- /** @type {(x:object) => object|object[] } */
34- const ensureArray = x => ( Object ( x ) instanceof Array ? x : [ x ] )
35-
36- const isEmptyObject = obj =>
37- Object . keys ( obj ) . length === 0 && obj . constructor === Object
38-
3935/** @type {(config:SolrConfig) => (path:string) => (data:SolrData) => Promise<SolrResponse> } */
4036const solrPost = config => path => async data => {
4137 const { core, apiPrefix } = config
@@ -45,9 +41,9 @@ const solrPost = config => path => async data => {
4541 } )
4642
4743 if ( config . debug ) {
48- const dataPart = isEmptyObject ( data ) ? "" : ` -d '${ JSON . stringify ( data ) } '`
44+ const dataPart = isEmptyObject ( data ) ? "" : `-d '${ JSON . stringify ( data ) } '`
4945 console . debug (
50- `\n$ curl -X POST '${ solrUrl } ' -H 'Content-Type: application/json'${ dataPart } \n`
46+ `\ncurl -X POST '${ solrUrl } ' -H 'Content-Type: application/json' ${ dataPart } \n`
5147 )
5248 }
5349
@@ -58,28 +54,6 @@ const solrPost = config => path => async data => {
5854 return response . data
5955}
6056
61- /**
62- * Perform an operation on solr schema.
63- * @see https://lucene.apache.org/solr/guide/7_5/schema-api.html#modify-the-schema
64- * @param {SolrConfig } config
65- */
66- const solrSchema = config => op => data =>
67- solrPost ( config ) ( "schema" ) ( { [ op ] : data } )
68-
69- const mergeConfig = ( a , b ) => mergeConfigImpure ( { ...a } , b )
70-
71- function mergeConfigImpure ( target , source ) {
72- for ( let k in source ) {
73- const objOrScalar = target [ k ]
74- if ( objOrScalar != null && objOrScalar . constructor === Object ) {
75- mergeConfigImpure ( objOrScalar , source [ k ] ) // recurse on objects
76- } else {
77- target [ k ] = source [ k ] // assign scalar value
78- }
79- }
80- return target
81- }
82-
8357/** @param {SolrConfig } userConfig */
8458const prepareSolrClient = ( userConfig = { } ) => {
8559 const config = mergeConfig ( defaultConfig , userConfig )
@@ -88,10 +62,19 @@ const prepareSolrClient = (userConfig = {}) => {
8862 if ( ! config . core ) {
8963 throw Error ( "missing 'core' parameter in your config" )
9064 }
65+
66+ // some functions require /solr prefix instead of /api/cores/
67+ const configWithSolrPrefix = { ...config , apiPrefix : "solr" }
68+
69+ const solrConfigRequest = solrPost ( configWithSolrPrefix ) ( "config" )
70+
71+ const solrSchemaRequest = op => data =>
72+ solrPost ( config ) ( "schema" ) ( { [ op ] : data } )
73+
9174 // now creating the API
9275 return {
9376 ping : ( ) =>
94- solrPost ( { ... config , apiPrefix : "solr" } ) ( "admin/ping" ) ( { } )
77+ solrPost ( configWithSolrPrefix ) ( "admin/ping" ) ( { } )
9578 . then ( value => {
9679 return value . status === "OK"
9780 } )
@@ -100,26 +83,39 @@ const prepareSolrClient = (userConfig = {}) => {
10083 /** @param {SolrDocument | SolrDocument[] } data */
10184 add : data => solrPost ( config ) ( "update" ) ( ensureArray ( data ) ) ,
10285
103- /** @param {DeleteQuery } deleteQuery */
86+ /** @param {DeleteRequest } deleteQuery */
10487 delete : deleteQuery =>
105- solrPost ( { ... config , apiPrefix : "solr" } ) ( "update" ) ( {
88+ solrPost ( configWithSolrPrefix ) ( "update" ) ( {
10689 delete : deleteQuery
10790 } ) ,
10891
10992 /** @type {(data:FieldProperties) => Promise<SolrResponse> } */
110- addField : solrSchema ( config ) ( "add-field" ) ,
93+ addField : solrSchemaRequest ( "add-field" ) ,
11194
11295 /** @type {(data:FieldTypeProperties) => Promise<SolrResponse> } */
113- addFieldType : solrSchema ( config ) ( "add-field-type" ) ,
96+ addFieldType : solrSchemaRequest ( "add-field-type" ) ,
11497
11598 /** @type {({name:string}) => Promise<SolrResponse> } */
116- deleteField : solrSchema ( config ) ( "delete-field" ) ,
99+ deleteField : solrSchemaRequest ( "delete-field" ) ,
117100
118101 /** @type {({name:string}) => Promise<SolrResponse> } */
119- deleteFieldType : solrSchema ( config ) ( "delete-field-type" ) ,
120-
121- /** @type {(data:SolrQuery) => Promise<SolrResponse> } */
122- query : solrPost ( config ) ( "query" )
102+ deleteFieldType : solrSchemaRequest ( "delete-field-type" ) ,
103+
104+ /** @type {(data:QueryRequest) => Promise<SolrResponse> } */
105+ query : solrPost ( config ) ( "query" ) ,
106+
107+ config : solrConfigRequest ,
108+
109+ /**
110+ * Convenience function to set the `update.autoCreateFields` user property.
111+ * @param {boolean } enable
112+ */
113+ configAutoEnableFields : enable =>
114+ solrConfigRequest ( {
115+ "set-user-property" : {
116+ "update.autoCreateFields" : enable ? "true" : "false"
117+ }
118+ } )
123119 }
124120}
125121
0 commit comments