PHP 8.2.30
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

Name Size Perms Modified Actions
127 B lr--r--r-- 2026-03-14 01:49:04
Edit Download
1.52 KB lrw-r--r-- 2026-03-04 17:40:42
Edit Download
691 B lrw-r--r-- 2026-02-20 19:24:55
Edit Download
2.42 KB lrw-r--r-- 2026-02-05 17:27:01
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).