dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
61 lines (60 loc) • 2.67 kB
JavaScript
import { $GET, isGetting, isRemoval } from '../../../../../entity/actions/update/symbols/index.js';
import { parseNumberExtension } from '../../../../../entity/actions/update/updateItemParams/extension/number.js';
import { parseReferenceExtension } from '../../../../../entity/actions/update/updateItemParams/extension/reference.js';
import { parseSetExtension } from '../../../../../entity/actions/update/updateItemParams/extension/set.js';
import { DynamoDBToolboxError } from '../../../../../errors/index.js';
import { formatArrayPath } from '../../../../../schema/actions/utils/formatArrayPath.js';
import { parseAnyExtension } from './any.js';
import { parseListExtension } from './list.js';
import { parseMapExtension } from './map.js';
import { parseRecordExtension } from './record.js';
export const parseUpdateAttributesExtension = (schema, input, options = {}) => {
const { transform = true, valuePath } = options;
if (isRemoval(input)) {
return {
isExtension: true,
*extensionParser() {
const { props } = schema;
const { required } = props;
const path = valuePath !== undefined ? formatArrayPath(valuePath) : undefined;
if (required !== 'never') {
throw new DynamoDBToolboxError('parsing.attributeRequired', {
message: `Attribute ${path !== undefined ? `'${path}' ` : ''}is required and cannot be removed`,
path
});
}
const parsedValue = input;
if (transform) {
yield parsedValue;
}
else {
return parsedValue;
}
const transformedValue = input;
return transformedValue;
}
};
}
if (isGetting(input) && input[$GET] !== undefined) {
return parseReferenceExtension(schema, input, options);
}
switch (schema.type) {
case 'any':
return parseAnyExtension(schema, input, options);
case 'number':
return parseNumberExtension(schema, input, options);
case 'set':
return parseSetExtension(schema, input, options);
case 'list':
return parseListExtension(schema, input, options);
case 'map':
return parseMapExtension(schema, input, options);
case 'record':
return parseRecordExtension(schema, input, options);
default:
return {
isExtension: false,
unextendedInput: input
};
}
};