dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
126 lines (125 loc) • 6 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 transformGteCondition = (schema, condition) => {
const conditions = new Deduper();
const schemaFinder = new 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 = 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 NumberSchema({}) : subSchema.schema;
const valueParser = new 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 joinDedupedConditions(conditions, attributePath);
};
export const transformGtCondition = (schema, condition) => {
const conditions = new Deduper();
const schemaFinder = new 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 = 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 NumberSchema({}) : subSchema.schema;
const valueParser = new 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 joinDedupedConditions(conditions, attributePath);
};
export const transformLteCondition = (schema, condition) => {
const conditions = new Deduper();
const schemaFinder = new 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 = 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 NumberSchema({}) : subSchema.schema;
const valueParser = new 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 joinDedupedConditions(conditions, attributePath);
};
export const transformLtCondition = (schema, condition) => {
const conditions = new Deduper();
const schemaFinder = new 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 = 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 NumberSchema({}) : subSchema.schema;
const valueParser = new 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 joinDedupedConditions(conditions, attributePath);
};