Skip to content

Instantly share code, notes, and snippets.

Avatar

gregfenton

View GitHub Profile
@gregfenton
gregfenton / firestore_add_field_to_all_docs_in_collection.js
Last active Dec 12, 2021
FIRESTORE: a node/JavaScript command-line script that visits all docs in a Firestore collection and updates them with the given values (in this example, we set/add an "enabled" field to TRUE) -- uses the Firebase Web SDK v9 API
View firestore_add_field_to_all_docs_in_collection.js
/**
* firestore_add_field_to_all_docs_in_collection.js
*
* This script is assumed to live in an existing Javascript project that has its own package.json.
* I store this script in <PROJECT_ROOT>/tools/cleanup/.
*
* To use:
* 1. Import the 'firebase' and 'esm' NPM modules to your project (Run the command: `npm install firebase esm`)
* 2. Edit the data for USER1 and FIREBASE_CONFIG, then adjust parameters in `main` for the call to `visitDocs`.
*
@gregfenton
gregfenton / get_firestore_collection_as_json.js
Last active Dec 12, 2021
FIRESTORE: a node/javascript command-line script to fetch all documents in a Firestore collection and output them to a file as JSON -- uses the Firebase Web SDK v9 API
View get_firestore_collection_as_json.js
/**
* get_firestore_collection_as_json.js
*
* This code is a "node script" used to fetch all documents from a Firestore collection,
* either in "the cloud" or the emulator, and write them to a file as JSON.
*
* The script is a "client app", so it logs in with Firebase Auth using email/password from the variable USER1.
* This USER1 user (the.admin.guy@test.com) must exist in your Firebase project and have read access
* to the collection(s) you are populating. You can create that account via
* Firebase Console >> Authentication.
@gregfenton
gregfenton / fetch-user-records-from-google-sheets.js
Last active Dec 12, 2021
JAVASCRIPT & GOOGLE SHEETS: a node/javascript command-line script that downloads data from a worksheet in Google Sheets and stores in a local JSON file
View fetch-user-records-from-google-sheets.js
/**
* A script that:
* 1. loads Google Sheets API keys and spreadsheet IDs (see GOOGLE_API_KEY)
* 2. connects to the identifed Google Sheet
* 3. grabs the data from a named Worksheet (see SHEET_TO_GET)
* 4. iterates over each row (see processUserRows())
* 5. puts them into a JSON structure
* 6. and writes that out to a file (see FILE_NAME)
*
* To run this script, I have this in my package.json:
@gregfenton
gregfenton / load_json_to_firstore.js
Last active Dec 12, 2021
FIRESTORE: a node/javascript command-line script that loads JSON data into a Firestore database (could be cloud or local emulator)
View load_json_to_firstore.js
/**
* This code is a "node script" used to load data into your Firestore database, either in "the cloud" or the emulator.
*
* The script is a "client app", so it logs in with Firebase Auth using email/password from the variable USER1.
*
* The USER1 user (the.admin.guy@test.com) must exist in your Firebase project and have write access
* to the collection(s) you are populating. You can create that account via Firebase Console >> Authentication.
*
* To use:
* 1. Import the 'firebase' and 'esm' NPM modules to your project (Run the command: `npm install firebase esm`)
@gregfenton
gregfenton / cleanup_firestore.js
Last active Dec 12, 2021
FIRESTORE: a (hack) node/javascript command-line script to connect to Firestore and delete some records -- uses the Firebase Web SDK v9 API
View cleanup_firestore.js
/**
* cleanup_firestore.js
*
* This code is a "node script" used to delete all documents from a Firestore
* collection that are older than a given date. This script can run against
* "the cloud" or the emulator.
*
* The script is a "client app", so it logs in with Firebase Auth using email/password from the variable USER1.
* This USER1 user (the.admin.guy@test.com) must exist in your Firebase project and have write access
* to the collection you are deleting from. You can create that user account via