UNPKG

dynolink

Version:
41 lines (40 loc) 1.63 kB
"use strict"; // infra/ensure-table.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.ensureTable = ensureTable; const client_dynamodb_1 = require("@aws-sdk/client-dynamodb"); const table_definitions_1 = require("./table-definitions"); const decorators_1 = require("../core/decorators"); /** * Ensures that the table for the given entity exists in DynamoDB. * @param entity * @param client */ async function ensureTable(entity, client) { const metadata = (0, decorators_1.getEntityMetadata)(entity); const tableName = metadata?.tableName; if (!tableName) throw new Error('Missing @Table decorator'); const existing = await client.send(new client_dynamodb_1.ListTablesCommand({})); if (existing.TableNames?.includes(tableName)) { console.log(`✅ Table "${tableName}" already exists`); } else { const input = (0, table_definitions_1.generateUnifiedCreateTableInput)([entity]); await client.send(new client_dynamodb_1.CreateTableCommand(input)); console.log(`✅ Created table "${tableName}"`); } // Enable TTL if `ttl` field is defined const hasTTL = [...metadata.attributes.entries()].find(([name]) => name === 'ttl'); if (hasTTL) { await client.send(new client_dynamodb_1.UpdateTimeToLiveCommand({ TableName: tableName, TimeToLiveSpecification: { AttributeName: 'ttl', Enabled: true, }, })); console.log(`✅ TTL enabled on "${tableName}" using 'ttl' attribute`); } console.log(`✅ Created table "${tableName}"`); }