es-alchemy
Advanced tools
Comparing version 11.3.1 to 11.4.0
@@ -55,4 +55,14 @@ import assert from 'assert'; | ||
list: () => Object.keys(indices).sort(), | ||
getMapping: (idx) => cloneDeep(indices[idx].mapping), | ||
getFields: (idx) => cloneDeep(indices[idx].fields), | ||
getMapping: (idx, exclude = null) => { | ||
if (exclude === null) { | ||
return cloneDeep(indices[idx].mapping); | ||
} | ||
const { name, ...specs } = indices[idx].specs; | ||
return generateMapping(name, specs, models, exclude); | ||
}, | ||
getFields: (idx, exclude = null) => ( | ||
exclude === null | ||
? cloneDeep(indices[idx].fields) | ||
: extractFields(indices[idx].specs, exclude).concat('_id') | ||
), | ||
getSpecs: (idx) => cloneDeep(indices[idx].specs) | ||
@@ -59,0 +69,0 @@ }, |
@@ -7,3 +7,3 @@ // Translate index from esalchemy syntax to ES syntax | ||
const buildPropertiesRec = (node, models) => { | ||
const buildPropertiesRec = (node, models, exclude) => { | ||
assert( | ||
@@ -14,3 +14,3 @@ node instanceof Object && Array.isArray(node) === false, | ||
assert( | ||
Object.keys(node).every((e) => ['model', 'fields', 'sources', 'nested', 'flat'].includes(e)), | ||
Object.keys(node).every((e) => ['model', 'type', 'fields', 'sources', 'nested', 'flat'].includes(e)), | ||
'Unknown specs entry provided.' | ||
@@ -36,2 +36,6 @@ ); | ||
assert( | ||
node.type === undefined || typeof node.type === 'string', | ||
'Invalid type field provided' | ||
); | ||
assert( | ||
node.fields.every((f) => typeof model.compiled.fields[typeof f === 'string' ? f : f.name] === 'function'), | ||
@@ -41,19 +45,21 @@ 'Unknown field provided.' | ||
const nested = Object.entries(node.nested || {}); | ||
return nested.reduce( | ||
(prev, [key, value]) => Object.assign(prev, { | ||
[key]: { | ||
properties: buildPropertiesRec(value, models), | ||
type: 'nested', | ||
...(get(value, 'flat', false) === true ? { include_in_root: true } : {}) | ||
} | ||
}), | ||
node.fields | ||
.reduce((prev, key) => { | ||
const isString = typeof key === 'string'; | ||
const k = isString ? key : key.name; | ||
// eslint-disable-next-line no-param-reassign | ||
prev[k] = model.compiled.fields[k](...(isString ? [] : [get(key, 'overwrite', {})])); | ||
return prev; | ||
}, {}) | ||
); | ||
return nested | ||
.filter(([_, value]) => !exclude.includes(value.type)) | ||
.reduce( | ||
(prev, [key, value]) => Object.assign(prev, { | ||
[key]: { | ||
properties: buildPropertiesRec(value, models, exclude), | ||
type: 'nested', | ||
...(get(value, 'flat', false) === true ? { include_in_root: true } : {}) | ||
} | ||
}), | ||
node.fields | ||
.reduce((prev, key) => { | ||
const isString = typeof key === 'string'; | ||
const k = isString ? key : key.name; | ||
// eslint-disable-next-line no-param-reassign | ||
prev[k] = model.compiled.fields[k](...(isString ? [] : [get(key, 'overwrite', {})])); | ||
return prev; | ||
}, {}) | ||
); | ||
}; | ||
@@ -72,3 +78,3 @@ | ||
export const generateMapping = (name, specs, models) => { | ||
export const generateMapping = (name, specs, models, exclude = []) => { | ||
assert( | ||
@@ -90,3 +96,3 @@ !get(specs, 'model', '').endsWith('[]'), | ||
flat: specs.flat | ||
}, models); | ||
}, models, exclude); | ||
const def = { | ||
@@ -118,2 +124,3 @@ dynamic: 'false', | ||
filterFn: ({ isLeaf }) => isLeaf, | ||
breakFn: ({ value, context }) => context.exclude.includes(value?.type), | ||
rtn: ({ key, value, getParents }) => { | ||
@@ -137,4 +144,4 @@ const result = []; | ||
export const extractFields = (specs) => extractFieldsScanner(specs); | ||
export const extractFields = (specs, exclude = []) => extractFieldsScanner(specs, { exclude }); | ||
export const extractRels = (spec) => extractRelsRec(spec); | ||
export const normalize = (field) => field.replace('$', '.'); |
{ | ||
"name": "es-alchemy", | ||
"type": "module", | ||
"version": "11.3.1", | ||
"version": "11.4.0", | ||
"description": "Simplification of Opensearch interactions", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -117,2 +117,9 @@ # ESAlchemy | ||
##### type | ||
Type: `string`<br> | ||
Optional. | ||
Free form string that can be used with "exclude" to filter mappings and fields | ||
##### fields | ||
@@ -384,4 +391,4 @@ | ||
- `list()` - list all indices registered with ES-Alchemy | ||
- `getMapping(name: String)` - get the mapping for Opensearch for this index | ||
- `getFields(name: String)` - get all fields (including nested) for this index | ||
- `getMapping(name: String, exclude = [])` - get the mapping for Opensearch for this index | ||
- `getFields(name: String, exclude = [])` - get all fields (including nested) for this index | ||
@@ -388,0 +395,0 @@ #### data |
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
78935
1911
452