dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
71 lines (70 loc) • 3.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformNeCondition = exports.transformEqCondition = 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 transformEqCondition = (schema, condition) => {
const conditions = new deduper_js_1.Deduper();
const schemaFinder = new index_js_1.Finder(schema);
const { eq: formattedEq } = 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 comparedSubSchemas = (0, utils_js_1.getComparedSubSchemas)(schemaFinder, formattedEq, transform);
for (const subSchema of subSchemas) {
const path = subSchema.transformedPath.strPath;
if (comparedSubSchemas !== undefined) {
for (const comparedSubSchema of comparedSubSchemas) {
const eq = { attr: comparedSubSchema.transformedPath.strPath };
conditions.push(size ? { size: path, eq } : { attr: path, eq });
}
}
else {
const valueSchema = size ? new schema_js_1.NumberSchema({}) : subSchema.schema;
const valueParser = new parser_js_1.Parser(valueSchema);
try {
const eq = valueParser.parse(formattedEq, { fill: false, transform });
conditions.push((size ? { size: path, eq } : { attr: path, eq }));
// eslint-disable-next-line no-empty
}
catch { }
}
}
return (0, utils_js_1.joinDedupedConditions)(conditions, attributePath);
};
exports.transformEqCondition = transformEqCondition;
const transformNeCondition = (schema, condition) => {
const conditions = new deduper_js_1.Deduper();
const schemaFinder = new index_js_1.Finder(schema);
const { ne: formattedNe } = 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 comparedSubSchemas = (0, utils_js_1.getComparedSubSchemas)(schemaFinder, formattedNe, transform);
for (const subSchema of subSchemas) {
const path = subSchema.transformedPath.strPath;
if (comparedSubSchemas !== undefined) {
for (const comparedSubSchema of comparedSubSchemas) {
const ne = { attr: comparedSubSchema.transformedPath.strPath };
conditions.push(size ? { size: path, ne } : { attr: path, ne });
}
}
else {
const valueSchema = size ? new schema_js_1.NumberSchema({}) : subSchema.schema;
const valueParser = new parser_js_1.Parser(valueSchema);
try {
const ne = valueParser.parse(formattedNe, { fill: false, transform });
conditions.push((size ? { size: path, ne } : { attr: path, ne }));
// eslint-disable-next-line no-empty
}
catch { }
}
}
return (0, utils_js_1.joinDedupedConditions)(conditions, attributePath);
};
exports.transformNeCondition = transformNeCondition;