dynamodb-dashboard
Version:
GUI Dashboard for local or remote DynamoDB
443 lines (441 loc) • 23.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _aws = _interopRequireDefault(require("../config/aws"));
var _dynamodb = require("../constants/dynamodb");
var _dynamodb2 = require("../utils/dynamodb");
var _database = _interopRequireDefault(require("./database.service"));
var _clientDynamodb = require("@aws-sdk/client-dynamodb");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
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 _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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
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 asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _awaitAsyncGenerator(e) { return new _OverloadYield(e, 0); }
function _wrapAsyncGenerator(e) { return function () { return new AsyncGenerator(e.apply(this, arguments)); }; }
function AsyncGenerator(e) { var r, t; function resume(r, t) { try { var n = e[r](t), o = n.value, u = o instanceof _OverloadYield; Promise.resolve(u ? o.v : o).then(function (t) { if (u) { var i = "return" === r ? "return" : "next"; if (!o.k || t.done) return resume(i, t); t = e[i](t).value; } settle(n.done ? "return" : "normal", t); }, function (e) { resume("throw", e); }); } catch (e) { settle("throw", e); } } function settle(e, n) { switch (e) { case "return": r.resolve({ value: n, done: !0 }); break; case "throw": r.reject(n); break; default: r.resolve({ value: n, done: !1 }); } (r = r.next) ? resume(r.key, r.arg) : t = null; } this._invoke = function (e, n) { return new Promise(function (o, u) { var i = { key: e, arg: n, resolve: o, reject: u, next: null }; t ? t = t.next = i : (r = t = i, resume(e, n)); }); }, "function" != typeof e["return"] && (this["return"] = void 0); }
AsyncGenerator.prototype["function" == typeof Symbol && Symbol.asyncIterator || "@@asyncIterator"] = function () { return this; }, AsyncGenerator.prototype.next = function (e) { return this._invoke("next", e); }, AsyncGenerator.prototype["throw"] = function (e) { return this._invoke("throw", e); }, AsyncGenerator.prototype["return"] = function (e) { return this._invoke("return", e); };
function _OverloadYield(e, d) { this.v = e, this.k = d; }
var TableServiceProvider = exports["default"] = /*#__PURE__*/function () {
/**
* @param {AWS} _AWS_
* @param {object} credentials
*/
function TableServiceProvider() {
var _AWS_ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _aws["default"];
_classCallCheck(this, TableServiceProvider);
this.AWS = _AWS_;
}
/**
* @returns {Promise<Array<object>>}
*/
return _createClass(TableServiceProvider, [{
key: "all",
value: (function () {
var _all = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
var tables, params, response;
return _regenerator().w(function (_context) {
while (1) switch (_context.n) {
case 0:
tables = [];
params = {};
case 1:
_context.n = 2;
return this.AWS.dynamodb.listTables(params);
case 2:
response = _context.v;
tables.push.apply(tables, _toConsumableArray(response.TableNames));
params.ExclusiveStartTableName = response.LastEvaluatedTableName;
case 3:
if (params.ExclusiveStartTableName) {
_context.n = 1;
break;
}
case 4:
return _context.a(2, tables);
}
}, _callee, this);
}));
function all() {
return _all.apply(this, arguments);
}
return all;
}()
/**
* @param {object} params
*
* @returns {Promise<object>}
*/
)
}, {
key: "create",
value: (function () {
var _create = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(params) {
var options,
response,
_args2 = arguments;
return _regenerator().w(function (_context2) {
while (1) switch (_context2.n) {
case 0:
options = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {};
_context2.n = 1;
return this.AWS.dynamodb.createTable(params, options);
case 1:
response = _context2.v;
return _context2.a(2, response);
}
}, _callee2, this);
}));
function create(_x) {
return _create.apply(this, arguments);
}
return create;
}()
/**
* @param {object} params
*
* @returns {Promise<object>}
*/
)
}, {
key: "describe",
value: (function () {
var _describe = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(tableName, options) {
var response;
return _regenerator().w(function (_context3) {
while (1) switch (_context3.n) {
case 0:
_context3.n = 1;
return this.AWS.dynamodb.describeTable({
TableName: tableName
}, options);
case 1:
response = _context3.v;
return _context3.a(2, response);
}
}, _callee3, this);
}));
function describe(_x2, _x3) {
return _describe.apply(this, arguments);
}
return describe;
}()
/**
* @param {string} tableName
*
* @returns {Promise<object>}
*/
)
}, {
key: "describeTimeToLive",
value: (function () {
var _describeTimeToLive = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(tableName) {
var response;
return _regenerator().w(function (_context4) {
while (1) switch (_context4.n) {
case 0:
_context4.n = 1;
return this.AWS.dynamodb.describeTimeToLive({
TableName: tableName
});
case 1:
response = _context4.v;
return _context4.a(2, response);
}
}, _callee4, this);
}));
function describeTimeToLive(_x4) {
return _describeTimeToLive.apply(this, arguments);
}
return describeTimeToLive;
}()
/**
* @param {string} tableName
*
* @returns {Promise<object>}
*/
)
}, {
key: "disableTimeToLive",
value: (function () {
var _disableTimeToLive = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(tableName) {
var response;
return _regenerator().w(function (_context5) {
while (1) switch (_context5.n) {
case 0:
_context5.n = 1;
return this.AWS.dynamodb.describeTimeToLive({
TableName: tableName
});
case 1:
response = _context5.v;
if (!response.TimeToLiveDescription.AttributeName) {
_context5.n = 3;
break;
}
_context5.n = 2;
return this.AWS.dynamodb.updateTimeToLive({
TableName: tableName,
TimeToLiveSpecification: {
Enabled: false,
AttributeName: response.TimeToLiveDescription.AttributeName
}
});
case 2:
response = _context5.v;
case 3:
return _context5.a(2, response);
}
}, _callee5, this);
}));
function disableTimeToLive(_x5) {
return _disableTimeToLive.apply(this, arguments);
}
return disableTimeToLive;
}()
/**
* @param {string} tableName
* @param {object} params
*
* @returns {Promise<object>}
*/
)
}, {
key: "updateTimeToLive",
value: (function () {
var _updateTimeToLive = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(tableName, params) {
var response;
return _regenerator().w(function (_context6) {
while (1) switch (_context6.n) {
case 0:
_context6.n = 1;
return this.AWS.dynamodb.updateTimeToLive(_objectSpread({
TableName: tableName
}, params));
case 1:
response = _context6.v;
return _context6.a(2, response);
}
}, _callee6, this);
}));
function updateTimeToLive(_x6, _x7) {
return _updateTimeToLive.apply(this, arguments);
}
return updateTimeToLive;
}()
/**
* @param {string} tableName
*
* @returns {Promise<object>}
*/
)
}, {
key: "destroy",
value: (function () {
var _destroy = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(tableName) {
var options,
response,
_args7 = arguments;
return _regenerator().w(function (_context7) {
while (1) switch (_context7.n) {
case 0:
options = _args7.length > 1 && _args7[1] !== undefined ? _args7[1] : {};
_context7.n = 1;
return this.AWS.dynamodb.deleteTable({
TableName: tableName
}, options);
case 1:
response = _context7.v;
return _context7.a(2, response);
}
}, _callee7, this);
}));
function destroy(_x8) {
return _destroy.apply(this, arguments);
}
return destroy;
}()
/**
* @param {string} tableName
*
* @returns {Promise<object>}
*/
)
}, {
key: "truncate",
value: (function () {
var _truncate = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(tableName) {
var response;
return _regenerator().w(function (_context8) {
while (1) switch (_context8.n) {
case 0:
_context8.n = 1;
return this.describe(tableName);
case 1:
response = _context8.v;
_context8.n = 2;
return this.destroy(tableName);
case 2:
_context8.n = 3;
return this.create((0, _dynamodb2.constructSchema)(response.Table));
case 3:
return _context8.a(2, response);
}
}, _callee8, this);
}));
function truncate(_x9) {
return _truncate.apply(this, arguments);
}
return truncate;
}()
/**
* @param {string} tableName
* @param {object} params
*
* @returns {Promise<object>}
*/
)
}, {
key: "update",
value: (function () {
var _update = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(tableName, params) {
var response;
return _regenerator().w(function (_context9) {
while (1) switch (_context9.n) {
case 0:
_context9.n = 1;
return this.AWS.dynamodb.updateTable(_objectSpread({
TableName: tableName
}, params));
case 1:
response = _context9.v;
return _context9.a(2, response);
}
}, _callee9, this);
}));
function update(_x0, _x1) {
return _update.apply(this, arguments);
}
return update;
}()
/**
* @param {string} sourceTableName
* @param {string} targetTableName
* @param {DatabaseServiceProvider} DatabaseServiceProvider
*
* @returns {Promise}
*/
)
}], [{
key: "restore",
value: function restore(sourceTableName, targetTableName, DatabaseServiceProvider, AbortController) {
return _wrapAsyncGenerator(/*#__PURE__*/_regenerator().m(function _callee0() {
var abortSignal, _yield$_awaitAsyncGen, Table, params, schema, restoredItemCount, response, _response$Items, Items, _response$LastEvaluat, LastEvaluatedKey, _yield$_awaitAsyncGen2, _yield$_awaitAsyncGen3, ItemCount, TotalItemCount;
return _regenerator().w(function (_context0) {
while (1) switch (_context0.n) {
case 0:
abortSignal = AbortController.signal;
_context0.n = 1;
return _awaitAsyncGenerator(DatabaseServiceProvider.SOURCE.TableService.describe(sourceTableName, {
abortSignal: abortSignal
}));
case 1:
_yield$_awaitAsyncGen = _context0.v;
Table = _yield$_awaitAsyncGen.Table;
_context0.n = 2;
return _awaitAsyncGenerator(Promise.allSettled([DatabaseServiceProvider.TARGET.TableService.destroy(targetTableName, {
abortSignal: abortSignal
}), (0, _clientDynamodb.waitUntilTableNotExists)({
client: DatabaseServiceProvider.TARGET.AWS.dynamodb,
maxWaitTime: 60,
abortSignal: abortSignal,
maxDelay: 5,
minDelay: 1
}, {
TableName: targetTableName
})]));
case 2:
_context0.n = 3;
return _awaitAsyncGenerator(Promise.allSettled([DatabaseServiceProvider.TARGET.TableService.create(_objectSpread(_objectSpread({}, (0, _dynamodb2.constructSchema)(Table)), {}, {
TableName: targetTableName
}), {
abortSignal: abortSignal
}), (0, _clientDynamodb.waitUntilTableExists)({
client: DatabaseServiceProvider.TARGET.AWS.dynamodb,
maxWaitTime: 60,
abortSignal: abortSignal,
maxDelay: 5,
minDelay: 1
}, {
TableName: targetTableName
})]));
case 3:
params = {
Limit: 500
};
schema = Table.KeySchema.map(function (_ref) {
var AttributeName = _ref.AttributeName;
return AttributeName;
});
restoredItemCount = 0;
case 4:
_context0.n = 5;
return _awaitAsyncGenerator(DatabaseServiceProvider.SOURCE.ItemService.fetch(_dynamodb.OPERATIONS.SCAN, sourceTableName, params, {
abortSignal: abortSignal
}));
case 5:
response = _context0.v;
_response$Items = response.Items, Items = _response$Items === void 0 ? [] : _response$Items, _response$LastEvaluat = response.LastEvaluatedKey, LastEvaluatedKey = _response$LastEvaluat === void 0 ? null : _response$LastEvaluat; // eslint-disable-next-line no-await-in-loop
_context0.n = 6;
return _awaitAsyncGenerator(Promise.all(Items.map(function (item) {
return DatabaseServiceProvider.TARGET.ItemService.create(targetTableName, schema, item, {
abortSignal: abortSignal
});
})));
case 6:
restoredItemCount += Items.length;
_context0.n = 7;
return _awaitAsyncGenerator(DatabaseServiceProvider.compare(sourceTableName, targetTableName));
case 7:
_yield$_awaitAsyncGen2 = _context0.v;
_yield$_awaitAsyncGen3 = _slicedToArray(_yield$_awaitAsyncGen2, 2);
ItemCount = _yield$_awaitAsyncGen3[0];
TotalItemCount = _yield$_awaitAsyncGen3[1];
_context0.n = 8;
return [ItemCount || restoredItemCount, TotalItemCount];
case 8:
params.ExclusiveStartKey = LastEvaluatedKey;
case 9:
if (params.ExclusiveStartKey) {
_context0.n = 4;
break;
}
case 10:
return _context0.a(2);
}
}, _callee0);
}))();
}
}]);
}();