[Huawei Developers] Sherlock F:
Cloud DB Configuration and Example

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015083307.30890582786761562793849037037133:50511029021612:2800:41A9E1E53AD7937A03DB79CEEAB21B150FAD9BF7EAA3A8AD53A97C2403391C11.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015083307.30890582786761562793849037037133:50511029021612:2800:41A9E1E53AD7937A03DB79CEEAB21B150FAD9BF7EAA3A8AD53A97C2403391C11.png[/IMG]

Hi Everyone, today I will try to explain Cloud DB and its features also You can find code examples under required topics. You can download my project that developed using Kotlin from link where is at the end of Page.

What is Cloud DB ?

Cloud DB is an relational database based on Cloud . In addition to the easy of use, attracts developers with its management and a user-friendly interface. If you don’t have server When starting to develop an app, you will definitely use it .It includes many features for developers like data storage, maintenance, distribution, object-based data model. Also it is free.

Currently, Cloud DB is in Beta version. It must be activated before using so Developers have to request the activation of the service by sending an e-mail to agconnect@huawei.com with the subject as “[Cloud DB]-[Company name]-[Developer account ID]-[App ID]”

! I said before Cloud Db is an relational database . The only drawback is that Developers can’t query in multiple object type that is called Table in normal relational database system.

Cloud DB Synchronization Modes

Cloud DB contains two development modes different from together. I used cache mode in related example.

Cache Mode : Application data is stored on the cloud, and data on the device is a subset of data on the cloud. If persistent cache is allowed, Cloud DB support the automatic caching of query results on the device

Local Mode : Users can operate only the local data on the device, while device-cloud and multi-device data synchronization cannot be implemented.

Note : The cache mode and local mode can be used together or separately.

Cloud Db has stronger technical specifications than other cloud service providers. You can read all specifications following link

Cloud DB Structure and Data Model

Cloud DB is an object model-based database with a three-level structure that consists of Cloud DB zone, object type, and object.Cloud db may include many different database as you see. All Database are independent from others.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015083637.57697333103532214063646338067868:50511029021612:2800:F5D19FB6D0619346EF686CA7C803583F6A9C84914F96A46BC44196763A2BF3AF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015083637.57697333103532214063646338067868:50511029021612:2800:F5D19FB6D0619346EF686CA7C803583F6A9C84914F96A46BC44196763A2BF3AF.png[/IMG]

Cloud DB Zone

As Developers , you can think it as Database. It consist of object types that contains data. Each Cloud Zone can be different object type.

Object Type

Object Type stores data and includes data features . It is same as Table in Relational Database .Each object type must include at least one column as primary key. Object Types include many type like others database’s table for instance string, long, float, date, Boolean and more. You can learn all data types of Cloud DB visiting link.

Developers can import data from your device . All data must be in the json file.in addition They can export data from table/tables as json file.

Object

Objects are called data record. These records are stored in Object types.To learn declarations steps and restriction with detail ,please follow link.

User Permissions

Cloud DB can authenticate all users’ access to ensure security of application data. Developers specify these roles and ensure data security.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015083914.81106113416496432151662479323659:50511029021612:2800:77B1EE4D8FE6B005F159D7DF7C152683C9997F0F7473C45213E5B893E7C14A76.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015083914.81106113416496432151662479323659:50511029021612:2800:77B1EE4D8FE6B005F159D7DF7C152683C9997F0F7473C45213E5B893E7C14A76.png[/IMG]

Cloud DB defines four roles: Everyone, Authenticated user, Data creator, and Administrator, and three permissions: query, upsert (including adding and modifying), and delete.

Everyone : They just read data that come from Cloud zone. Upsert and delete rules can’t be added. but query permission can be changed.

Authenticated user : these users can only read data by default but developers can change their permissions .

Data Creator : The information about data creators is stored in the system table of data records. This role has all permissions by default and can customize the permissions.

Administrator : This role has all permissions by default and can customize the permissions. An administrator can manage and configure the permissions of other roles.

Note : If you want to use the permissions of Authenticated user when developing applications on the device, you need to enable auth service to sign in operation.

How to use Cloud db in an app

After this part I try to explain cloud db integration steps and its functions. I will share related code block under topic but If you want to test app , You can get related source(I will put link under article.).

Note : Also app was developed using Kotlin.

Before start to develop , you need to send mail to enable Cloud DB . I explained before How to do this so I don’t write again .After open Cloud db, create cloud zone and then Object type to store data.

agconnect-services.json file must be created. To learn how to create it please visit link .

//Cloud DB
implementation 'com.huawei.agconnect:agconnect-database:1.2.1.301'
//Auth Service
implementation 'com.huawei.agconnect:agconnect-core:1.3.1.300'
implementation 'com.huawei.hms:hwid:4.0.1.300'
implementation 'com.huawei.agconnect:agconnect-auth:1.3.1.300'

After enable cloud DB , Cloud Zone and Object type can be created. In this Example I used this object type. First field is primary key of Object type.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015084406.32280947230618058166645287573664:50511029021612:2800:ED34344D2473C6D8A3229A288DDDFB1B4B1F4DD5C4194A7E182D2B06D0844F64.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015084406.32280947230618058166645287573664:50511029021612:2800:ED34344D2473C6D8A3229A288DDDFB1B4B1F4DD5C4194A7E182D2B06D0844F64.png[/IMG]

When the Object type creating is finished , we need to export Object type information from Cloud DB page to use in app.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015084458.75301705674991135303791292900081:50511029021612:2800:9E4D7A41ECB64CE34945BE47B3B06E662ECEFF7B3C54AAFF9E2C192CD200857A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015084458.75301705674991135303791292900081:50511029021612:2800:9E4D7A41ECB64CE34945BE47B3B06E662ECEFF7B3C54AAFF9E2C192CD200857A.png[/IMG]

After click export button , you need to write app’s package name after that document will be created .You can export related information as Json or Java file.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015084553.73720971330294538445614848746898:50511029021612:2800:56FCF468A0D2E516C1CF47A4C2DD1DF96FB0E75AA701A1266946E5B202128528.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/255/0890090000023106255.20201015084553.73720971330294538445614848746898:50511029021612:2800:56FCF468A0D2E516C1CF47A4C2DD1DF96FB0E75AA701A1266946E5B202128528.png[/IMG]

Before start to develop cloud DB functions like upsert , delete or query , developers need to initialize AGConnectCloudDB, create a Cloud DB zone and object types.App needs to initialize before using. All developers must follow sequence of Cloud DB.

AGConnectCloudDB.initialize(context)

Initialize AGConnectCloudDB

Open CloudDB zone

Before starting with cloud DB zone, all initialization must be finished .

//CloudDB's initialize method is called, this method is called inside manifest file.
AGConnectCloudDB.initialize(applicationContext)

private val cloudDB = AGConnectCloudDB.getInstance()
cloudDB.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo())

private val cloudDBZoneConfig = CloudDBZoneConfig()//it must be created before open Cloud DB zone

private val cloudDbZone = cloudDB.openCloudDBZone(cloudDBZoneConfig,true)

Open CloudDBZone

Opening cloud db zone is important part of every project because all developers have to open cloud db zone to manage data. All transactions are developed and run using CloudDBZone object. If you check app , you can learn in a short time how to use it.

/* 1.Parameter is used to send cloud DB zone name You need to write cloudDBZone name into first parameter
2.Parameter is used to send CloudDB Synchronization Modes
3.Parameter is used to send Access property like CLOUDDBZONE_PUBLIC
after create CloudDBZoneConfig , CloudDBZone can be opened.
*/
cloudDBZoneConfig = CloudDBZoneConfig("BookComment", CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE, CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC)
cloudDbZone = cloudDB.openCloudDBZone(cloudDBZoneConfig,true)

Notes :

All Cloud db operations (Upsert,Query,Delete) must be run when the Cloud DB zone is opened. Otherwise, the write operation will fail.

Many object can be inserted or deleted at the same time If all objects are the same object type.

Select Operation

Cloud DB uses the executeQuery to get data from Cloud .

// It is used to get all comment from Cloud db zone.

val cloudDBZoneQueryTask =cloudDbZone.executeQuery(CloudDBZoneQuer
.where(Comment::class.java)

CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_FROM_CLOUD_ONLY)

If you want to get specific data , you can specify related column and restriction using method instead of SQL. Cloud Db doesn’t support sql.It includes many type of function to query operations like greaterThan(),greaterThanOrEqual(),orderByAsc(),etc.

More than one restriction can be used in one query.

val cloudDBZoneQueryTask =cloudDbZone.executeQuery(CloudDBZoneQuery
.where(Comment::class.java).contains("BookName",bookName),
CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_FROM_CLOUD_ONLY)

for more example ,please visit link

Insert & Update Operations

Cloud DB uses executeUpsert to insert and update operation. If an object with the same primary key exists in the Cloud DB zone, the existing object data will be updated. Otherwise, a new object is inserted. We can send model to insert or update operation.

var upsertTask : CloudDBZoneTask<Int> = cloudDbZone.executeUpsert(newComment)

Delete Operation

executeDelete() or executeDeleteAll() functions can be used to delete data.ExecuteDelete() function is used to delete a single object or a group of objects,executeDeleteAll() function is used to delete all data of an object type.Cloud DB will delete the corresponding data based on the primary key of the input object and does not check whether other attributes of the object are consistent with the stored data.

val cloudDBDeleteTask = cloudDbZone.executeDelete(selectedItem)

When you delete objects, the number of deleted objects will be returned if the deletion succeeds; otherwise, an exception will be returned.

All CRUD operations are in WrapperClass , you can see it

More information, you can visit https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201381349122390461&fid=0101187876626530001