dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
66 lines (65 loc) • 2.94 kB
JavaScript
import { DynamoDBToolboxError } from '../../../../../errors/index.js';
import { Parser } from '../../../../../schema/actions/parse/index.js';
import { formatArrayPath } from '../../../../../schema/actions/utils/formatArrayPath.js';
import { isArray } from '../../../../../utils/validation/isArray.js';
import { isString } from '../../../../../utils/validation/isString.js';
import { $GET, isGetting } from '../../symbols/index.js';
export const parseReferenceExtension = (schema, inputValue, { transform = true, valuePath } = {}) => {
if (!isGetting(inputValue) || inputValue[$GET] === undefined) {
return {
isExtension: false,
unextendedInput: inputValue
};
}
return {
isExtension: true,
*extensionParser() {
const references = inputValue[$GET];
const referencesPath = [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), '$GET'];
if (!isArray(references)) {
const path = formatArrayPath(referencesPath);
throw new 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 (!isString(reference)) {
const path = formatArrayPath([...referencesPath, 0]);
throw new 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 Parser(schema).start(fallback, {
fill: false,
transform,
parseExtension: parseReferenceExtension,
valuePath: [...referencesPath, 1]
})
: undefined;
const parsedValue = {
[$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 = {
[$GET]: [reference, ...(fallbackParser !== undefined ? [fallbackParser.next().value] : [])]
};
return transformedValue;
}
};
};