backend/config/setup/index.ts
creations 320e2cc121
All checks were successful
Code quality checks / biome (push) Successful in 7s
Add guild routes, setup tables, validators, and update environment example
Added routes for guild creation, deletion, joining, leaving, and invites
Set up Cassandra tables for guilds, invites, and members
Added validators for guild input
Updated .env.example with required config values
2025-05-03 13:56:57 -04:00

77 lines
2 KiB
TypeScript

import { readdir, stat } from "node:fs/promises";
import { extname, join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { cassandra as cassandraConfig, verifyRequiredVariables } from "@config";
import { logger } from "@creations.works/logger";
import { cassandra } from "@lib/cassandra";
async function loadTables(dir: string): Promise<void> {
const entries = await readdir(dir);
for (const entry of entries) {
const fullPath = join(dir, entry);
const stats = await stat(fullPath);
if (stats.isDirectory()) {
await loadTables(fullPath);
continue;
}
if (extname(entry) !== ".ts") continue;
const modulePath = pathToFileURL(fullPath).href;
const mod = await import(modulePath);
if (typeof mod.default === "function") {
await mod.default();
logger.info(`Ran default export from ${fullPath}`);
} else if (typeof mod.createTable === "function") {
await mod.createTable();
logger.info(`Ran createTable from ${fullPath}`);
} else {
logger.warn(`No callable export found in ${fullPath}`);
}
}
}
async function setup(): Promise<void> {
verifyRequiredVariables();
await cassandra.connect({ withKeyspace: false });
const client = cassandra.getClient();
const keyspace = cassandraConfig.keyspace;
if (!keyspace) {
logger.error("No Cassandra keyspace configured in environment.");
process.exit(1);
}
await client.execute(`
CREATE KEYSPACE IF NOT EXISTS ${keyspace}
WITH REPLICATION = {
'class': 'SimpleStrategy',
'replication_factor': 1
};
`);
await client.execute(`USE ${keyspace}`);
logger.info(`Keyspace "${keyspace}" ensured and selected.`);
const tablesDir = resolve("config", "setup", "tables");
await loadTables(tablesDir);
logger.info("Setup complete.");
}
setup()
.catch((error: Error) => {
logger.error(error);
process.exit(1);
})
.finally(() => {
cassandra.shutdown().catch((error: Error) => {
logger.error(["Error shutting down Cassandra client:", error]);
});
process.exit(0);
});