mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-01 11:28:13 +03:00
Move scripts into src/
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
// note: a hell of a lot of this code is copy pasted
|
||||
// this is just a script, so, feel free to refactor it.
|
||||
import { Command } from "commander";
|
||||
import { Databases, monkDB } from "external/mongo/db";
|
||||
import { DatabaseSchemas } from "external/mongo/schemas";
|
||||
import CreateLogCtx from "lib/logger/logger";
|
||||
import { PrudenceError } from "prudence";
|
||||
import { FormatPrError } from "utils/prudence";
|
||||
|
||||
const program = new Command();
|
||||
program.option("-c, --collection <collectionName>");
|
||||
|
||||
program.parse(process.argv);
|
||||
const options = program.opts();
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
|
||||
export async function ValidateCollection(collectionName: Databases): Promise<void> {
|
||||
const schemaRunner = DatabaseSchemas[collectionName];
|
||||
|
||||
const documents = await monkDB.get(collectionName).count({});
|
||||
|
||||
logger.info(`Validating ${collectionName}. This is ${documents} documents.
|
||||
`);
|
||||
let success = 0;
|
||||
let fails = 0;
|
||||
|
||||
await monkDB
|
||||
.get(collectionName)
|
||||
.find({})
|
||||
// @ts-expect-error faulty monk types
|
||||
.each((c) => {
|
||||
try {
|
||||
schemaRunner(c);
|
||||
success++;
|
||||
} catch (err) {
|
||||
logger.error(`[${collectionName}]: ${FormatPrError(err as PrudenceError)}`, c);
|
||||
fails++;
|
||||
}
|
||||
});
|
||||
|
||||
logger.info(`Success: ${success} (${(100 * success) / documents}%)`);
|
||||
logger.info(`Failures: ${fails} (${(100 * fails) / documents}%)`);
|
||||
}
|
||||
|
||||
export async function ValidateAllCollections() {
|
||||
for (const collectionName in DatabaseSchemas) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await ValidateCollection(collectionName as keyof typeof DatabaseSchemas);
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
if (options.collection) {
|
||||
ValidateCollection(options.collection).then(() => process.exit(0));
|
||||
} else {
|
||||
ValidateAllCollections().then(() => process.exit(0));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user