Preview: exportDb.js
Size: 1.52 KB
//home/byroehnu/easepay.easetack.com/scripts/exportDb.js
const fs = require('fs');
const path = require('path');
const { PrismaClient, Prisma } = require('@prisma/client');
const prisma = new PrismaClient();
async function exportDatabase() {
console.log('🔄 Starting database export...');
const exportData = {};
try {
// Dynamically get all model names from Prisma schema
const models = Prisma.dmmf.datamodel.models.map(m => m.name);
for (const modelName of models) {
// Prisma methods are camelCased model names
const delegateName = modelName.charAt(0).toLowerCase() + modelName.slice(1);
console.log(`📦 Exporting table: ${modelName}...`);
if (prisma[delegateName] && typeof prisma[delegateName].findMany === 'function') {
try {
const records = await prisma[delegateName].findMany();
exportData[modelName] = records;
console.log(` └─ ${records.length} records exported.`);
} catch (err) {
console.error(` └─ ❌ Error reading ${modelName}:`, err.message);
}
}
}
// Write all data to a single JSON file
const outputPath = path.join(__dirname, '..', 'database_export.json');
fs.writeFileSync(outputPath, JSON.stringify(exportData, null, 2));
console.log(`\n✅ Database successfully exported!`);
console.log(`📁 File saved at: ${outputPath}`);
} catch (error) {
console.error('❌ Failed to export database:', error);
} finally {
await prisma.$disconnect();
}
}
exportDatabase();
Directory Contents
Dirs: 0 × Files: 4