From 1d2fb461c8634187b7225c66506243bc973f196b Mon Sep 17 00:00:00 2001 From: pepermao Date: Fri, 13 Oct 2023 20:36:00 +0200 Subject: [PATCH] create migration which moves extra properties to the props field --- ...1013183353-move-source-extra-properties.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 migrations/20231013183353-move-source-extra-properties.ts diff --git a/migrations/20231013183353-move-source-extra-properties.ts b/migrations/20231013183353-move-source-extra-properties.ts new file mode 100644 index 000000000..c95cb074f --- /dev/null +++ b/migrations/20231013183353-move-source-extra-properties.ts @@ -0,0 +1,30 @@ +import { Db } from "mongodb"; + +export async function up(db: Db) { + const sourcesCursor = await db.collection("sources").find(); + + while (await sourcesCursor.hasNext()) { + const source = await sourcesCursor.next(); + const { _id, user, href, targetId } = source; + + const updateData: any = { + _id, + targetId, + href, + user, + props: {}, + }; + + const copyPropsIfExist = (key) => { + if (source[key] || source.props?.[key]) { + updateData.props[key] = source[key] || source.props[key]; + } + }; + + copyPropsIfExist("targetReference"); + copyPropsIfExist("targetText"); + copyPropsIfExist("textRange"); + + await db.collection("sources").update({ _id: source._id }, updateData); + } +}