es-alchemy
Advanced tools
Comparing version 10.4.6 to 10.4.7
@@ -5,2 +5,3 @@ import assert from 'assert'; | ||
import extractPrefix from './extract-prefix.js'; | ||
import { normalize } from './index.js'; | ||
@@ -16,3 +17,6 @@ const buildRec = (filterBy, allowedFields, root) => { | ||
assert(root === null || prefix.startsWith(root), 'Can only reference relative paths in sort filters.'); | ||
return [prefix === root ? '' : prefix, actionMapFilter[filterBy[1]](filterBy[0], ...filterBy.slice(2))]; | ||
return [ | ||
prefix === root ? '' : prefix, | ||
actionMapFilter[filterBy[1]](normalize(filterBy[0]), ...filterBy.slice(2)) | ||
]; | ||
} | ||
@@ -19,0 +23,0 @@ |
@@ -5,2 +5,3 @@ // Translate index from esalchemy syntax to ES syntax | ||
import objectHash from 'object-hash'; | ||
import objectScan from 'object-scan'; | ||
@@ -57,10 +58,2 @@ const buildPropertiesRec = (node, models) => { | ||
const extractFieldsRec = (node, prefix = []) => Object | ||
.entries(node.nested || {}) | ||
.map(([relName, childNode]) => extractFieldsRec(childNode, prefix.concat(relName))) | ||
.reduce( | ||
(p, c) => p.concat(c), | ||
node.fields.map((field) => prefix.concat(typeof field === 'string' ? field : field.name).join('.')) | ||
); | ||
const extractRelsRec = (node, prefix = []) => Object | ||
@@ -114,3 +107,29 @@ .entries(node.nested || {}) | ||
}; | ||
export const extractFields = (specs) => extractFieldsRec(specs); | ||
const extractFieldsScanner = objectScan([ | ||
'**{nested.*}.fields[*]', | ||
'**{nested.*}.fields[*].name', | ||
'**{nested.*}.fields[*].overwrite.fields.*.type' | ||
], { | ||
filterFn: ({ isLeaf }) => isLeaf, | ||
rtn: ({ key, value, getParents }) => { | ||
const result = []; | ||
let i = 0; | ||
while (key[i] !== 'fields') { | ||
result.push(key[i + 1]); | ||
i += 2; | ||
} | ||
if (key.length - i === 6) { | ||
const parents = getParents(); | ||
result.push(`${parents[3].name}$${key[key.length - 2]}`); | ||
} else { | ||
result.push(value); | ||
} | ||
return result.join('.'); | ||
}, | ||
afterFn: ({ result }) => result.reverse() | ||
}); | ||
export const extractFields = (specs) => extractFieldsScanner(specs); | ||
export const extractRels = (spec) => extractRelsRec(spec); | ||
export const normalize = (field) => field.replace('$', '.'); |
@@ -9,2 +9,3 @@ import assert from 'assert'; | ||
import extractPrefix from './extract-prefix.js'; | ||
import { normalize } from './index.js'; | ||
@@ -80,3 +81,6 @@ const splitPath = (fullPath, mapping, allowedFields) => { | ||
result.sort = [ | ||
...orderBy, | ||
...orderBy.map(([field, ...args]) => [ | ||
typeof field === 'string' ? normalize(field) : field, | ||
...args | ||
]), | ||
...(addScore ? [['_score', 'desc']] : []), | ||
@@ -86,2 +90,7 @@ ...(addId ? [['_id', 'asc']] : []) | ||
.map((e) => actionMap.order[e[1]]([...splitPath(e[0], mapping, allowedFields), ...e.slice(2)], { allowedFields })); | ||
assert( | ||
allowedFields === null | ||
|| orderBy.every(([f]) => f === null || allowedFields.includes(f) || ['_id', '_score'].includes(f)), | ||
'Unexpected field in orderBy' | ||
); | ||
if (result.search_after.length === 0) { | ||
@@ -88,0 +97,0 @@ delete result.search_after; |
@@ -7,2 +7,3 @@ import assert from 'assert'; | ||
import extractPrefix from '../../extract-prefix.js'; | ||
import { normalize } from '../../index.js'; | ||
@@ -16,2 +17,6 @@ export default (call, idx, allowedFields, fields, opts) => { | ||
}).nand('limit', 'cursor')); | ||
assert( | ||
(Array.isArray(fields) ? fields : [fields]).every((f) => allowedFields.includes(f)), | ||
'Unexpected field provided' | ||
); | ||
const cursorPayload = opts.cursor === undefined ? null : fromCursor(opts.cursor); | ||
@@ -33,2 +38,3 @@ const after = get(cursorPayload, 'searchAfter', null); | ||
sources: (Array.isArray(fields) ? fields : [fields]) | ||
.map((f) => normalize(f)) | ||
.map((field) => ({ [field]: { terms: { field } } })) | ||
@@ -57,4 +63,4 @@ } | ||
const value = Array.isArray(fields) | ||
? fields.map((field) => e.key[field]) | ||
: e.key[fields]; | ||
? fields.map((field) => e.key[normalize(field)]) | ||
: e.key[normalize(fields)]; | ||
return count ? [value, e.doc_count] : value; | ||
@@ -61,0 +67,0 @@ }) |
{ | ||
"name": "es-alchemy", | ||
"type": "module", | ||
"version": "10.4.6", | ||
"version": "10.4.7", | ||
"description": "Simplification of Opensearch interactions", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
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
76028
1780