/** * Export every document from your survey's Firestore collections into a * single JSON file that the analysis pipeline can read. * * Output structure (matches what analysis/01_data_prep/02_convert_main_json_to_csv.py expects): * * { * "participants": { "": { ...fields }, ... }, * "responses": { "": { ...fields }, ... }, * "system": { "": { ...fields }, ... } * } * * Setup: * 1) Generate a service-account key for your Firebase project: * Firebase console → ⚙️ Project settings → Service accounts * → Generate new private key → save the JSON. * Put it at ./service-account.json (gitignored). * 2) Install dependencies: * npm install * 3) Run: * node tools/export-firestore.js * Optional flags: * --service-account default: ./service-account.json * --out default: data/your_study/raw/firebase_export.json * --collections default: participants,responses,system * * The exported JSON is then consumed by: * ANALYSIS_DATA_DIR=data/your_study python3 analysis/01_data_prep/02_convert_main_json_to_csv.py */ const admin = require("firebase-admin"); const fs = require("fs").promises; const path = require("path"); function parseArgs(argv) { const args = { serviceAccount: "./service-account.json", out: "data/your_study/raw/firebase_export.json", collections: ["participants", "responses", "system"], }; for (let i = 0; i < argv.length; i++) { const a = argv[i]; if (a === "--service-account") args.serviceAccount = argv[++i]; else if (a === "--out") args.out = argv[++i]; else if (a === "--collections") args.collections = argv[++i].split(","); else if (a === "-h" || a === "--help") { console.log(`Usage: node tools/export-firestore.js [--service-account ] [--out ] [--collections a,b,c]`); process.exit(0); } } return args; } async function exportCollection(db, name) { const out = {}; let lastDoc = null; const batchSize = 200; while (true) { let q = db.collection(name).limit(batchSize); if (lastDoc) q = q.startAfter(lastDoc); const snap = await q.get(); if (snap.empty) break; snap.forEach((doc) => { out[doc.id] = doc.data(); }); lastDoc = snap.docs[snap.docs.length - 1]; if (snap.size < batchSize) break; } return out; } async function main() { const args = parseArgs(process.argv.slice(2)); const saPath = path.resolve(args.serviceAccount); let serviceAccount; try { serviceAccount = require(saPath); } catch (e) { console.error( `\nCould not load service-account JSON at ${saPath}.\n` + `Generate one in Firebase console → ⚙️ Project settings → Service accounts ` + `→ Generate new private key, save it as service-account.json next to ` + `this script, then re-run.\n` ); process.exit(1); } admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); const db = admin.firestore(); console.log(`Exporting from project '${serviceAccount.project_id}'...`); const bundle = {}; for (const col of args.collections) { process.stdout.write(` ${col} ... `); bundle[col] = await exportCollection(db, col); console.log(`${Object.keys(bundle[col]).length} doc(s)`); } const outPath = path.resolve(args.out); await fs.mkdir(path.dirname(outPath), { recursive: true }); await fs.writeFile(outPath, JSON.stringify(bundle, null, 2)); console.log(`\nWrote ${outPath}`); console.log( `Next: ANALYSIS_DATA_DIR=${path.relative(process.cwd(), path.dirname(path.dirname(outPath)))} ` + `python3 analysis/01_data_prep/02_convert_main_json_to_csv.py` ); process.exit(0); } main().catch((e) => { console.error("Export failed:", e); process.exit(1); });