UNPKG

dynamodb-toolbox

Version:

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

51 lines (50 loc) 2.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const parseMapping_js_1 = __importDefault(require("./parseMapping.js")); const parseCompositeKey_js_1 = __importDefault(require("./parseCompositeKey.js")); const utils_js_1 = require("./utils.js"); const parseEntityAttributes = (attributes, track) => { // Parse attributes into standard format const parsedAttributes = Object.keys(attributes).reduce((acc, field) => { const attributeDefinition = attributes[field]; // If a string value if (typeof attributeDefinition === 'string') { if ((0, utils_js_1.isDynamoDbType)(attributeDefinition)) { // Merge and return mapping return Object.assign(acc, (0, parseMapping_js_1.default)(field, { type: attributeDefinition }, track)); } else { // If invalid type, throw error (0, utils_js_1.typeError)(field); } } // If an array if (Array.isArray(attributeDefinition)) { return Object.assign(acc, (0, parseCompositeKey_js_1.default)(field, // 🔨 TOIMPROVE: Use typeguard attributes[field], track, attributes)); } // If complex mapping // 🔨 TOIMPROVE: Use typeguard const fieldVal = attributes[field]; // Default field to 'string' fieldVal.type = fieldVal.type || 'string'; // If invalid type, throw error if (!utils_js_1.validTypes.includes(fieldVal.type)) { (0, utils_js_1.typeError)(field); } return Object.assign(acc, (0, parseMapping_js_1.default)(field, fieldVal, track)); }, {}); // Check that a partitionKey was defined (additional checks done when adding table) if (!track.keys.partitionKey) (0, utils_js_1.error)('Entity requires a partitionKey attribute'); // Return keys and attributes return { keys: track.keys, attributes: parsedAttributes }; }; exports.default = parseEntityAttributes;