import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { await prisma.$transaction( async tx => { const messages = await tx.message.findMany(); for (const message of messages) { const { responseBody, response, result } = message; let newResponseBody = responseBody; let newResult = result; if (responseBody && result) { continue; } if (!responseBody && response) { console.log('update response body', message.id); newResponseBody = response .split('\n') .filter(chunk => !!chunk.trim()) .map(chunk => JSON.parse(chunk)) .filter( body => body.type !== 'final_error' && body.type !== 'final_code', ); } if (!result && response) { console.log('update response result', message.id); newResult = response .split('\n') .filter(chunk => !!chunk.trim()) .map(chunk => JSON.parse(chunk)) ?.find( body => body.type === 'final_error' || body.type === 'final_code', ); } await tx.message.update({ where: { id: message.id }, data: { ...(newResult ? { result: newResult } : {}), ...(newResponseBody ? { responseBody: newResponseBody } : {}), }, }); } }, { timeout: 30000 }, ); } main() .catch(async e => { console.error(e); await prisma.$disconnect(); process.exit(1); }) .finally(async () => { console.log('finished'); await prisma.$disconnect(); });