Using the Cloud DB JavaScript Server SDK in Cloud Functions
Tags: javascript, huawei, database, cloud
Published: 2022-01-04 10:00:36

With [Cloud Functions](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-cloudfunction-introduction-0000001059279544), you can configure a CLOUDDB trigger to call a function when data is entered, deleted, or modified in Cloud DB.
The [Cloud DB](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-clouddb-get-started-serverjs-0000001127676879) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0ngd3j3w1nfd9uemev5.png)
 
## Enabling Required Services
You need to enable two services.
First of all, sign in to [AppGallery Connect](https://developer.huawei.com/consumer/en/service/josp/agc/index.html#/) 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](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-clouddb-get-started-0000001127676473#section138971210193319) and [a Cloud DB zone](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-clouddb-get-started-0000001127676473#section138971210193319).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnwplf1wyebh0y1ar4ag.png)

## Configuring the JavaScript Server SDK of Cloud DB

Here, I will use the **src** file in the Huawei service [demo](https://github.com/AppGalleryConnect/agc-demos/tree/main/Server/agc-clouddb-demo-javascript) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xivr7t8rux5tgn4d8cr9.png)

### 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:

{% gist https://gist.github.com/devwithzachary/e58c011fe2e7f9992a68a56aae801937 %}

### 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:

{% gist https://gist.github.com/devwithzachary/99e711abcf1e0f38b7480e0e43ed7528 %}

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lnvx1mxwt5ayavuyob8u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9h5clhm01582jet1k85.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wy94epo4tf93jfxt19i.png)
 
## Testing the Function
In AppGallery Connect, go to **Cloud Functions** > **Test**.
Configure a trigger event according to the [documentation](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-cloudfunction-programmode-0000001058311525#section207831182811)
If you need to configure a data insertion event, add the following sample code.

{% gist https://gist.github.com/devwithzachary/d9c65039345bdd00afea26aa3649ffc3 %}

Check the test result.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pria6xgfkfp3c0nnj3xg.png)

## Reference
[Cloud Functions](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-cloudfunction-introduction-0000001059279544)
[Defining a programming model](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-cloudfunction-programmode-0000001058311525)
[Cloud DB JavaScript Server SDK](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-clouddb-get-started-serverjs-0000001127676879)