Member-only story
Appwrite Cloud Functions in Dart — Part 2
How to unit test and debug cloud functions in VScode
Check out part one if you want to know how to set up the project:
In the first part, I wrote about the configuration and test of cloud functions on the web console of Appwrite. I also had examples of scheduling functions and triggering them by events. In this Part 2, I describe my workflow of developing a cloud function, that uses the backend services as input and output of their execution. The function will just write a custom log of all events into a separate database.
In the first section, I show the cumbersome workflow to test a cloud function using the deployed code in the web console. In the second section, I show an alternative way and use my minimalistic integration test frame when developing cloud functions. Maybe there is another way to debug cloud functions in a Docker container or the real backend. I think my workflow is quite easy and it can’t be simpler. But see yourself.
If you followed part 1 you know that there is already a skeleton of the eventListener function in functions/eventListener/lib/main.dart.
Add Functionality to the Function
Until now the cloud function is nothing more than a hello world kind of starting code. As the name suggests the function shall listen to events and do something useful with them. As a simple example, the function will write log entries into a collection called event_log of the database called monitoring.
First I remove all services except Databases database = Databases(client);
from the template code. This is the only one I need for writing the output. The input is taken from the variables map coming in with the request.
Then I must get rid of this message Environment variables are not set. Function cannot use Appwrite SDK.
you see in stdout. The trick is, that cloud functions use the server SDK just as the appwrite CLI is using. For this to work the…