Manage Cloud DB via Cloud Function

Uğur ÇALIK
Huawei Developers
Published in
6 min readSep 8, 2021

Introduction

In this article we will focus how to use Cloud Function to manage the Cloud DB.

Cloud Function

What is the Cloud Function?

Cloud Function provides serverless Function as a service. Also it provides Cloud Function SDK which use with Cloud DB and Cloud Storage so that your app function can be implemented more easily. With Cloud Function you do not need server resource and reduce costs.

Getting Started

I assume that you have been created Cloud DB table and Objects. If you have not created, you can read the Cloud DB Documents and follow the steps. We just focus Cloud Function Server Implementation.

How to use Cloud Function to Manage the Cloud DB?

Creating Your Project and Adding an App, If you have not added a project, sign in to AppGallery Connect, create a project, and add an app to the project. For details, please refer to Getting Started with Android.

1- Enabling Cloud Functions

  1. Sign in to AppGallery Connect and click My projects.
  2. Find your project for which you need to enable Cloud Functions from the project list.
  3. Go to Build > Cloud Functions. The Cloud Functions page is displayed. Click Enable now in the upper right corner
Cloud Function Enabled

4. If you have not set the default data processing location, set it in the displayed dialog box and click OK. For details, please refer to Setting the Default Data Processing Location.

Now we are ready to create Cloud Function, but first we should have the Cloud DB Server package to upload to the Cloud Function.

Note: If you want to connect the Cloud DB from Cloud Function you can not use the Edit code inline option, you should use upload .zip file option. Actually that .zip file is a Cloud DB server implemantation.

.zip file option

2- Creating Cloud DB Server Package

The server code should be coded JavaScript. After that we will create a .zip package to upload the Cloud Function. The .zip file content should like as below

handler.zip

— — handler.js

— — node_modules

— — — — async

— — — — async-listener

Configuring the Development Environment

1- Add an authentication credential by referring to Project-Level Credential. And save the credential file agc-apiclient-*.json to the specified path on your server. The file will be used during SDK initialization.

2- If you do not have the package.json file, run the following command in the root directory of your project to create it.

Creating package.json

3- Run the following command to integrate the AppGallery Connect Server SDK into your project. After run that command the node_models will be created in the project folder.

integrating the AppGallery Connect Server SDK

4- Import the AGC module to your project.

Adding Object Type Files

Export the JS object type files used for service development on the server from Cloud DB. Ensure that File Format: JS, File type of js: serveSDK choose. For details, see Exporting Object Types.

Initialization

After adding the object type files, you can start to develop services on the server. Before developing a service on the server, initialize AGCClient, set a name of a Cloud DB zone where data operations are performed, and obtain the Cloud DB zone instance.

1- Initialize AGCClient.

2- Initialize the AGConnectCloudDB instance

3- Create the CloudDBZoneConfig configuration object, set the Cloud DB zone name on the cloud, and open the Cloud DB zone instance.

Writing Data

This section mainly describes how to write data to services on the server using Server SDK. Use the executeUpsert() method to write data.

Viewing Data

Query data through executeQuery().

Combine the query method with the orderByDesc() and limit() methods to sort data in descending order and limit the number of data records to be displayed.

Optional: If you want to see the response on log you can use the processQueryResult function.

● Now let’s look at the latest folder structure and handler.js ,CloudDBZoneWrapper.js files. You can get referance from these files.

Latest Folder Structure

● After the folder structure control to create the package, select all server file and folder. Right click and add to archieve as a .zip file.

Creating Cloud Function Package
Creating .zip file

Now we have created the handler.js file which we will upload to the Cloud Function.

3- Uploading the package to the Cloud Function

  1. Go to > My Project > Build > Cloud Functions and click the New Function
Create New Function
  1. Fill the function names section and

2. Upload the .zip file which we created before.

3. This part is very important, handler is our .zip file name, myHandler is our main method name.

4. Click the save button and wait until processing end.

4- Testing Cloud Function Package

  1. Click the Function name which you created previous part.

2. Add the trigger which you want. I will add Cron Trigger, it provides certain periods run the function automatically.

3. Click the test button

When code is run successfully the log should be like below

Note: If you can not see any logs, probably there be a syntax or exception error in your code.You should surround all function returns with try {} catch{} block and print the error. Check your code and print the all line step by step and upload the recent package to Cloud Function and run again. You can find the error with this way.

Conclusion

We have learnt how to create the Cloud DB server package, and we have upload the that server package to Cloud Function and we have run it. So we did not need to pyhsical server we done all things via Huawei Cloud Function features.

Thanks for reading …

References

Huawei Cloud DB

Huawei Cloud Function

--

--