Files
afrov2/scripts/backfill-slugs.ts
streap2 d1e551bcca
All checks were successful
Build and Push App / build (push) Successful in 9m12s
feat: implement SEO enhancements, automatic slug generation, and font optimizations
2026-04-26 08:11:31 +02:00

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()
})