From 9cdcacfbb54825b239eac7b69b67230e320c566c Mon Sep 17 00:00:00 2001 From: Patrik Kozak Date: Tue, 8 Oct 2024 11:31:58 -0400 Subject: [PATCH 1/2] chore: adds scripts to resave documents --- package.json | 4 +- src/resave-collections.ts | 60 +++++++++++++++++++++ src/resave-duplicate-documents.ts | 87 +++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/resave-collections.ts create mode 100644 src/resave-duplicate-documents.ts diff --git a/package.json b/package.json index c087769cb..70b62e5cb 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,9 @@ "lint": "next lint", "prettier:fix": "prettier --write .", "migrate-lexical-script": "payload run ./src/migrate.ts", - "migrate-posts": "payload run ./src/migrate-posts-to-lexical.ts" + "migrate-posts": "payload run ./src/migrate-posts-to-lexical.ts", + "resave-collections": "payload run ./src/resave-collections.ts", + "resave-duplicate-documents": "payload run ./src/resave-duplicate-documents.ts" }, "dependencies": { "@docsearch/react": "^3.6.0", diff --git a/src/resave-collections.ts b/src/resave-collections.ts new file mode 100644 index 000000000..443223c17 --- /dev/null +++ b/src/resave-collections.ts @@ -0,0 +1,60 @@ +import config from '@payload-config' +import { getPayload } from 'payload' + +const resaveCollections = async () => { + const payload = await getPayload({ config }) + + type CollectionSlug = 'case-studies' | 'community-help' | 'docs' | 'media' | 'pages' | 'partners' | 'posts' | 'reusable-content' | 'users'; + + const collectionSlugs: CollectionSlug[] = [ + 'community-help', + 'case-studies', + 'docs', + 'media', + 'pages', + 'partners', + 'posts', + 'reusable-content', + 'users' + ] + + try { + // Loop through each collection + for (const collectionSlug of collectionSlugs) { + const results = await payload.find({ + collection: collectionSlug, + depth: 0, + }) + + // eslint-disable-next-line no-console + console.log(`Resaving collection: ${collectionSlug} with ${results.totalDocs} documents`) + + await Promise.all(results.docs.map(async (result) => { + const { id } = result + + try { + await payload.update({ + id, + collection: collectionSlug, + data: {}, + }) + + // eslint-disable-next-line no-console + console.log(`Document in '${collectionSlug}' with id '${id}' updated successfully`) + } catch (e) { + payload.logger.error(`Document in '${collectionSlug}' with id '${id}' failed to update`) + payload.logger.error(e) + } + })) + } + } catch (e) { + payload.logger.error('Something went wrong.') + payload.logger.error(e) + } + + // eslint-disable-next-line no-console + console.log('Complete') + process.exit(0) +} + +void resaveCollections() \ No newline at end of file diff --git a/src/resave-duplicate-documents.ts b/src/resave-duplicate-documents.ts new file mode 100644 index 000000000..c271bb90c --- /dev/null +++ b/src/resave-duplicate-documents.ts @@ -0,0 +1,87 @@ +import config from '@payload-config' +import { getPayload } from 'payload' + +const resaveDuplicateDocuments = async () => { + const payload = await getPayload({ config }) + + type CollectionSlug = 'case-studies' | 'community-help' | 'docs' | 'media' | 'pages' | 'partners' | 'posts' | 'reusable-content' | 'users'; + + const collectionSlugs: CollectionSlug[] = [ + 'community-help', + 'case-studies', + 'docs', + 'media', + 'pages', + 'partners', + 'posts', + 'reusable-content', + 'users' + ] + + try { + // Loop through each collection + for (const collectionSlug of collectionSlugs) { + const results = await payload.find({ + collection: collectionSlug, + depth: 0, + }) + + // eslint-disable-next-line no-console + console.log(`Checking collection: ${collectionSlug} with ${results.totalDocs} documents`) + + // Find duplicate ids + const idMap = new Map() + results.docs.forEach(doc => { + if (idMap.has(doc.id)) { + idMap.set(doc.id, idMap.get(doc.id) + 1) + } else { + idMap.set(doc.id, 1) + } + }) + + // Filter only the duplicate ids + const duplicateIds = Array.from(idMap.entries()) + .filter(([id, count]) => count > 1) + .map(([id]) => id) + + if (duplicateIds.length > 0) { + + // eslint-disable-next-line no-console + console.log(`Found ${duplicateIds.length} duplicate IDs in '${collectionSlug}'. Resaving documents...`) + + await Promise.all(results.docs + .filter(doc => duplicateIds.includes(doc.id)) + .map(async (result) => { + const { id } = result + + try { + // await payload.update({ + // id, + // collection: collectionSlug, + // data: {}, + // }) + + // eslint-disable-next-line no-console + console.log(`Document in '${collectionSlug}' with duplicate id '${id}' updated successfully`) + } catch (e) { + payload.logger.error(`Document in '${collectionSlug}' with duplicate id '${id}' failed to update`) + payload.logger.error(e) + } + }) + ) + } else { + // eslint-disable-next-line no-console + console.log(`No duplicate IDs found in '${collectionSlug}'`) + } + } + } catch (e) { + payload.logger.error('Something went wrong.') + payload.logger.error(e) + } + + // eslint-disable-next-line no-console + console.log('Complete') + process.exit(0) +} + +void resaveDuplicateDocuments() \ No newline at end of file From 01ff07e1b9002489f907a47b81682d75b794a7dd Mon Sep 17 00:00:00 2001 From: Patrik Kozak Date: Tue, 8 Oct 2024 11:35:27 -0400 Subject: [PATCH 2/2] chore: uncomments update callin duplicate document script --- src/resave-duplicate-documents.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/resave-duplicate-documents.ts b/src/resave-duplicate-documents.ts index c271bb90c..4a26fd6a4 100644 --- a/src/resave-duplicate-documents.ts +++ b/src/resave-duplicate-documents.ts @@ -55,11 +55,11 @@ const resaveDuplicateDocuments = async () => { const { id } = result try { - // await payload.update({ - // id, - // collection: collectionSlug, - // data: {}, - // }) + await payload.update({ + id, + collection: collectionSlug, + data: {}, + }) // eslint-disable-next-line no-console console.log(`Document in '${collectionSlug}' with duplicate id '${id}' updated successfully`)