Skip to content

Instantly share code, notes, and snippets.

View gregfenton's full-sized avatar

Greg Fenton gregfenton

View GitHub Profile
@gregfenton
gregfenton / restore-uistate.jsx
Created March 8, 2024 21:46
Algolia react-instantsearch code not working correctly
<InstantSearch
indexName={ALGOLIA_INDEX_PROJECT}
searchClient={searchClient}
routing={routing}
future={{
preserveSharedStateOnUnmount: true,
}}
onStateChange={({uiState, setUiState}) => {
if (firstRender.current) {
firstRender.current = false;
@gregfenton
gregfenton / create-alerts.js
Created February 10, 2024 17:57
Failing attempt at using @google-cloud/monitoring to add LogEvent policies -- API does not currently support it
// This code fails with the error described in:
// https://github.com/googleapis/google-cloud-node/issues/4361
//
// The Node/JS version of the SDK currently does not support adding LogEvent policies.
//
//
// To run this code:
// - save to a file (e.g. "create-alerts.js")
// - be logged into your gcloud account (`gcloud auth login`)
// - set the env var `PROJ_ID` with your project ID
@gregfenton
gregfenton / functions--index.ts
Last active February 7, 2024 16:43
`index.ts` for my Cloud Functions project that load other CF files dynamically from the directory structure
/*
* This file is used to dynamically import and export functions from the `fs`, `http`, `sched`, and `storage` directories.
* The exported functions are then used in the `index.ts` file to define the Cloud Functions.
*
* Only files with the extension `.f.ts` are imported and exported (well, `.f.js` after `tsc` has run).
*
* Concept for this is inspired by https://codeburst.io/organizing-your-firebase-cloud-functions-67dc17b3b0da
* -- Thank you @TarikHuber !!
*/
import * as fs from 'fs';
@gregfenton
gregfenton / firestore_add_field_to_all_docs_in_collection.js
Last active December 7, 2023 15:33
Updates all docs in a Firestore collection with the given values -- command-line JavaScript/node
/**
* 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 / load_json_to_firstore.js
Last active December 4, 2023 16:24
Loads JSON data into a Firestore database (cloud or local emulator) -- command-line JavaScript/node
/**
* 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.
*
@gregfenton
gregfenton / AuthProvider.js
Created February 8, 2022 03:18
A React/React-Native authentication provider that uses the Context API to make authentication & profile info available to "the rest of the app"
import React, {createContext, useEffect, useState} from 'react';
import auth from '@react-native-firebase/auth'; // or from 'firebase/auth';
import Loading from '../components/Loading';
import {myFirestore} from '../common/firebase.js';
export const AuthContext = createContext({});
export const AuthProvider = (props) => {
@gregfenton
gregfenton / cleanup_firestore.js
Last active September 10, 2023 02:18
Deletes all records from a Firestore collection that match some criteria -- command-line JavaScript/node
/**
* 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 jump point for the script is this line in main():
*
* await deleteDocs('orders', 'closedDate', '<', new Date('2021/08/31'))
@gregfenton
gregfenton / create_firebase_project.bash
Last active June 16, 2023 23:04
Shell script for creating a Firebase project
#!/bin/bash
red=$(tput setaf 1)
green=$(tput setaf 2)
blue=$(tput setaf 6)
yellow=$(tput setaf 3)
reset=$(tput sgr0)
ROOT=$(pwd)
echo ""
@gregfenton
gregfenton / get_firestore_collection_as_json.js
Last active May 16, 2023 17:38
Downloads all docs in a Firestore collection and stores as a JSON file -- command-line JavaScript/node
/**
* 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 / README.md
Last active March 10, 2023 17:45 — forked from acidtone/README.md
Express: send() vs json() vs end()

Express: res.send() vs res.json() vs res.end()

Materials

Key Takeaways

  • When in doubt, use .send(). It dynamically sets Content-Type headers to match the data it sends.
  • When sending JSON, you can either use .json() or .send().
    • .json() is likely less confusing
    • .json() uses .send() under the hood so the resulting HTTP headers are the same.