UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

86 lines (85 loc) 4.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Synchronizer = void 0; const database_js_1 = require("../../../database/database.js"); const index_js_1 = require("../../../entity/actions/dto/index.js"); const index_js_2 = require("../../../table/actions/dto/index.js"); const constants_js_1 = require("./constants.js"); const deleteEntity_js_1 = require("./deleteEntity.js"); const getTableEntityNames_js_1 = require("./getTableEntityNames.js"); const putAccessRole_js_1 = require("./putAccessRole.js"); const putAwsAccount_js_1 = require("./putAwsAccount.js"); const putEntity_js_1 = require("./putEntity.js"); const putTable_js_1 = require("./putTable.js"); class Synchronizer extends database_js_1.DatabaseAction { constructor(database, awsAccount) { super(database); this.apiUrl = 'https://api.dynamodb-toolshack.com'; this[constants_js_1.$awsAccount] = awsAccount; } awsAccount(awsAccount) { return new Synchronizer(this.database, awsAccount); } async sync({ apiKey, deleteUnknownEntities = false, fetch: _fetch = fetch }) { var _a, _b, _c, _d, _e; const fetchOpts = { apiUrl: this.apiUrl, fetch: _fetch, apiKey }; const awsAccount = this[constants_js_1.$awsAccount]; if (awsAccount === undefined) { throw new Error('Synchronizer incomplete: Missing "awsConfig" property'); } const { awsAccountId, awsRegion, title: awsAccountTitle = String(awsAccountId), color: awsAccountColor = 'blue', description: awsAccountDescription } = awsAccount; await (0, putAwsAccount_js_1.putAWSAccount)({ awsAccountId, title: awsAccountTitle, color: awsAccountColor, description: awsAccountDescription }, fetchOpts); for (const registry of Object.values(this.database.tables)) { const { tableName, ...tableDTO } = registry.build(index_js_2.TableDTO).toJSON(); if (tableName === undefined) { throw new Error('tableName should be provided'); } const tableMetadata = registry.table.meta; await (0, putTable_js_1.putTable)({ tableName, ...awsAccount, ...tableDTO, title: tableMetadata.title, description: tableMetadata.description, icon: (_b = (_a = tableMetadata._ddbToolshack) === null || _a === void 0 ? void 0 : _a.icon) !== null && _b !== void 0 ? _b : 'database-zap' }, fetchOpts); const accessRole = (_c = tableMetadata._ddbToolshack) === null || _c === void 0 ? void 0 : _c.accessRole; if (accessRole !== undefined) { const { roleName } = accessRole; await (0, putAccessRole_js_1.putAccessRole)({ awsAccountId, ...accessRole }, fetchOpts); await (0, putAccessRole_js_1.assignAccessRole)({ awsAccountId, awsRegion, tableName, roleName }, fetchOpts); } let unknownEntityNames = undefined; if (deleteUnknownEntities) { const tableEntityNames = await (0, getTableEntityNames_js_1.getTableEntityNames)({ awsAccountId, awsRegion, tableName }, fetchOpts); unknownEntityNames = new Set(tableEntityNames); } for (const entity of Object.values(registry.entities)) { const entityDTO = entity.build(index_js_1.EntityDTO).toJSON(); const entityMetadata = entity.meta; await (0, putEntity_js_1.putEntity)({ awsAccountId, awsRegion, tableName, ...entityDTO, title: entityMetadata.title, description: entityMetadata.description, icon: (_e = (_d = entityMetadata._ddbToolshack) === null || _d === void 0 ? void 0 : _d.icon) !== null && _e !== void 0 ? _e : 'database-zap' }, fetchOpts); unknownEntityNames === null || unknownEntityNames === void 0 ? void 0 : unknownEntityNames.delete(entity.entityName); } if (deleteUnknownEntities && unknownEntityNames !== undefined) { for (const entityName of unknownEntityNames) { await (0, deleteEntity_js_1.deleteEntity)({ awsAccountId, awsRegion, tableName, entityName }, fetchOpts); } } } } } exports.Synchronizer = Synchronizer; Synchronizer.actionName = 'synchronize';