UNPKG

dynamodb-toolbox

Version:

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

123 lines (122 loc) 5.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseTable = void 0; const parseTableAttributes_js_1 = __importDefault(require("./parseTableAttributes.js")); const utils_js_1 = require("./utils.js"); // Parse table const parseTable = (table) => { let { name, // Table name alias, // For batch references partitionKey, sortKey, entityField, attributes, indexes, // eslint-disable-next-line prefer-const autoExecute, // eslint-disable-next-line prefer-const autoParse, // eslint-disable-next-line prefer-const removeNullAttributes, // eslint-disable-next-line prefer-const entities, // eslint-disable-next-line prefer-const DocumentClient, // eslint-disable-next-line prefer-const ...args // extraneous config } = table; // Error on extraneous arguments if (Object.keys(args).length > 0) (0, utils_js_1.error)(`Invalid Table configuration options: ${Object.keys(args).join(', ')}`); // 🔨 TOIMPROVE: Not triming would be better for type safety (no need to cast) // Table name name = (typeof name === 'string' && name.trim().length > 0 ? name.trim() : (0, utils_js_1.error)(`'name' must be defined`)); // Verify alias alias = typeof alias === 'string' && alias.trim().length > 0 ? alias.trim() : alias ? (0, utils_js_1.error)(`'alias' must be a string value`) : null; // 🔨 TOIMPROVE: Not triming would be better for type safety (no need to cast) // Specify partitionKey attribute partitionKey = (typeof partitionKey === 'string' && partitionKey.trim().length > 0 ? partitionKey.trim() : (0, utils_js_1.error)(`'partitionKey' must be defined`)); // 🔨 TOIMPROVE: Not triming would be better for type safety (no need to cast) // Specify sortKey attribute (optional) sortKey = (typeof sortKey === 'string' && sortKey.trim().length > 0 ? sortKey.trim() : sortKey ? (0, utils_js_1.error)(`'sortKey' must be a string value`) : null); // Disable, or rename field for entity tracking entityField = entityField === false ? false : typeof entityField === 'string' && entityField.trim().length > 0 ? entityField.trim() : '_et'; // Parse table attributes attributes = (0, utils_js_1.hasValue)(attributes) && (attributes === null || attributes === void 0 ? void 0 : attributes.constructor) === Object ? attributes : attributes ? (0, utils_js_1.error)(`Please provide a valid 'attributes' object`) : {}; // Add entityField to attributes if (entityField) attributes[entityField] = 'string'; // Parse indexes (optional) indexes = (0, utils_js_1.hasValue)(indexes) && (indexes === null || indexes === void 0 ? void 0 : indexes.constructor) === Object ? // 🔨 TOIMPROVE: Allow numbers & symbols in parseIndexes ? parseIndexes(indexes, partitionKey) : indexes ? (0, utils_js_1.error)(`Please provide a valid 'indexes' object`) : {}; // Return the table return Object.assign({ name, alias, Table: { partitionKey, sortKey, entityField, // 🔨 TOIMPROVE: Allow numbers & symbols in parseAttributes ? attributes: (0, parseTableAttributes_js_1.default)(attributes, partitionKey, sortKey), indexes }, autoExecute, autoParse, removeNullAttributes, _entities: [] }, DocumentClient ? { DocumentClient } : {}, entities ? { entities } : {}); }; exports.parseTable = parseTable; // Parse Indexes const parseIndexes = (indexes, pk) => Object.keys(indexes).reduce((acc, index) => { // TODO: indexes can not be named TABLE // Destructure the index const { partitionKey, sortKey, ...args } = indexes[index]; // Error on extraneous arguments if (Object.keys(args).length > 0) (0, utils_js_1.error)(`Invalid index options: ${Object.keys(args).join(', ')}`); // Verify partitionKey if (partitionKey && typeof partitionKey !== 'string') (0, utils_js_1.error)(`'partitionKey' for ${index} must be a string`); // Verify sortKey if (sortKey && typeof sortKey !== 'string') (0, utils_js_1.error)(`'sortKey' for ${index} must be a string`); // Verify the presences of either pk or sk if (!sortKey && !partitionKey) (0, utils_js_1.error)(`A 'partitionKey', 'sortKey' or both, must be provided for ${index}`); // Guess index type const type = !partitionKey || partitionKey === pk ? 'LSI' : 'GSI'; // Return the structured index object return Object.assign(acc, { [index]: Object.assign({}, partitionKey && type === 'GSI' ? { partitionKey } : {}, sortKey ? { sortKey } : {}, { type }) }); }, {}); exports.default = exports.parseTable;