From c7d450ecf5780944010964d96c36431d426e1dc7 Mon Sep 17 00:00:00 2001 From: Courey Elliott Date: Fri, 13 Dec 2024 12:44:02 -0500 Subject: [PATCH] DO NOT MERGE --- app/backfill-synonym-slugs.ts | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 app/backfill-synonym-slugs.ts diff --git a/app/backfill-synonym-slugs.ts b/app/backfill-synonym-slugs.ts new file mode 100644 index 000000000..29e4c8dea --- /dev/null +++ b/app/backfill-synonym-slugs.ts @@ -0,0 +1,55 @@ +import { tables } from '@architect/functions' +import type { DynamoDBDocument } from '@aws-sdk/lib-dynamodb' +import { paginateScan } from '@aws-sdk/lib-dynamodb' +import { slug } from 'github-slugger' + +import type { Synonym } from '~/routes/synonyms/synonyms.lib' + +function* chunks(arr: T[], n: number): Generator { + for (let i = 0; i < arr.length; i += n) { + yield arr.slice(i, i + n) + } +} + +export async function backfillSynonyms() { + console.log('Starting SYNONYM SLUG backfill...') + const db = await tables() + const client = db._doc as unknown as DynamoDBDocument + const TableName = db.name('synonyms') + const pages = paginateScan( + { client }, + { + TableName, + } + ) + + for await (const page of pages) { + const chunked = [...chunks(page.Items || [], 25)] + const synonymsToUpdate = [] as Synonym[] + for (const chunk of chunked) { + for (const record of chunk) { + const synonym = record as unknown as Synonym + if (!synonym.slug) { + synonymsToUpdate.push(synonym) + } + } + if (synonymsToUpdate.length > 0) { + await client.batchWrite({ + RequestItems: { + [TableName]: synonymsToUpdate.map((synonym) => ({ + PutRequest: { + Item: { + synonymId: synonym.synonymId, + eventId: synonym.eventId, + slug: slug(synonym.eventId), + }, + }, + })), + }, + }) + console.log(`updated ${synonymsToUpdate.length} records`) + } + } + } + console.log('... End SYNONYM SLUG backfill') +}