27
packages/scripts/cleanDatabase.ts
Normal file
27
packages/scripts/cleanDatabase.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { PrismaClient } from 'db'
|
||||
import { promptAndSetEnvironment } from './utils'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export const cleanDatabase = async () => {
|
||||
await promptAndSetEnvironment('production')
|
||||
|
||||
console.log('Starting database cleanup...')
|
||||
await deleteOldChatSessions()
|
||||
console.log('Done!')
|
||||
}
|
||||
|
||||
const deleteOldChatSessions = async () => {
|
||||
const threeDaysAgo = new Date()
|
||||
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3)
|
||||
const { count } = await prisma.chatSession.deleteMany({
|
||||
where: {
|
||||
updatedAt: {
|
||||
lte: threeDaysAgo,
|
||||
},
|
||||
},
|
||||
})
|
||||
console.log(`Deleted ${count} old chat sessions.`)
|
||||
}
|
||||
|
||||
cleanDatabase().then()
|
||||
Reference in New Issue
Block a user