2
0

👷 Add daily database cleanup action

Closes #201
This commit is contained in:
Baptiste Arnaud
2023-01-10 11:39:56 +01:00
parent b142dc18eb
commit 4c2eaf9b79
4 changed files with 66 additions and 15 deletions

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