UNPKG

dynamodb-toolbox

Version:

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

82 lines (81 loc) 4.18 kB
import { DatabaseAction } from '../../../database/database.js'; import { EntityDTO } from '../../../entity/actions/dto/index.js'; import { TableDTO } from '../../../table/actions/dto/index.js'; import { $awsAccount } from './constants.js'; import { deleteEntity } from './deleteEntity.js'; import { getTableEntityNames } from './getTableEntityNames.js'; import { assignAccessRole, putAccessRole } from './putAccessRole.js'; import { putAWSAccount } from './putAwsAccount.js'; import { putEntity } from './putEntity.js'; import { putTable } from './putTable.js'; export class Synchronizer extends DatabaseAction { constructor(database, awsAccount) { super(database); this.apiUrl = 'https://api.dynamodb-toolshack.com'; this[$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[$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 putAWSAccount({ awsAccountId, title: awsAccountTitle, color: awsAccountColor, description: awsAccountDescription }, fetchOpts); for (const registry of Object.values(this.database.tables)) { const { tableName, ...tableDTO } = registry.build(TableDTO).toJSON(); if (tableName === undefined) { throw new Error('tableName should be provided'); } const tableMetadata = registry.table.meta; await 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 putAccessRole({ awsAccountId, ...accessRole }, fetchOpts); await assignAccessRole({ awsAccountId, awsRegion, tableName, roleName }, fetchOpts); } let unknownEntityNames = undefined; if (deleteUnknownEntities) { const tableEntityNames = await getTableEntityNames({ awsAccountId, awsRegion, tableName }, fetchOpts); unknownEntityNames = new Set(tableEntityNames); } for (const entity of Object.values(registry.entities)) { const entityDTO = entity.build(EntityDTO).toJSON(); const entityMetadata = entity.meta; await 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 deleteEntity({ awsAccountId, awsRegion, tableName, entityName }, fetchOpts); } } } } } Synchronizer.actionName = 'synchronize';