DEV Community

Zachary Powell
Zachary Powell

Posted on

Using the Cloud DB JavaScript Server SDK in Cloud Functions

With Cloud Functions, you can configure a CLOUDDB trigger to call a function when data is entered, deleted, or modified in Cloud DB.
The Cloud DB service has been released with a JavaScript Server SDK, which can be used in the Node.js environment that the Cloud Functions service supports. In this article, I will elaborate on how to enter and query data with the Cloud DB JavaScript Server SDK in Cloud Functions.
Image description

Enabling Required Services

You need to enable two services.
First of all, sign in to AppGallery Connect and perform the following operations:

  1. 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.
  2. 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.

Image description

Configuring the JavaScript Server SDK of Cloud DB

Here, I will use the src file in the Huawei service demo to demonstrate how the JavaScript Server SDK is configured. You can also download the file and use it for your own file from GitHub.

Then you need to do the following in AppGallery Connect:

Export an object type.

On the Cloud DB page, export the BookInfo object type to a JS file, and then add it to the model directory of your project.
Image description

Download the authentication credential.

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

Modify the path to the credential.

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:

Configure a handler.

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:

Packaging Your Project and Configuring a Trigger

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.

Image description

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

Image description

Configure a CLOUDDB trigger. Click + Add Trigger and configure the type, Cloud DB zone, and object type for the trigger.

Image description

Testing the Function

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.

Image description

Reference

Cloud Functions
Defining a programming model
Cloud DB JavaScript Server SDK

Top comments (0)