#!/usr/bin/env ts-node import { DocumentData, Firestore, QueryDocumentSnapshot, } from '@google-cloud/firestore'; import { config as setupEnv } from 'dotenv-flow'; import { sequence } from '@/utils'; import connect from '../src/config/db'; setupEnv(); /** * @returns {Map} a map mapping all root level keys for a * collection (`collectionName`) to an id, representing one object in that * collection that contains that key (to do further investigation into) */ const discoverSchema = async ({ firestore, collectionName, }: { firestore: Firestore; collectionName: string; }): Promise> => { const schemaMap = new Map(); const collection = firestore.collection(collectionName); const documents = new Array>(); const snapshot = await collection.get(); snapshot.forEach((doc) => { documents.push(doc); }); await sequence( documents.map((doc) => async () => { const data = await doc.data(); Object.keys(data).forEach((key) => { schemaMap.set(key, doc.id); }); }) ); return schemaMap; }; const main = async () => { await connect(); const firestore = new Firestore(); const schema = await discoverSchema({ firestore, collectionName: 'pages' }); Array.from(schema.keys()).forEach((key) => { console.log(`${key}:`, schema.get(key)); }); }; main().catch((error) => { console.error(error); process.exit(1); });