Firebase Cloud Functions: 4 Low-Code Examples (2023)

If you've ever felt tired of dealing with backend web development and servers, know that you aren't alone: 94% of all companies use cloud services, and up to 75% rely on serverless architectures to reduce costs while increasing their development speed, according to a study by Datadog.

With solutions like Firebase Cloud Functions, you can now easily create and maintain entire backends visually without writing with a single shell command. In this article, we'll take you on a wild ride as we show you the ins and outs of Firebase Cloud Functions―what they look like, how to create them with Rowy, and how powerful they can be. Time to unleash the full potential of your backend!

What's a Firebase Cloud Function

Google Cloud Functions is a service provided by Google to run code in the cloud without worrying about managing and scaling web servers. They are written in JavaScript and run in a managed Node.js environment.

Firestore Cloud Functions are Google Cloud Functions for Firestore. For example, you can write code to run when a new Firestore document is created.

We can distinguish two types of Google Cloud Functions: HTTP functions and event-driven functions. HTTP functions are triggered when you visit a generated URL, like an API call. Event-driven functions are webhooks triggered by a specific event, like when a user updates a document. Both are commonly used for data processing, automation, and building microservices. Let's see how to use both.

How To Create A Firebase Cloud Function With Rowy

1. How To Create Google Cloud Functions Manually

Setting up Cloud Functions manually can feel overwhelming:

1. You need to have a Firebase project
2. You need to set up Node.js and download the Firebase CLI
3. You have to follow six steps to get a working project directory
4. Write the code logic on your own machine
5. Download a local emulator to test your functions
6. Deploy the functions to the production environment
7. Monitor logs and errors in your Google Cloud Console

Then, you must repeat these steps every time you add a new feature, read the documentation, learn Git to collaborate with other developers, etc.

In other words, it's a pain if you come from a front-end engineering or product background or just want to get things done.

That's where Rowy comes in. With Rowy, you can write Firebase Cloud Functions in a low-code way, right from inside a familiar spreadsheet-like interface. You don't need to download anything or read complex documentation―it just works. You just select an event (when a new row is created, updated, or deleted, for example, or when a user visits a URL), write the code, and deploy it in a click.

Rowy proposes 3 types of Firebase cloud functions out-of-the-box to cover both HTTP functions and event-driven functions: Action, Derivatives, and Webhooks. We'll know go through each of them in detail to see how they work.

2. Rowy Action Functions

Let's say you want to trigger a function from time to time. It could be to send an email, publish a blog post, or extract data from a webpage. You can do this with a click of a button with Rowy's Action column type.

Action columns are HTTP functions you can use to run any script on demand. In Rowy, simply add a new column with the Action type, write your code in the column's settings, and click the corresponding button in the table to execute the script. For example, an action column we use at Rowy to publish blog posts to Webflow from our Rowy CMS:

An action column - 0.jpg

When creating an Action column, you can pick between Script mode and Callable mode. The former runs right away but you can't use external npm packages. The latter has a build step but handles more complex use cases when you need to import a npm package. In the example above, we just run a simple script that fetches a webpage and stores it in the corresponding Firestore document.

The cloud function is automatically deployed, you get logs for testing by default, and you can use any npm package without any complex, multi-step setup. An example using the firebase-admin npm package to suspend a user's account in Firebase Auth:

import * as admin from "firebase-admin";
const auth = admin.auth();

import callableAction from "rowy-actions";

export const SuspendUser = callableAction(async ({ row, callableData }) => {
  const { action } = callableData;
  const { firstName, email } = row;
  // switch statement can be used to perform different event based on the state of the action cell
  const user = await auth.getUserByEmail(email);
  switch (action) {
    case "run":
    case "redo":
      // both run and redo preform the same action; disabling the user's account from firebase auth
      await auth.updateUser(user.uid, { disabled: true });
      return {
        success: true, // return if the operation was success
        message: `${firstName}'s account has been disabled`, // message shown in snackbar on the rowy ui after the completion of action
        cellStatus: "disabled", // optional cell label, to indicate the latest state of the cell/row
        newState: "undo", // "redo" | "undo" | "disabled" are options set the behavior of action button next time it runs
      };
    case "undo":
      // re-enable user's firebase account
      await auth.updateUser(user.uid, { disabled: false });
      return {
        success: true, // return if the operation was success
        message: `${firstName}'s account has been re-enabled`, // message shown in snackbar on the rowy ui after the completion of action
        cellStatus: "active", // optional cell label, to indicate the latest state of the cell/row
        newState: "redo", // "redo" | "undo" | "disabled" are options set the behavior of action button next time it runs
      };
    default:
      // return error message when no action is preformed
      return {
        success: false,
        message: "Reached undefined state",
        newState: "redo",
      };
  }
});

3. Derivative Functions in Rowy

Derivative columns are another type of Rowy columns that run a Cloud function when the corresponding row is created, updated or deleted―an event-driven function for Firestore.

It's especially useful to process Firestore data whenever the database changes, to derive values from other fields for example. Let's say you have a firstName column and a lastName column in your Firestore database. You can create a fullName Deruvatuve column that concatenates the two fields automatically:

a derivative column - 1.jpg

Just like Action columns, you can use any npm package in Derivative columns. You can also use the function's parameters to access the database, files from Google Storage, or users authenticated with Google Auth:

const derivative: Derivative = async ({ row, ref, db, storage, auth }) => {
    const showdown = require('showdown')

    const converter = new showdown.Converter();

    return converter.makeHtml(row.content);
}

4. Webhook Functions with Rowy

Last but not least, we have Rowy webhooks. Webhooks can be used as event-driven functions for third-party service events, but they can also be used as HTTP functions by using the webhook's generated URL: whenever you visit the URL, the webhook is triggered.

For example, you can create a Stripe webhook to listen to Stripe events by telling Stripe to use your webhook URL. Whenever a new customer registers on your app, you could add it to your Rowy-powered CRM automatically:

A webhook creating a new customer row - 2.jpg

Webhooks can also be called from other Rowy functions―Derivative or Action―to create advanced features. Naturally, you can also use any npm pakkage or Firestore object within a webhook function.

Join The Rowy Community On Discord

That's a wrap. You now know how to create Firebase Cloud Functions with Rowy for all the use cases you can think about. Rowy proposes a simple solution you can use in 15 minutes to build your backend without writing a single line of code, and it won't take much time to maintain on a monthly basis either. Don't hesitate to invite your teammates to build together as well!

We're building a community of developers and product managers who want to build better and faster products with low-code: join us on Discord to get help, share your ideas, and meet other people who are building with Rowy.

Get started with Rowy in minutes

Continue reading

Browse all