47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import prisma from '../lib/prisma'
|
|
import { generateSlug } from '../lib/utils'
|
|
|
|
async function main() {
|
|
const posts = await prisma.blogPost.findMany({
|
|
where: {
|
|
OR: [
|
|
{ slug: null },
|
|
{ slug: '' }
|
|
]
|
|
}
|
|
})
|
|
|
|
console.log(`Found ${posts.length} posts without slug.`)
|
|
|
|
for (const post of posts) {
|
|
const newSlug = generateSlug(post.title)
|
|
|
|
// Handle potential duplicates
|
|
let slug = newSlug
|
|
let count = 1
|
|
let unique = false
|
|
|
|
while (!unique) {
|
|
const existing = await prisma.blogPost.findUnique({ where: { slug } })
|
|
if (!existing || existing.id === post.id) {
|
|
unique = true
|
|
} else {
|
|
slug = `${newSlug}-${count}`
|
|
count++
|
|
}
|
|
}
|
|
|
|
await prisma.blogPost.update({
|
|
where: { id: post.id },
|
|
data: { slug }
|
|
})
|
|
console.log(`Updated post: ${post.title} -> ${slug}`)
|
|
}
|
|
}
|
|
|
|
main()
|
|
.catch(console.error)
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
})
|