-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportFirestore.js
More file actions
36 lines (28 loc) · 1000 Bytes
/
exportFirestore.js
File metadata and controls
36 lines (28 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const fs = require('fs');
const path = require('path');
const { getFirebaseAdmin } = require('./firebaseAdmin');
const admin = getFirebaseAdmin();
const firestore = admin.firestore();
async function exportData() {
const backup = {};
const collections = await firestore.listCollections();
for (const collection of collections) {
console.log(`Exporting collection: ${collection.id}`);
backup[collection.id] = {};
const snapshot = await collection.get();
snapshot.forEach(doc => {
backup[collection.id][doc.id] = doc.data();
});
}
// Save backup
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const backupPath = path.join(__dirname, `backup_${timestamp}.json`);
fs.writeFileSync(backupPath, JSON.stringify(backup, null, 2));
console.log(`Backup saved to: ${backupPath}`);
return backup;
}
module.exports = { exportData };
// Only run if called directly
if (require.main === module) {
exportData().catch(console.error);
}