Skip to content

Instantly share code, notes, and snippets.

@yamankatby
Created March 2, 2023 08:35
Show Gist options
  • Save yamankatby/5a725fee5035b40a007be50c03e715f4 to your computer and use it in GitHub Desktop.
Save yamankatby/5a725fee5035b40a007be50c03e715f4 to your computer and use it in GitHub Desktop.

Firebase Functions Extended

Some ideas about new functions that can be added as an extension to the firebase functions library.

Firestore

onFieldCreate

This functions is triggered when a field inside a document is added to the document for the first time. That can be if:

  • The document created for the first time and includes this field.
  • The document has been created before without that field, and has been updated to include it.
const functions = require("firebase-functions");

exports.myFunction = functions.firestore
  .document("my-collection/{docId}")
  .onWrite((change, context) => {
    const isFieldCreated =
      change.before.get(FIELD_NAME) === undefined &&
      change.after.get(FIELD_NAME) !== undefined;
    if (!isFieldCreated) return;
    /* ... */
  });

Note Checking the value to be a strong undifined as null, 0, and empty string are all valid Firestore document values, however undefined is not.

onFieldUpdate

This function is triggered when a field inside a document already exists and has been updated.

If the document as whole is updated, whoever the field value hasn't changed this function should'nt be triggered.

onFieldDelete

This function is triggered when a field inside a document is delted from the document. This can be if:

  • The document has be deleted as whole.
  • The field was exists in the document and the document has been updated and it's not exists in the new version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment