UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

21 lines (20 loc) 996 B
export const doesSchemaValidateTableSchemaKey = (schema, key) => { if (key === undefined) return true; const keyAttributeEntry = [...schema.keyAttributeNames.values()] .map(attributeName => [attributeName, schema.attributes[attributeName]]) .find(([attributeName, { props }]) => props.savedAs === key.name || (props.savedAs === undefined && attributeName === key.name)); if (keyAttributeEntry === undefined) { return false; } const [, keyAttribute] = keyAttributeEntry; return (keyAttribute !== undefined && keyAttribute.type === key.type && keyAttribute.props.key === true && (keyAttribute.props.required === 'always' || keyAttribute.props.keyDefault !== undefined)); }; export const doesSchemaValidateTableSchema = (schema, table) => { const { partitionKey, sortKey } = table; return (doesSchemaValidateTableSchemaKey(schema, partitionKey) && doesSchemaValidateTableSchemaKey(schema, sortKey)); };