-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportDBStructure.ts
More file actions
129 lines (104 loc) · 4.59 KB
/
exportDBStructure.ts
File metadata and controls
129 lines (104 loc) · 4.59 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Script to export Firestore database structure
* This script will list all collections and document structures in the database
* Reference: Firebase Admin SDK Documentation
*/
import { initializeApp, cert } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
// Initialize Firebase Admin
const serviceAccountPath = join(process.cwd(), 'service_key.json');
console.log('Looking for service account key at:', serviceAccountPath);
// Store markdown content
let markdownContent = '# Firestore Database Structure\n\n';
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const outputPath = join(process.cwd(), `database-structure-${timestamp}.md`);
try {
const serviceAccount = JSON.parse(readFileSync(serviceAccountPath, 'utf-8'));
initializeApp({
credential: cert(serviceAccount as any),
});
const db = getFirestore();
async function analyzeDocument(doc: FirebaseFirestore.DocumentSnapshot, depth: number = 0): Promise<string> {
let content = '';
const indent = ' '.repeat(depth);
const mdIndent = ' '.repeat(depth);
content += `${mdIndent}- 📄 Document: \`${doc.id}\`\n`;
const data = doc.data();
if (!data) return content;
// Add document fields
for (const [key, value] of Object.entries(data)) {
const type = Array.isArray(value) ? 'array' :
value instanceof Date ? 'timestamp' :
value === null ? 'null' :
typeof value;
content += `${mdIndent} - 🔑 \`${key}\`: \`${type}\`\n`;
// For arrays and objects, show their structure
if (type === 'array' && value.length > 0) {
content += `${mdIndent} - Array contains: \`${typeof value[0]}\`\n`;
} else if (type === 'object' && value !== null && !(value instanceof Date)) {
content += `${mdIndent} - Object keys: \`${Object.keys(value).join(', ')}\`\n`;
}
}
// Get subcollections of this document
const subcollections = await doc.ref.listCollections();
if (subcollections.length > 0) {
content += `${mdIndent} - 📚 **Subcollections:**\n`;
for (const subcol of subcollections) {
content += await analyzeCollection(subcol, depth + 2);
}
}
return content;
}
async function analyzeCollection(collection: FirebaseFirestore.CollectionReference, depth: number = 0): Promise<string> {
let content = '';
const mdIndent = ' '.repeat(depth);
content += `${mdIndent}- 📂 Collection: \`${collection.id}\`\n`;
const snapshot = await collection.limit(10).get(); // Increased limit to 10 for better sampling
if (snapshot.empty) {
content += `${mdIndent} - ℹ️ *No documents found in this collection*\n\n`;
return content;
}
for (const doc of snapshot.docs) {
content += await analyzeDocument(doc, depth + 1);
}
return content;
}
async function listCollectionsAndStructure() {
try {
console.log('🔍 Starting database structure export...\n');
// Add header information
markdownContent += `Generated on: ${new Date().toLocaleString()}\n\n`;
markdownContent += '## Collections Structure\n\n';
const collections = await db.listCollections();
for (const collection of collections) {
markdownContent += await analyzeCollection(collection);
markdownContent += '\n'; // Add spacing between top-level collections
}
// Save to file
writeFileSync(outputPath, markdownContent);
console.log('✅ Database structure export completed!');
console.log(`📝 Markdown file saved to: ${outputPath}`);
} catch (error) {
console.error('❌ Error while exporting database structure:', error);
process.exit(1);
}
}
// Execute the function
listCollectionsAndStructure();
} catch (error: any) {
if (error.code === 'ENOENT') {
console.error('❌ Error: service_key.json not found!');
console.error('Please follow these steps to get your service account key:');
console.error('1. Go to Firebase Console (https://console.firebase.google.com)');
console.error('2. Select your project');
console.error('3. Go to Project Settings (⚙️)');
console.error('4. Go to Service Accounts tab');
console.error('5. Click "Generate New Private Key"');
console.error('6. Save the downloaded file as "service_key.json" in the project root directory');
} else {
console.error('❌ Error initializing Firebase Admin:', error);
}
process.exit(1);
}