dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
77 lines (76 loc) • 3.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformBetweenCondition = 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 transformBetweenCondition = (schema, condition) => {
const conditions = new deduper_js_1.Deduper();
const schemaFinder = new index_js_1.Finder(schema);
const { between: formattedBetween } = condition;
const size = 'size' in condition;
const attributePath = size ? condition.size : condition.attr;
const transform = size ? undefined : condition.transform;
const subSchemas = schemaFinder.search(attributePath);
const [formattedLeft, formattedRight] = formattedBetween;
const comparedLeftSubSchemas = (0, utils_js_1.getComparedSubSchemas)(schemaFinder, formattedLeft, transform);
const comparedRightSubSchemas = (0, utils_js_1.getComparedSubSchemas)(schemaFinder, formattedRight, transform);
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);
const betweens = new deduper_js_1.Deduper();
if (comparedLeftSubSchemas !== undefined) {
for (const comparedLeftSubSchema of comparedLeftSubSchemas) {
const left = { attr: comparedLeftSubSchema.transformedPath.strPath };
if (comparedRightSubSchemas !== undefined) {
for (const comparedRightSubSchema of comparedRightSubSchemas) {
const right = { attr: comparedRightSubSchema.transformedPath.strPath };
betweens.push([left, right]);
}
}
else {
try {
const right = valueParser.parse(formattedRight, { fill: false, transform });
betweens.push([left, right]);
// eslint-disable-next-line no-empty
}
catch { }
}
}
}
else {
let left;
try {
left = valueParser.parse(formattedLeft, { fill: false, transform });
}
catch {
continue;
}
if (comparedRightSubSchemas !== undefined) {
for (const comparedRightSubSchema of comparedRightSubSchemas) {
const right = { attr: comparedRightSubSchema.transformedPath.strPath };
betweens.push([left, right]);
}
}
else {
try {
const right = valueParser.parse(formattedRight, { fill: false, transform });
betweens.push([left, right]);
// eslint-disable-next-line no-empty
}
catch { }
}
}
if (betweens.values.length === 0) {
continue;
}
for (const between of betweens.values) {
conditions.push((size ? { size: path, between } : { attr: path, between }));
}
}
return (0, utils_js_1.joinDedupedConditions)(conditions, attributePath);
};
exports.transformBetweenCondition = transformBetweenCondition;