backend/config/setup/index.ts
creations 9d8b3eb969
All checks were successful
Code quality checks / biome (push) Successful in 8s
seperate all config files, move so i can just call @config
2025-05-03 08:00:36 -04:00

65 lines
1.7 KiB
TypeScript

import { readdir } 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 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");
const files = await readdir(tablesDir);
for (const file of files) {
if (extname(file) !== ".ts") continue;
const modulePath = pathToFileURL(join(tablesDir, file)).href;
const mod = await import(modulePath);
if (typeof mod.default === "function") {
await mod.default();
logger.info(`Ran default export from ${file}`);
} else if (typeof mod.createTable === "function") {
await mod.createTable();
logger.info(`Ran createTable from ${file}`);
} else {
logger.warn(`No callable export found in ${file}`);
}
}
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 as Error]);
});
process.exit(0);
});