backend/config/setup/teardown.ts
creations 0cb7ebb245
All checks were successful
Code quality checks / biome (push) Successful in 8s
add user info, add table drop for dev env, fix invite route
2025-05-04 08:59:36 -04:00

64 lines
1.6 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 dropTables(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 dropTables(fullPath);
continue;
}
if (extname(entry) !== ".ts") continue;
const modulePath = pathToFileURL(fullPath).href;
const mod = await import(modulePath);
if (typeof mod.dropTable === "function") {
await mod.dropTable();
logger.info(`Ran dropTable from ${fullPath}`);
} else {
logger.warn(`No dropTable export found in ${fullPath}`);
}
}
}
async function teardown(): Promise<void> {
verifyRequiredVariables();
await cassandra.connect({ withKeyspace: true });
const keyspace = cassandraConfig.keyspace;
if (!keyspace) {
logger.error("No Cassandra keyspace configured in environment.");
process.exit(1);
}
logger.info(`Dropping all tables in keyspace "${keyspace}"...`);
const tablesDir = resolve("config", "setup", "tables");
await dropTables(tablesDir);
logger.info("Teardown complete.");
}
teardown()
.catch((error) => {
logger.error(error);
process.exit(1);
})
.finally(async () => {
try {
await cassandra.shutdown();
} catch (error) {
logger.error(["Error shutting down Cassandra client:", error as Error]);
}
});