dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
49 lines (48 loc) • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformContainsCondition = 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/string/schema.js");
const parser_js_1 = require("../../../parse/parser.js");
const utils_js_1 = require("./utils.js");
const transformContainsCondition = (schema, condition) => {
const conditions = new deduper_js_1.Deduper();
const schemaFinder = new index_js_1.Finder(schema);
const { contains: formattedContains, transform } = condition;
const attributePath = condition.attr;
const subSchemas = schemaFinder.search(attributePath);
const comparedSubSchemas = (0, utils_js_1.getComparedSubSchemas)(schemaFinder, formattedContains, transform);
for (const subSchema of subSchemas) {
const path = subSchema.transformedPath.strPath;
if (comparedSubSchemas !== undefined) {
for (const comparedSubSchema of comparedSubSchemas) {
const contains = { attr: comparedSubSchema.transformedPath.strPath };
conditions.push({ attr: path, contains });
}
}
else {
try {
let valueSchema = subSchema.schema;
switch (subSchema.schema.type) {
case 'set':
case 'list':
valueSchema = subSchema.schema.elements;
break;
case 'string':
// We accept any string in case of contains
valueSchema = new schema_js_1.StringSchema({});
break;
default:
}
const valueParser = new parser_js_1.Parser(valueSchema);
const contains = valueParser.parse(formattedContains, { fill: false, transform });
conditions.push({ attr: path, contains });
// eslint-disable-next-line no-empty
}
catch { }
}
}
return (0, utils_js_1.joinDedupedConditions)(conditions, attributePath);
};
exports.transformContainsCondition = transformContainsCondition;