dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
133 lines (132 loc) • 6.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformLtCondition = exports.transformLteCondition = exports.transformGtCondition = exports.transformGteCondition = 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 transformGteCondition = (schema, condition) => {
const conditions = new deduper_js_1.Deduper();
const schemaFinder = new index_js_1.Finder(schema);
const { gte: formattedGte } = 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, formattedGte, transform);
for (const subSchema of subSchemas) {
const path = subSchema.transformedPath.strPath;
if (comparedSubSchemas !== undefined) {
for (const comparedSubSchema of comparedSubSchemas) {
const gte = { attr: comparedSubSchema.transformedPath.strPath };
conditions.push(size ? { size: path, gte } : { attr: path, gte });
}
}
else {
const valueSchema = size ? new schema_js_1.NumberSchema({}) : subSchema.schema;
const valueParser = new parser_js_1.Parser(valueSchema);
try {
const gte = valueParser.parse(formattedGte, { fill: false, transform });
conditions.push((size ? { size: path, gte } : { attr: path, gte }));
// eslint-disable-next-line no-empty
}
catch { }
}
}
return (0, utils_js_1.joinDedupedConditions)(conditions, attributePath);
};
exports.transformGteCondition = transformGteCondition;
const transformGtCondition = (schema, condition) => {
const conditions = new deduper_js_1.Deduper();
const schemaFinder = new index_js_1.Finder(schema);
const { gt: formattedGt } = 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, formattedGt, transform);
for (const subSchema of subSchemas) {
const path = subSchema.transformedPath.strPath;
if (comparedSubSchemas !== undefined) {
for (const comparedSubSchema of comparedSubSchemas) {
const gt = { attr: comparedSubSchema.transformedPath.strPath };
conditions.push(size ? { size: path, gt } : { attr: path, gt });
}
}
else {
const valueSchema = size ? new schema_js_1.NumberSchema({}) : subSchema.schema;
const valueParser = new parser_js_1.Parser(valueSchema);
try {
const gt = valueParser.parse(formattedGt, { fill: false, transform });
conditions.push((size ? { size: path, gt } : { attr: path, gt }));
// eslint-disable-next-line no-empty
}
catch { }
}
}
return (0, utils_js_1.joinDedupedConditions)(conditions, attributePath);
};
exports.transformGtCondition = transformGtCondition;
const transformLteCondition = (schema, condition) => {
const conditions = new deduper_js_1.Deduper();
const schemaFinder = new index_js_1.Finder(schema);
const { lte: formattedLte } = 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, formattedLte, transform);
for (const subSchema of subSchemas) {
const path = subSchema.transformedPath.strPath;
if (comparedSubSchemas !== undefined) {
for (const comparedSubSchema of comparedSubSchemas) {
const lte = { attr: comparedSubSchema.transformedPath.strPath };
conditions.push(size ? { size: path, lte } : { attr: path, lte });
}
}
else {
const valueSchema = size ? new schema_js_1.NumberSchema({}) : subSchema.schema;
const valueParser = new parser_js_1.Parser(valueSchema);
try {
const lte = valueParser.parse(formattedLte, { fill: false, transform });
conditions.push((size ? { size: path, lte } : { attr: path, lte }));
// eslint-disable-next-line no-empty
}
catch { }
}
}
return (0, utils_js_1.joinDedupedConditions)(conditions, attributePath);
};
exports.transformLteCondition = transformLteCondition;
const transformLtCondition = (schema, condition) => {
const conditions = new deduper_js_1.Deduper();
const schemaFinder = new index_js_1.Finder(schema);
const { lt: formattedLt } = 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, formattedLt, transform);
for (const subSchema of subSchemas) {
const path = subSchema.transformedPath.strPath;
if (comparedSubSchemas !== undefined) {
for (const comparedSubSchema of comparedSubSchemas) {
const lt = { attr: comparedSubSchema.transformedPath.strPath };
conditions.push(size ? { size: path, lt } : { attr: path, lt });
}
}
else {
const valueSchema = size ? new schema_js_1.NumberSchema({}) : subSchema.schema;
const valueParser = new parser_js_1.Parser(valueSchema);
try {
const lt = valueParser.parse(formattedLt, { fill: false, transform });
conditions.push((size ? { size: path, lt } : { attr: path, lt }));
// eslint-disable-next-line no-empty
}
catch { }
}
}
return (0, utils_js_1.joinDedupedConditions)(conditions, attributePath);
};
exports.transformLtCondition = transformLtCondition;