dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
95 lines (94 loc) • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseRecordExtension = void 0;
const index_js_1 = require("../../../../../schema/actions/parse/index.js");
const isObject_js_1 = require("../../../../../utils/validation/isObject.js");
const index_js_2 = require("../../symbols/index.js");
const attribute_js_1 = require("./attribute.js");
function* recordElementsParser(schema, inputValue, { transform = true, valuePath } = {}) {
if ((0, index_js_2.isRemoval)(inputValue)) {
const parsedValue = inputValue;
if (transform) {
yield parsedValue;
}
else {
return parsedValue;
}
const transformedValue = parsedValue;
return transformedValue;
}
return yield* new index_js_1.Parser(schema.elements).start(inputValue, {
mode: 'update',
fill: false,
transform,
parseExtension: attribute_js_1.parseUpdateExtension,
valuePath
});
}
const parseRecordExtension = (schema, input, { transform = true, valuePath } = {}) => {
if ((0, index_js_2.isSetting)(input) && input[index_js_2.$SET] !== undefined) {
return {
isExtension: true,
*extensionParser() {
const parser = new index_js_1.Parser(schema).start(input[index_js_2.$SET], {
fill: false,
transform,
valuePath: [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), '$SET']
});
const parsedValue = { [index_js_2.$SET]: parser.next().value };
if (transform) {
yield parsedValue;
}
else {
return parsedValue;
}
const transformedValue = { [index_js_2.$SET]: parser.next().value };
return transformedValue;
}
};
}
if ((0, isObject_js_1.isObject)(input)) {
return {
isExtension: true,
*extensionParser() {
const parsers = Object.entries(input)
.filter(([, inputValue]) => inputValue !== undefined)
.map(([inputKey, inputValue]) => [
new index_js_1.Parser(schema.keys).start(inputKey, { fill: false, transform }),
recordElementsParser(schema, inputValue, {
transform,
valuePath: [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), inputKey]
})
]);
const parsedValue = Object.fromEntries(parsers
.map(([keyParser, elementParser]) => [
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
keyParser.next().value,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
elementParser.next().value
])
.filter(([, element]) => element !== undefined));
if (transform) {
yield parsedValue;
}
else {
return parsedValue;
}
const transformedValue = Object.fromEntries(parsers
.map(([keyParser, elementParser]) => [
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
keyParser.next().value,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
elementParser.next().value
])
.filter(([, element]) => element !== undefined));
return transformedValue;
}
};
}
return {
isExtension: false,
unextendedInput: input
};
};
exports.parseRecordExtension = parseRecordExtension;