From d0cf0337633bc93b622a7d80e58d7888fff173d2 Mon Sep 17 00:00:00 2001 From: zkldi Date: Tue, 4 May 2021 16:27:02 +0100 Subject: [PATCH] create scripts to migrate folders and support spread --- .../__KT_DATABASE_MIGRATION/folders.ts | 44 +++++++++++++++++++ .../__KT_DATABASE_MIGRATION/migrate.ts | 2 +- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 server/scripts/__KT_DATABASE_MIGRATION/folders.ts diff --git a/server/scripts/__KT_DATABASE_MIGRATION/folders.ts b/server/scripts/__KT_DATABASE_MIGRATION/folders.ts new file mode 100644 index 000000000..cdb8e807a --- /dev/null +++ b/server/scripts/__KT_DATABASE_MIGRATION/folders.ts @@ -0,0 +1,44 @@ +import { FolderDocument, Game } from "kamaitachi-common"; +import { validPlaytypes, gameHuman } from "kamaitachi-common/js/config"; +import db from "../../src/db/db"; +import CreateLogCtx from "../../src/logger"; +import MigrateRecords from "./migrate"; +import crypto from "crypto"; + +const logger = CreateLogCtx("folders.ts"); + +function ConvertFn(c: any): FolderDocument[] { + let docs = []; + + for (const playtype of validPlaytypes[c.game as Game]) { + const fd: FolderDocument = { + title: c.title, + game: c.game, + playtype, + type: "query" as const, + query: { + collection: c.query.collection, + body: c.query.query, + }, + folderID: crypto.randomBytes(20).toString("hex"), + table: c.table, + tableIndex: 0, // ??? + }; + + let gh = gameHuman[c.game as Game]; + + fd.title = fd.title.replace(new RegExp(`${gh} +`, "gi"), ""); + + logger.info(`Porting folder ${fd.title} (${playtype})`); + + docs.push(fd); + } + + return docs; +} + +(async () => { + await MigrateRecords(db.folders, "folders", ConvertFn); + + process.exit(0); +})(); diff --git a/server/scripts/__KT_DATABASE_MIGRATION/migrate.ts b/server/scripts/__KT_DATABASE_MIGRATION/migrate.ts index a19820fa2..366919fd8 100644 --- a/server/scripts/__KT_DATABASE_MIGRATION/migrate.ts +++ b/server/scripts/__KT_DATABASE_MIGRATION/migrate.ts @@ -42,7 +42,7 @@ export default async function MigrateRecords( }); logger.info(`Inserting ${newDocuments.length} documents.`); - await existingCollection.insert(newDocuments); + await existingCollection.insert(newDocuments.flat()); logger.info(`Done!`); process.exit(0);