firebase init
npm init
npm install --save-dev typescript @types/node
npx tsc --init
add "build": "tsc"
to package.json
"runQueriesAgainstEmulator": "ts-node src/index.ts"
ADd tsconfig:
"include": ["src"], "exclude": ["node_modules"] Ensure firebase.json includes databases and indexes
firebase emulators:start --project=ctfirestoreindexes
npm run build
npm run runQueriesAgainstEmulator
{ "reports": [{ "index": { "name": "projects/ctfirestoreindexes/databases/(default)/collectionGroups/cities/indexes/", "queryScope": "COLLECTION", "fields": [{ "fieldPath": "name", "order": "ASCENDING" }, { "fieldPath": "state", "order": "ASCENDING" }, { "fieldPath": "name", "order": "ASCENDING" }] }, "numQueries": "6" }, { "index": { "name": "projects/ctfirestoreindexes/databases/(default)/collectionGroups/cities/indexes/", "queryScope": "COLLECTION", "fields": [{ "fieldPath": "state", "order": "ASCENDING" }, { "fieldPath": "population", "order": "ASCENDING" }, { "fieldPath": "name", "order": "ASCENDING" }] }, "numQueries": "2" }] }
firebase firestore:indexes
firebase deploy --only firestore:indexes
As a proof of concept run
curl http://127.0.0.1:8080/emulator/v1/projects/ctfirestoreindexes:indexUsage?database=projects/ctfirestoreindexes/databases/\(default\) > emulator_index_dump.json npm run convertTofirestoreIndexesJson
This will print out the new json file. Some additional work is required to replace firestore.indexes.json with the new json.
removes name convert "name" to "collectionGroup" by taking the collection ID flatten reports.index delete numQueries