UNPKG

dynamodb-dashboard

Version:
120 lines (117 loc) 5.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.constructSchema = constructSchema; exports.deserialize = deserialize; exports.serialize = serialize; var _lodash = require("lodash"); var _libDynamodb = require("@aws-sdk/lib-dynamodb"); var _clientDynamodb = require("@aws-sdk/client-dynamodb"); var _table = require("../schemas/table.joi"); function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } /** * @param {object} Table * * @returns {object} */ function constructSchema(Table) { var _Table$BillingModeSum; var keys = Object.keys(_table.create.describe().keys); var schema = (0, _lodash.pick)(Table, keys); schema.BillingMode = ((_Table$BillingModeSum = Table.BillingModeSummary) === null || _Table$BillingModeSum === void 0 ? void 0 : _Table$BillingModeSum.BillingMode) || _clientDynamodb.BillingMode.PROVISIONED; if (schema.BillingMode !== _clientDynamodb.BillingMode.PAY_PER_REQUEST) { return schema; } delete schema.ProvisionedThroughput; if (schema.GlobalSecondaryIndexes) { schema.GlobalSecondaryIndexes.forEach(function (gsi) { return delete gsi.ProvisionedThroughput; }); } return schema; } /** * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes * * @param {object} Item * @param {string} path * @param {function} callback * * @returns {object} */ function serialize(Item) { var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ""; var callback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {}; if (["string", "number", "boolean"].includes(_typeof(Item)) || Item === null) { return Item; } if (Item instanceof Set) { callback(path); return Array.from(Item); } if (Item instanceof Array) { return Item.map(function (value, index) { return serialize(value, "".concat(path, "[").concat(index, "]"), callback); }); } var output = {}; Object.entries(Item).forEach(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), key = _ref2[0], value = _ref2[1]; var currentPath = path ? "".concat(path, ".").concat(key) : key; if (value instanceof Set) { callback(currentPath); output[key] = Array.from(value); return; } if (typeof value === "bigint") { callback(currentPath); output[key] = String(value); return; } if (value instanceof Array) { output[key] = value.map(function (item, index) { return serialize(item, "".concat(currentPath, "[").concat(index, "]"), callback); }); return; } if (value instanceof Object) { output[key] = serialize(value, currentPath, callback); return; } output[key] = value; }); return output; } /** * @param {object} object * @param {object} source * * @returns {object} */ function deserialize() { var source = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var object = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var paths = []; serialize(source, "", function (path) { return paths.push(path); }); paths.forEach(function (path) { var value = (0, _lodash.get)(object, path); if (value instanceof Array) { (0, _lodash.set)(object, path, new Set(value)); } if (typeof value === "string") { (0, _lodash.set)(object, path, new _libDynamodb.NumberValue(value)); } }); return object; }