UNPKG

dynamodb-toolbox

Version:

A simple set of tools for working with Amazon DynamoDB and the DocumentClient.

56 lines (55 loc) 2.21 kB
"use strict"; /** * DynamoDB Toolbox: A simple set of tools for working with Amazon DynamoDB * @author Jeremy Daly <jeremy@jeremydaly.com> * @license MIT */ Object.defineProperty(exports, "__esModule", { value: true }); const utils_js_1 = require("./utils.js"); // Parse the attributes and verify valid types exports.default = (attrs, partitionKey, sortKey) => Object.keys(attrs).reduce((acc, field) => { const attribute = attrs[field]; if (typeof attribute === 'string') { // Error if invalid key type if ([partitionKey, sortKey].includes(field) && !(0, utils_js_1.isDynamoDbKeyType)(attribute)) { (0, utils_js_1.keyTypeError)(field); } // Error if invalid type if (!(0, utils_js_1.isDynamoDbType)(attribute)) { (0, utils_js_1.typeError)(field); } // Merge and return parsed attribute return Object.assign(acc, parseAttributeConfig(field, { type: attribute })); } // Error if invalid key type if ([partitionKey, sortKey].includes(field) && !(0, utils_js_1.isDynamoDbKeyType)(attribute.type)) { (0, utils_js_1.keyTypeError)(field); } // Error if invalid type if (!(0, utils_js_1.isDynamoDbType)(attribute.type)) { (0, utils_js_1.typeError)(field); } // Merge and return parsed attribute return Object.assign(acc, parseAttributeConfig(field, attribute)); }, {}); // Parse and validate attributes config const parseAttributeConfig = (field, config) => { // Validate props Object.keys(config).forEach(prop => { switch (prop) { case 'type': break; case 'setType': if (config.type !== 'set') (0, utils_js_1.error)(`'setType' is only valid for type 'set'`); if (!['string', 'number', 'bigint', 'binary'].includes(config[prop])) (0, utils_js_1.error)(`Invalid 'setType', must be 'string', 'number', 'bigint' or 'binary'`); break; default: (0, utils_js_1.error)(`'${prop}' is not a valid property type`); } }); return { [field]: { ...config, mappings: {} } }; };