Cloud functions

Folder Structure

1
functions/
2
├── src/
3
│ ├── auth/
4
│ │ ├── onCreate.function.ts
5
│ │ └── onDelete.function.ts
6
│ │
7
│ ├── db/
8
│ │ ├── users/
9
│ │ │ ├── onCreate.function.ts
10
│ │ │ ├── onModify.function.ts
11
│ │ │ └── ... other database functions ...
12
│ ├── firestore/
13
│ │ ├── users/
14
│ │ │ ├── onCreate.function.ts
15
│ │ │ ├── onModify.function.ts
16
│ │ │ └── ... other database functions ...
17
│ ├── storage/
18
│ │ ... storage functions ...
19
│ ├── https/
20
│ │ ├── createUser.function.ts
21
│ │ └── ... other https functions ...
22
│ └── index.ts
23
├── test/
24
│ ├── db/
25
│ │ ├── users/
26
│ │ │ ├── onDelete.test.ts
27
│ │ │ ├── onUpdate.test.ts
28
│ │ │ └── ... other database tests ...
29
│ ├── firestore/
30
│ │ ├── users/
31
│ │ │ ├── onCreate.test.ts
32
│ │ │ ├── onModify.test.ts
33
│ │ │ └── ... other database tests...
34
│ ├── https/
35
│ │ ├── createUser.test.ts
36
│ │ └── ... other https tests ...
37
│ └── util/
38
│ └── config.ts
Copied!

Installing dependencies

From the functions folder run:
1
npm install
2
npm install -g firebase-tools
Copied!

Authenticate to your Firebase account

1
firebase login
Copied!

Listing Firebase projects

1
firebase projects:list
Copied!

Listing aliases available in the project

1
firebase use
Copied!

Selecting an alias

We should use staging alias while testing new functionality.
1
firebase use staging
Copied!

Creating a new cloud function

Functions are automatically loaded from the index.js file if you follow the naming convention and folder structure, no need to add it manually.

Naming:

  • The naming of functions should indicate the trigger and not what they are doing.
  • Name your files according to the trigger you use ( onWrite, onCreate, onUpdate , onChange and onDelete).
  • Functions should end with .function.js to get loaded.

Folders:

  • Functions should be stored inside the folder that corresponds to the service that triggers the function. For example: the function onCreate.function.js inside `auth/` gets triggered when a new user is created on the authentication service of Firebase.

Testing functions locally

We can test functions locally but currently we cannot test other services of Firebase like Realtime Database, Firestore, etc. So be cautious, your local functions will be interacting with services deployed in staging.
Cloud functions run in a Node.js environment with version 8 and the React frontend with > 12 or the latest one. So a tool like Node Version Manager comes in handy in order to seamlessly change Node versions between terminal windows.
To initialize the emulator with functions only, run:
1
firebase emulators:start --only functions
Copied!
After it initializes, you should get your endpoints to test your HTTP functions:
1
✔ functions[requestsApp]: http function initialized (http://localhost:5001/...../requestsApp).
Copied!
More information about the Firebase Emulator.

Testing functions in online mode

Testing your cloud functions online is very simple and easy.
For that, you only have to set the variables localted in the env.example.json inside /functions folder. (Remember to rename the file to env.json)
Follow these steps for setting up your env.json file:
  • The first 3 properties "databaseURL", "storageBucket" and "projectId" are the same ones previously added to the frontend .env file.
  • For "serviceAccountKey" you should do the following:
    • Go to your proyect in the Firebase dashboard, click on Project settings and then click on Service accounts tab.
    • After that you'll be able to click on Generate new private key button and a json file containing your service account's credentials will be downloaded.
    • Place that file in your project and include the location of it into the "serviceAccountKey" in your env.json file.
After that, open your terminal, navigate to the /functions folder and execute npm test.
Warning: Use extra caution when handling service account credentials in your code. Do not commit them to a public repository, deploy them in a client app, or expose them in any way that could compromise the security of your Firebase project.

Deployment

When a pull request gets merged into development, functions are deployed automatically to the staging project in Firebase. Likewise, when merging/pushing into master, they're deployed to production.
We can manually deploy our project with the following command.
1
firebase deploy
Copied!
This will deploy Hosting, Functions and DB rules. Be sure to build the React frontend before deploying to Firebase.
If we want to deploy only certain services, we can do it with the following command. In this example we're only deploying our functions.
1
firebase deploy --only functions
Copied!
It's possible to deploy only a specific function.
1
firebase deploy --only functions:myFunction
Copied!
We can deploy more than one function at a time.
1
firebase deploy --only functions:myFunction,functions:anotherfunction
Copied!
Last modified 1yr ago