dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
70 lines (69 loc) • 3.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseReferenceExtension = void 0;
const index_js_1 = require("../../../../../errors/index.js");
const index_js_2 = require("../../../../../schema/actions/parse/index.js");
const formatArrayPath_js_1 = require("../../../../../schema/actions/utils/formatArrayPath.js");
const isArray_js_1 = require("../../../../../utils/validation/isArray.js");
const isString_js_1 = require("../../../../../utils/validation/isString.js");
const index_js_3 = require("../../symbols/index.js");
const parseReferenceExtension = (schema, inputValue, { transform = true, valuePath } = {}) => {
if (!(0, index_js_3.isGetting)(inputValue) || inputValue[index_js_3.$GET] === undefined) {
return {
isExtension: false,
unextendedInput: inputValue
};
}
return {
isExtension: true,
*extensionParser() {
const references = inputValue[index_js_3.$GET];
const referencesPath = [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), '$GET'];
if (!(0, isArray_js_1.isArray)(references)) {
const path = (0, formatArrayPath_js_1.formatArrayPath)(referencesPath);
throw new index_js_1.DynamoDBToolboxError('parsing.invalidAttributeInput', {
message: `References ${path !== undefined ? `for attribute '${path}' ` : ''}should be a tuple of one or two elements`,
path,
payload: { received: references }
});
}
const [reference, fallback] = references;
if (!(0, isString_js_1.isString)(reference)) {
const path = (0, formatArrayPath_js_1.formatArrayPath)([...referencesPath, 0]);
throw new index_js_1.DynamoDBToolboxError('parsing.invalidAttributeInput', {
message: `First elements of references ${path !== undefined ? `for attribute '${path}' ` : ''}should be strings`,
path,
payload: { received: reference }
});
}
const fallbackParser = fallback !== undefined
? new index_js_2.Parser(schema).start(fallback, {
fill: false,
transform,
parseExtension: exports.parseReferenceExtension,
valuePath: [...referencesPath, 1]
})
: undefined;
const parsedValue = {
[index_js_3.$GET]: [
/**
* @debt "TODO: Validate reference here and remove entities/schemas in expressUpdate"
*/
reference,
...(fallbackParser !== undefined ? [fallbackParser.next().value] : [])
]
};
if (transform) {
yield parsedValue;
}
else {
return parsedValue;
}
const transformedValue = {
[index_js_3.$GET]: [reference, ...(fallbackParser !== undefined ? [fallbackParser.next().value] : [])]
};
return transformedValue;
}
};
};
exports.parseReferenceExtension = parseReferenceExtension;