dynamodb-toolbox
Version:
A simple set of tools for working with Amazon DynamoDB and the DocumentClient.
36 lines (35 loc) • 1.71 kB
JavaScript
;
/**
* DynamoDB Toolbox: A simple set of tools for working with Amazon DynamoDB
* @author Jeremy Daly <jeremy@jeremydaly.com>
* @license MIT
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const validateTypes_js_1 = __importDefault(require("./validateTypes.js"));
const utils_js_1 = require("./utils.js");
// Get partitionKey/sortKey
exports.default = () => (data, schema, partitionKey, sortKey) => {
// TODO: Think about a better way to reference pk/sk - can it work with secondary indexes?
partitionKey = schema[partitionKey].map || partitionKey;
sortKey = (schema[sortKey] && schema[sortKey].map) || sortKey || null;
const validateType = (0, validateTypes_js_1.default)();
// TODO: Add tests for 0 values
const pk = data[partitionKey];
if (pk === undefined || pk === null || pk === '') {
(0, utils_js_1.error)(`'${partitionKey}'${schema[partitionKey].alias ? ` or '${schema[partitionKey].alias}'` : ''} is required`);
}
const sk = data[sortKey];
if (sortKey && (sk === undefined || sk === null || sk === '')) {
(0, utils_js_1.error)(`'${sortKey}'${schema[sortKey].alias ? ` or '${schema[sortKey].alias}'` : ''} is required`);
}
return Object.assign({
[partitionKey]: (0, utils_js_1.transformAttr)(schema[partitionKey], validateType(schema[partitionKey], partitionKey, pk), data)
}, sortKey !== null
? {
[sortKey]: (0, utils_js_1.transformAttr)(schema[sortKey], validateType(schema[sortKey], sortKey, sk), data)
}
: {});
};