1+ import { initializeApp } from "firebase/app" ;
2+ import { collection , getDocs , getFirestore , query } from "firebase/firestore" ;
3+ import fs from 'fs' ;
4+ import dotEnv from 'dotenv' ;
5+
6+ dotEnv . config ( ) ;
7+
8+ // TODO: Replace the following with your app's Firebase project configuration
9+ // See: https://support.google.com/firebase/answer/7015592
10+ const firebaseConfig = {
11+ apiKey : process . env . FB_API_KEY ,
12+ authDomain : process . env . FB_AUTH_DOMAIN ,
13+ projectId : process . env . FB_PROJECT_ID ,
14+ storageBucket : process . env . FB_STORAGE_BUCKET ,
15+ messagingSenderId : process . env . FB_MESSAGING_SENDER_ID ,
16+ appId : process . env . FB_APP_ID ,
17+ measurementId : process . env . FB_MEASUREMENT_ID
18+ } ;
19+
20+ // Initialize Firebase
21+ const app = initializeApp ( firebaseConfig ) ;
22+
23+
24+ // Initialize Cloud Firestore and get a reference to the service
25+ const db = getFirestore ( app ) ;
26+
27+ const demographQuery = query ( collection ( db , 'demographics' ) )
28+ const snapshot = await getDocs ( demographQuery )
29+
30+ const data = [ ] ;
31+ snapshot . forEach ( doc => {
32+ data . push ( doc . data ( ) ) ;
33+ } )
34+ const allKeys = Array . from ( new Set ( data . flatMap ( elem => Object . keys ( elem ) ) ) ) ;
35+
36+
37+ function toCSV ( list , keys ) {
38+ const fields = [ ]
39+ for ( const item of list ) {
40+ fields . push ( keys . map ( key => item [ key ] ) . join ( ',' ) )
41+ }
42+ return fields . join ( "\n" )
43+ }
44+ const csvFormat = allKeys . join ( ',' ) + "\n" + toCSV ( data , allKeys ) + "\n" ;
45+
46+ fs . mkdirSync ( './.output' , { recursive : true } ) ;
47+ fs . writeFileSync ( './.output/demographicsICHack24.csv' , csvFormat ) ;
48+
49+ process . exit ( 0 ) ;
0 commit comments