How to use Cloud DB with Kotlin ?
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.
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.
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 .
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.
When the Object type creating is finished , we need to export Object type information from Cloud DB page to use in app.
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.
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 .
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.
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 .
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.
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.
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.
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
App Images
Reference
Cloud DB’s Web page : Link
To learn all features of Cloud DB , please following this page: Link
App can be downloaded from Github :