Skip to content

Commit 051ecd0

Browse files
committed
feat: add mergeConfig exported function used also in prepareSolrClient
BREAKING CHANGE: the prepareSolrClient function now accepts user config which is merged with the default config (overrides its fields). The userConfig can also be empty.
1 parent c388079 commit 051ecd0

4 files changed

Lines changed: 130 additions & 114 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "node-solr-lambda",
33
"version": "1.1.0",
44
"description": "Minimalistic Solr client written in functional programming style",
5-
"main": "src/index.js",
5+
"main": "src/index",
66
"repository": "https://github.com/vsimko/node-solr-lambda.git",
77
"author": "Viliam Simko <viliam.simko@gmail.com>",
88
"license": "MIT",

src/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,24 @@ const solrPost = config => path => async data => {
6666
const solrSchema = config => op => data =>
6767
solrPost(config)("schema")({ [op]: data })
6868

69-
/** @param {SolrConfig} config */
70-
const prepareSolrClient = config => {
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+
83+
/** @param {SolrConfig} userConfig */
84+
const prepareSolrClient = (userConfig = {}) => {
85+
const config = mergeConfig(defaultConfig, userConfig)
86+
7187
// sanity checks
7288
if (!config.core) {
7389
throw Error("missing 'core' parameter in your config")
@@ -109,5 +125,6 @@ const prepareSolrClient = config => {
109125

110126
module.exports = {
111127
prepareSolrClient,
112-
defaultConfig
128+
defaultConfig,
129+
mergeConfig
113130
}

src/index.d.ts renamed to src/solr.d.ts

Lines changed: 105 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -261,126 +261,125 @@ interface FacetFunctionResult {
261261
[facetName: string]: number
262262
}
263263

264-
declare global {
265-
interface SolrData {
266-
[key: string]: SolrDataValue
267-
}
264+
export interface SolrData {
265+
[key: string]: SolrDataValue
266+
}
268267

269-
/**
270-
* Fields are defined in the fields element of `schema.xml`.
271-
* Once you have the **field types** set up, defining the fields themselves is simple.
272-
*
273-
* @see https://lucene.apache.org/solr/guide/7_5/defining-fields.html
274-
* @see https://lucene.apache.org/solr/guide/7_5/defining-fields.html#optional-field-type-override-properties
275-
*/
276-
type FieldProperties = FieldGeneralProperties & FieldTypeDefaultProperties
268+
/**
269+
* Fields are defined in the fields element of `schema.xml`.
270+
* Once you have the **field types** set up, defining the fields themselves is simple.
271+
*
272+
* @see https://lucene.apache.org/solr/guide/7_5/defining-fields.html
273+
* @see https://lucene.apache.org/solr/guide/7_5/defining-fields.html#optional-field-type-override-properties
274+
*/
275+
export type FieldProperties = FieldGeneralProperties &
276+
FieldTypeDefaultProperties
277277

278-
/**
279-
* A field type defines the analysis that will occur on a field when documents are indexed
280-
* or queries are sent to the index.
281-
* A field type definition can include four types of information:
282-
* - The `name` of the field type (**mandatory**).
283-
* - An implementation `class` name (**mandatory**).
284-
* - If the field type is `TextField`, a description of the field analysis for the field type.
285-
* - Field type properties - depending on the implementation class,
286-
* some properties **may be mandatory**.
287-
*
288-
* The field type `class` determines most of the behavior of a field type, but optional properties
289-
* can also be defined.
290-
*
291-
* The properties that can be specified for a given field type fall into three major categories:
292-
* - Properties specific to the field type’s class.
293-
* - General Properties {@link FieldTypeGeneralProperties} that Solr supports for any field type.
294-
* - Field Default Properties {@link FieldTypeDefaultProperties} that can be specified on the field
295-
* type that will be inherited by fields that use this type instead of the default behavior.
296-
*/
297-
type FieldTypeProperties = FieldTypeGeneralProperties &
298-
FieldTypeDefaultProperties
278+
/**
279+
* A field type defines the analysis that will occur on a field when documents are indexed
280+
* or queries are sent to the index.
281+
* A field type definition can include four types of information:
282+
* - The `name` of the field type (**mandatory**).
283+
* - An implementation `class` name (**mandatory**).
284+
* - If the field type is `TextField`, a description of the field analysis for the field type.
285+
* - Field type properties - depending on the implementation class,
286+
* some properties **may be mandatory**.
287+
*
288+
* The field type `class` determines most of the behavior of a field type, but optional properties
289+
* can also be defined.
290+
*
291+
* The properties that can be specified for a given field type fall into three major categories:
292+
* - Properties specific to the field type’s class.
293+
* - General Properties {@link FieldTypeGeneralProperties} that Solr supports for any field type.
294+
* - Field Default Properties {@link FieldTypeDefaultProperties} that can be specified on the field
295+
* type that will be inherited by fields that use this type instead of the default behavior.
296+
*/
297+
export type FieldTypeProperties = FieldTypeGeneralProperties &
298+
FieldTypeDefaultProperties
299299

300-
interface SolrDocument {
301-
id: string
302-
_childDocuments_?: SolrFragmentWithId | SolrFragmentWithId[]
303-
[key: string]: SolrDataValue | SolrData
304-
}
300+
export interface SolrDocument {
301+
id: string
302+
_childDocuments_?: SolrFragmentWithId | SolrFragmentWithId[]
303+
[key: string]: SolrDataValue | SolrData
304+
}
305305

306-
/**
307-
* This type definition contains just the most important parts.
308-
*/
309-
interface SolrResponse {
310-
status?: string
311-
facets?: {
312-
count?: number
313-
} & {
314-
[facetName: string]: (TermsFacet | FacetFunctionResult)[] // TODO: to be completed
315-
}
316-
response?: {
317-
docs: SolrDocument[]
318-
numFound: number
319-
start: number
320-
}
321-
responseHeader: SolrResponseHeader
306+
/**
307+
* This type definition contains just the most important parts.
308+
*/
309+
export interface SolrResponse {
310+
status?: string
311+
facets?: {
312+
count?: number
313+
} & {
314+
[facetName: string]: (TermsFacet | FacetFunctionResult)[] // TODO: to be completed
315+
}
316+
response?: {
317+
docs: SolrDocument[]
318+
numFound: number
319+
start: number
322320
}
321+
responseHeader: SolrResponseHeader
322+
}
323323

324-
/**
325-
* This type definition contains just the most important parts.
326-
*/
327-
interface SolrException {
324+
/**
325+
* This type definition contains just the most important parts.
326+
*/
327+
export interface SolrException {
328+
config
329+
message: string
330+
request: ClientRequest
331+
response: {
328332
config
329-
message: string
330-
request: ClientRequest
331-
response: {
332-
config
333-
data: {
334-
error: {
335-
code: number
336-
details: {
337-
errorMessages: string[]
338-
[key: string]: object
339-
}[]
340-
metadata: string[]
341-
}
342-
responseHeader: SolrResponseHeader
333+
data: {
334+
error: {
335+
code: number
336+
details: {
337+
errorMessages: string[]
338+
[key: string]: object
339+
}[]
340+
metadata: string[]
343341
}
344-
headers
345-
request: ClientRequest
346-
status: number
347-
statusText: string
342+
responseHeader: SolrResponseHeader
348343
}
349-
stack: string
344+
headers
345+
request: ClientRequest
346+
status: number
347+
statusText: string
350348
}
349+
stack: string
350+
}
351351

352-
interface SolrQuery {
353-
query?
354-
filter?
355-
start?
356-
limit?
357-
sort?
358-
facet?
359-
params?: {
360-
hl?: "on" | "off"
361-
"hl.simple.pre"?: string
362-
"hl.simple.post"?: string
363-
"hl.fl"?: string
364-
indent?: "off" | "on"
365-
}
352+
export interface SolrQuery {
353+
query?
354+
filter?
355+
start?
356+
limit?
357+
sort?
358+
facet?
359+
params?: {
360+
hl?: "on" | "off"
361+
"hl.simple.pre"?: string
362+
"hl.simple.post"?: string
363+
"hl.fl"?: string
364+
indent?: "off" | "on"
366365
}
366+
}
367367

368-
interface SolrConfig {
369-
urlConfig: UrlObject & {
370-
query: {
371-
overwrite: boolean
372-
commitWithin: number
373-
wt: "json" | "xml" | "python" | "ruby" | "php" | "csv"
374-
[key: string]: any
375-
}
368+
export interface SolrConfig {
369+
urlConfig?: UrlObject & {
370+
query?: {
371+
overwrite?: boolean
372+
commitWithin?: number
373+
wt?: "json" | "xml" | "python" | "ruby" | "php" | "csv"
374+
[key: string]: any
376375
}
377-
debug: boolean
378-
core?: string
379-
apiPrefix: string
380376
}
377+
debug?: boolean
378+
core?: string
379+
apiPrefix?: string
380+
}
381381

382-
interface DeleteQuery {
383-
id?: string
384-
query?: any
385-
}
382+
export interface DeleteQuery {
383+
id?: string
384+
query?: any
386385
}

test/solr.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const { expect } = require("chai")
33

44
describe("#prepareSolrClient", () => {
55
it("should be able to prepare the client", () => {
6-
const client = prepareSolrClient()
7-
expect(client).to.haveOwnProperty("ping")
8-
expect(client).to.haveOwnProperty("select")
9-
expect(client).to.haveOwnProperty("add")
6+
const solr = prepareSolrClient({ core: "dummy" })
7+
expect(solr).to.haveOwnProperty("ping")
8+
expect(solr).to.haveOwnProperty("query")
9+
expect(solr).to.haveOwnProperty("add")
1010
})
1111
})

0 commit comments

Comments
 (0)