Creates a new TypeScript script `scripts/export-data.ts` that queries all tables from the database (sessions, users, mediaOutlets, articles, predictionMarkets, auctions, bids, mediaOutletRequests, comments, predictionBets) and writes the data to a JSON file. Replit-Commit-Author: Agent Replit-Commit-Session-Id: aabe2db1-f078-4501-aab5-be145ebc6b9a Replit-Commit-Checkpoint-Type: intermediate_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/3df548ff-50ae-432f-9be4-25d34eccc983/aabe2db1-f078-4501-aab5-be145ebc6b9a/TqVS1Ec
154 lines
4.9 KiB
TypeScript
154 lines
4.9 KiB
TypeScript
import { db } from "../server/db";
|
|
import {
|
|
sessions,
|
|
users,
|
|
mediaOutlets,
|
|
articles,
|
|
predictionMarkets,
|
|
auctions,
|
|
bids,
|
|
mediaOutletRequests,
|
|
comments,
|
|
predictionBets,
|
|
} from "../shared/schema";
|
|
import { writeFileSync } from "fs";
|
|
import { join } from "path";
|
|
|
|
interface ExportData {
|
|
exportDate: string;
|
|
version: "1.0.0";
|
|
tables: {
|
|
sessions: any[];
|
|
users: any[];
|
|
mediaOutlets: any[];
|
|
articles: any[];
|
|
predictionMarkets: any[];
|
|
auctions: any[];
|
|
bids: any[];
|
|
mediaOutletRequests: any[];
|
|
comments: any[];
|
|
predictionBets: any[];
|
|
};
|
|
metadata: {
|
|
totalRecords: number;
|
|
tableStats: Record<string, number>;
|
|
};
|
|
}
|
|
|
|
async function exportData() {
|
|
console.log("🚀 Starting data export...\n");
|
|
|
|
try {
|
|
// Export data from all tables in dependency order
|
|
console.log("📊 Exporting sessions...");
|
|
const sessionsData = await db.select().from(sessions);
|
|
console.log(` ✅ Exported ${sessionsData.length} sessions`);
|
|
|
|
console.log("📊 Exporting users...");
|
|
const usersData = await db.select().from(users);
|
|
console.log(` ✅ Exported ${usersData.length} users`);
|
|
|
|
console.log("📊 Exporting media outlets...");
|
|
const mediaOutletsData = await db.select().from(mediaOutlets);
|
|
console.log(` ✅ Exported ${mediaOutletsData.length} media outlets`);
|
|
|
|
console.log("📊 Exporting articles...");
|
|
const articlesData = await db.select().from(articles);
|
|
console.log(` ✅ Exported ${articlesData.length} articles`);
|
|
|
|
console.log("📊 Exporting prediction markets...");
|
|
const predictionMarketsData = await db.select().from(predictionMarkets);
|
|
console.log(` ✅ Exported ${predictionMarketsData.length} prediction markets`);
|
|
|
|
console.log("📊 Exporting auctions...");
|
|
const auctionsData = await db.select().from(auctions);
|
|
console.log(` ✅ Exported ${auctionsData.length} auctions`);
|
|
|
|
console.log("📊 Exporting bids...");
|
|
const bidsData = await db.select().from(bids);
|
|
console.log(` ✅ Exported ${bidsData.length} bids`);
|
|
|
|
console.log("📊 Exporting media outlet requests...");
|
|
const mediaOutletRequestsData = await db.select().from(mediaOutletRequests);
|
|
console.log(` ✅ Exported ${mediaOutletRequestsData.length} media outlet requests`);
|
|
|
|
console.log("📊 Exporting comments...");
|
|
const commentsData = await db.select().from(comments);
|
|
console.log(` ✅ Exported ${commentsData.length} comments`);
|
|
|
|
console.log("📊 Exporting prediction bets...");
|
|
const predictionBetsData = await db.select().from(predictionBets);
|
|
console.log(` ✅ Exported ${predictionBetsData.length} prediction bets`);
|
|
|
|
// Prepare export data
|
|
const totalRecords =
|
|
sessionsData.length +
|
|
usersData.length +
|
|
mediaOutletsData.length +
|
|
articlesData.length +
|
|
predictionMarketsData.length +
|
|
auctionsData.length +
|
|
bidsData.length +
|
|
mediaOutletRequestsData.length +
|
|
commentsData.length +
|
|
predictionBetsData.length;
|
|
|
|
const exportData: ExportData = {
|
|
exportDate: new Date().toISOString(),
|
|
version: "1.0.0",
|
|
tables: {
|
|
sessions: sessionsData,
|
|
users: usersData,
|
|
mediaOutlets: mediaOutletsData,
|
|
articles: articlesData,
|
|
predictionMarkets: predictionMarketsData,
|
|
auctions: auctionsData,
|
|
bids: bidsData,
|
|
mediaOutletRequests: mediaOutletRequestsData,
|
|
comments: commentsData,
|
|
predictionBets: predictionBetsData,
|
|
},
|
|
metadata: {
|
|
totalRecords,
|
|
tableStats: {
|
|
sessions: sessionsData.length,
|
|
users: usersData.length,
|
|
mediaOutlets: mediaOutletsData.length,
|
|
articles: articlesData.length,
|
|
predictionMarkets: predictionMarketsData.length,
|
|
auctions: auctionsData.length,
|
|
bids: bidsData.length,
|
|
mediaOutletRequests: mediaOutletRequestsData.length,
|
|
comments: commentsData.length,
|
|
predictionBets: predictionBetsData.length,
|
|
},
|
|
},
|
|
};
|
|
|
|
// Generate filename with timestamp
|
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').split('T')[0];
|
|
const filename = `data-export-${timestamp}.json`;
|
|
const filepath = join(process.cwd(), filename);
|
|
|
|
// Write to file
|
|
console.log(`\n💾 Writing data to ${filename}...`);
|
|
writeFileSync(filepath, JSON.stringify(exportData, null, 2), 'utf-8');
|
|
|
|
console.log(`\n✅ Export completed successfully!`);
|
|
console.log(`\n📁 File saved to: ${filepath}`);
|
|
console.log(`📊 Total records exported: ${totalRecords}`);
|
|
console.log(`\n📈 Table breakdown:`);
|
|
Object.entries(exportData.metadata.tableStats).forEach(([table, count]) => {
|
|
console.log(` - ${table}: ${count} records`);
|
|
});
|
|
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error("\n❌ Error during export:", error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Run export
|
|
exportData();
|