dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
66 lines (65 loc) • 3.16 kB
JavaScript
import { Finder } from '../../../../../schema/actions/finder/index.js';
import { Deduper } from '../../../../../schema/actions/utils/deduper.js';
import { NumberSchema } from '../../../../../schema/number/schema.js';
import { Parser } from '../../../parse/parser.js';
import { getComparedSubSchemas, joinDedupedConditions } from './utils.js';
export const transformEqCondition = (schema, condition) => {
const conditions = new Deduper();
const schemaFinder = new 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 = 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 NumberSchema({}) : subSchema.schema;
const valueParser = new 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 joinDedupedConditions(conditions, attributePath);
};
export const transformNeCondition = (schema, condition) => {
const conditions = new Deduper();
const schemaFinder = new 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 = 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 NumberSchema({}) : subSchema.schema;
const valueParser = new 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 joinDedupedConditions(conditions, attributePath);
};