UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

33 lines (32 loc) 1.55 kB
import { Finder } from '../../../../../schema/actions/finder/index.js'; import { Deduper } from '../../../../../schema/actions/utils/deduper.js'; import { Parser } from '../../../parse/parser.js'; import { getComparedSubSchemas, joinDedupedConditions } from './utils.js'; export const transformBeginsWithCondition = (schema, condition) => { const conditions = new Deduper(); const schemaFinder = new Finder(schema); const { beginsWith: formattedBeginsWith, transform } = condition; const attributePath = condition.attr; const subSchemas = schemaFinder.search(attributePath); const comparedSubSchemas = getComparedSubSchemas(schemaFinder, formattedBeginsWith, transform); for (const subSchema of subSchemas) { const path = subSchema.transformedPath.strPath; if (comparedSubSchemas !== undefined) { for (const comparedSubSchema of comparedSubSchemas) { const beginsWith = { attr: comparedSubSchema.transformedPath.strPath }; conditions.push({ attr: path, beginsWith }); } } else { const valueSchema = subSchema.schema; const valueParser = new Parser(valueSchema); try { const beginsWith = valueParser.parse(formattedBeginsWith, { fill: false, transform }); conditions.push({ attr: path, beginsWith }); // eslint-disable-next-line no-empty } catch { } } } return joinDedupedConditions(conditions, attributePath); };