Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adds scripts to resave documents #379

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
60 changes: 60 additions & 0 deletions src/resave-collections.ts
Original file line number Diff line number Diff line change
@@ -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()
87 changes: 87 additions & 0 deletions src/resave-duplicate-documents.ts
Original file line number Diff line number Diff line change
@@ -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()