lfg9-forums/setup-dev-tables.js
Developer 097d5c4109 init
2025-09-02 14:05:42 -05:00

128 lines
3.9 KiB
JavaScript

const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const { CreateTableCommand } = require('@aws-sdk/client-dynamodb');
const client = new DynamoDBClient({
region: process.env.AWS_REGION || 'us-east-1',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});
const tablePrefix = 'lfg9_forums_dev_';
const tables = [
{
name: `${tablePrefix}users`,
keySchema: [{ AttributeName: 'userId', KeyType: 'HASH' }],
attributeDefinitions: [{ AttributeName: 'userId', AttributeType: 'S' }],
},
{
name: `${tablePrefix}categories`,
keySchema: [{ AttributeName: 'categoryId', KeyType: 'HASH' }],
attributeDefinitions: [{ AttributeName: 'categoryId', AttributeType: 'S' }],
},
{
name: `${tablePrefix}threads`,
keySchema: [{ AttributeName: 'threadId', KeyType: 'HASH' }],
attributeDefinitions: [
{ AttributeName: 'threadId', AttributeType: 'S' },
{ AttributeName: 'categoryId', AttributeType: 'S' },
{ AttributeName: 'authorId', AttributeType: 'S' },
],
globalSecondaryIndexes: [
{
IndexName: 'CategoryIdIndex',
KeySchema: [{ AttributeName: 'categoryId', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
},
{
IndexName: 'AuthorIdIndex',
KeySchema: [{ AttributeName: 'authorId', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
},
],
},
{
name: `${tablePrefix}posts`,
keySchema: [{ AttributeName: 'postId', KeyType: 'HASH' }],
attributeDefinitions: [
{ AttributeName: 'postId', AttributeType: 'S' },
{ AttributeName: 'threadId', AttributeType: 'S' },
{ AttributeName: 'authorId', AttributeType: 'S' },
],
globalSecondaryIndexes: [
{
IndexName: 'ThreadIdIndex',
KeySchema: [{ AttributeName: 'threadId', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
},
{
IndexName: 'AuthorIdIndex',
KeySchema: [{ AttributeName: 'authorId', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
},
],
},
{
name: `${tablePrefix}files`,
keySchema: [{ AttributeName: 'fileId', KeyType: 'HASH' }],
attributeDefinitions: [
{ AttributeName: 'fileId', AttributeType: 'S' },
{ AttributeName: 'userId', AttributeType: 'S' },
],
globalSecondaryIndexes: [
{
IndexName: 'UserIdIndex',
KeySchema: [{ AttributeName: 'userId', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
},
],
},
];
async function createTable(tableConfig) {
const params = {
TableName: tableConfig.name,
KeySchema: tableConfig.keySchema,
AttributeDefinitions: tableConfig.attributeDefinitions,
BillingMode: 'PAY_PER_REQUEST',
};
if (tableConfig.globalSecondaryIndexes) {
params.GlobalSecondaryIndexes = tableConfig.globalSecondaryIndexes.map(gsi => ({
...gsi,
BillingMode: 'PAY_PER_REQUEST',
}));
}
try {
const command = new CreateTableCommand(params);
const result = await client.send(command);
console.log(`✅ Created table: ${tableConfig.name}`);
return result;
} catch (error) {
if (error.name === 'ResourceInUseException') {
console.log(`⚠️ Table ${tableConfig.name} already exists`);
} else {
console.error(`❌ Error creating table ${tableConfig.name}:`, error.message);
}
}
}
async function setupTables() {
console.log('🚀 Setting up DynamoDB tables for development...\n');
for (const table of tables) {
await createTable(table);
}
console.log('\n✨ Database setup complete!');
console.log('🔗 You can view your tables in the AWS Console:');
console.log('https://console.aws.amazon.com/dynamodb/home?region=us-east-1#tables:');
}
// Load environment variables from .env file
require('dotenv').config({ path: './backend/.env' });
setupTables().catch(console.error);