With Cloud Functions, you can configure a CLOUDDB trigger to call a function when data is entered, deleted, or modified in Cloud DB.
You need to enable two services.
First of all, sign in to AppGallery Connect and perform the following operations:
- Enable Cloud Functions. Click My projects, click your project card, and go to Build > Cloud Functions. On the Cloud Functions page, click Enable now. Click + New Function to create a function.
- Enable Cloud DB. Click My projects, click your project card, and go to Build > Cloud DB. On the Cloud DB page, click Enable now and then create an object type and a Cloud DB zone.
Then you need to do the following in AppGallery Connect:
Go to Project settings > Server SDK. On the Server SDK tab page, click Create for API client first, and then click Download credential to download the authentication credential. Finally, add it to the model directory of your project
You need to modify the path to the credential as it was saved to the model directory. In the CloudDBZoneWrapper.js file (use the _dirname method to obtain the file), modify the initialization code as follows:
You can either use the Start.js file as the handler by default or create a new one. For example, the inde.js file is created as a handler. The sample code is as follows:
You need to package your project, upload it to Cloud Functions, and test it with events. The handler file should be saved in the root directory.
Sign in to AppGallery Connect and go to the Cloud Functions page. Upload your package and set the name of the handler to index.myHandler
Configure a CLOUDDB trigger. Click + Add Trigger and configure the type, Cloud DB zone, and object type for the trigger.
In AppGallery Connect, go to Cloud Functions > Test.
Configure a trigger event according to the documentation
If you need to configure a data insertion event, add the following sample code.
Check the test result.