dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
47 lines (46 loc) • 2.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformInCondition = void 0;
const index_js_1 = require("../../../../../schema/actions/finder/index.js");
const deduper_js_1 = require("../../../../../schema/actions/utils/deduper.js");
const schema_js_1 = require("../../../../../schema/number/schema.js");
const parser_js_1 = require("../../../parse/parser.js");
const utils_js_1 = require("./utils.js");
const transformInCondition = (schema, condition) => {
const conditions = new deduper_js_1.Deduper();
const schemaFinder = new index_js_1.Finder(schema);
const { in: formattedIns } = condition;
const size = 'size' in condition;
const attributePath = size ? condition.size : condition.attr;
const transform = size ? undefined : condition.transform;
const subSchemas = schemaFinder.search(attributePath);
for (const subSchema of subSchemas) {
const path = subSchema.transformedPath.strPath;
const valueSchema = size ? new schema_js_1.NumberSchema({}) : subSchema.schema;
const valueParser = new parser_js_1.Parser(valueSchema);
// Wrap value in object to avoid mixing str/number types
const _in = new deduper_js_1.Deduper({ serializer: value => JSON.stringify({ _: value }) });
for (const formattedRangeValue of formattedIns) {
const comparedSubSchemas = (0, utils_js_1.getComparedSubSchemas)(schemaFinder, formattedRangeValue, transform);
if (comparedSubSchemas !== undefined) {
for (const comparedSubSchema of comparedSubSchemas) {
_in.push({ attr: comparedSubSchema.transformedPath.strPath });
}
}
else {
try {
_in.push(valueParser.parse(formattedRangeValue, { fill: false, transform }));
// eslint-disable-next-line no-empty
}
catch { }
}
}
const inValues = _in.values;
if (inValues.length === 0) {
continue;
}
conditions.push((size ? { size: path, in: inValues } : { attr: path, in: inValues }));
}
return (0, utils_js_1.joinDedupedConditions)(conditions, attributePath);
};
exports.transformInCondition = transformInCondition;