当前位置:网站首页>Using cloud DB to build apps quick start - quick games
Using cloud DB to build apps quick start - quick games
2022-06-11 15:47:00 【Gauss squirrel Club】
Fast game data will be stored on the cloud side , Data is not cached locally . When performing data management operations , You will directly operate cloud side data . Play fast SDK It will guarantee the communication and communication security between your application and cloud database .
Use Cloud DB Build fast games , The following preparations need to be completed :
- You have registered your account on the official website of the developer alliance and passed the real name authentication , For details, see Account registration authentication .
- Complete the construction of the development environment , Included in PC Installation on cocos creator、 Install the fast game loader on the Android phone debugging fast games , For details, see Install development tools .
- stay AppGallery Connect Complete the creation of fast game on the console , For details, see Create fast game .
- Generate fingerprint Certificate , For details, see “ Generate fingerprint Certificate ”.
- You've got the sample code , Please from Sample code obtain .
Enable service
Use Cloud DB Before service , You need to enable the service first .
- Sign in AppGallery Connect Website , choice “ My project ”.
- Select the item in the item list page , Click the application that needs to enable cloud database service under the project .
- Select... From the navigation tree “ structure > Cloud database ”.
- single click “ Open now ”, Open Cloud database service .

- ( Optional ) If you have not selected a data processing location , You need to set the data processing location first , Please refer to Set the data processing location .

- After the service is initialized successfully , That is, the cloud database service is enabled successfully .
Add and export object types
You need to be based on AppGallery Connect Console creates object types , Please follow the steps to create the object types involved in the example , And export... For fast game development json Format and js Format object type file . It is not allowed to modify the exported json Format and js Format file , Otherwise, the data synchronization function will be abnormal .
- Sign in AppGallery Connect Website , choice “ My project ”.
- Select the item in the item list page , Click the application that needs to create the object type under the project .
- Select... From the navigation tree “ structure > Cloud database ”.
- single click “ newly added ”, Enter the create object type page .

- Enter an object type named “BookInfo” after , single click “ next step ”.
- single click
, After adding the following fields , single click “ next step ”.
surface 1 Field definition table Field name
type
Primary key
Non empty
encryption
The default value is
id
Integer
√
√
–
–
bookName
String
–
–
–
–
author
String
–
–
–
–
price
Double
–
–
–
–
publisher
String
–
–
–
–
publishTime
Date
–
–
–
–
shadowFlag
Boolean
–
–
–
true
- single click
, Set the index name to “bookName”, The index field is “bookName” after , single click “ next step ”. - Set the permissions of each role according to the following requirements , single click “ next step ”.
surface 2 Permission configuration table role
query
upsert
delete
All the people
√
–
–
Authenticated user
√
√
√
Data creator
√
√
√
Administrators
√
√
√
- single click “ determine ”.
After creation, return to the object type list , You can view the object types that have been created .
- single click “ export ”.

- export “json Format ” and “js Format ” file , The exported file is used to add to the local development environment in subsequent steps .
- export json Format file
- choice “json Format ”.
- single click “ export ”.
- export js Format file
- choice “js Format ”.
- choice js file type , choice “quickGame”.
- single click “ export ”.
- export json Format file
New storage area
You can be based on AppGallery Connect The console creates a data store on the cloud side , Please follow the steps to create a storage area named “QuickStartDemo” The storage area of .
- Sign in AppGallery Connect Website , choice “ My project ”.
- Select the item in the item list page , Click the application that needs to create a storage area under the project .
- Select... From the navigation tree “ structure > Cloud database ”.
- choice “ Storage area ” Tab .
- single click “ newly added ”, Enter the create storage page .

- The input store name is “QuickStartDemo”.
- single click “ determine ”.
After creation, return to the storage area list , You can view the created storage .
Configure the development environment
- open cocos creator, An example of its directory is as follows .

- Configure application information .
- Sign in AppGallery Connect Website , choice “ My project ”.
- Select the item in the item list page , Click apply... Under project .
- choice “ routine ” Tab , Download profile “agconnect-services.json”, And copy it to your fast game project assets/Script Under the table of contents , It is recommended to create a separate directory to store .
- stay function Add... To the file agconnect-services.json File reference .
import * as context from './agconnect-services.json';
- Integrate Play fast SDK. Download the fast game of fast cloud database service SDK, And will SDK js The script is imported into... In the project assets/Script Under the table of contents , It is recommended to create a separate directory to store . And put all SDK js Script settings “ Import as plug-in ”, The specific steps are: :
Add object type file
When developing applications , You can directly put AppGallery Connect Exported from the console json Format and js Add the format file to the local development environment , And pass AGConnectCloudDB Class createObjectType() Method to define and create object types . When you are developing local applications , There is no need to create the object type again .
- Will already be in AppGallery Connect All exported on the console json Format and js Add the format file to the local development environment .
- initialization Cloud DB, adopt AGConnectCloudDB Class createObjectType() Method to define and create object types , For details, see initialization .
initialization
After adding the object type file , You can use cloud database for application development . When you develop an application , You need to perform initialization first , initialization AGConnectCloudDB、 establish Cloud DB zone And object types .
- adopt initialize() Method initialization AGConnectCloudDB.
agconnect.cloudDB.AGConnectCloudDB.initialize(context); - adopt getInstance() Method to get AGConnectCloudDB example , And use createObjectType() Create object type .
const schema = require('./BookInfo.json'); const agcCloudDB = agconnect.cloudDB.AGConnectCloudDB.getInstance(); agcCloudDB.createObjectType(schema); - open Cloud DB zone.
const config = new agconnect.cloudDB.CloudDBZoneConfig('QuickStartDemo'); const cloudDBZone = await agcCloudDB.openCloudDBZone(config);
Write data
This section mainly introduces how to write data in the application , As shown below , Use executeUpsert() Realize data writing .
async function executeUpsert (book) {
try {
const cloudDBZoneResult = await cloudDBZone.executeUpsert(book);
console.log('upsert' + cloudDBZoneResult + 'record' );
} catch (e) {
conso.log(e);
}
}View the data
Data query and sorting
adopt executeQuery() Realize asynchronous query of data .
async function executeQuery() {
try {
const query = agconnect.cloudDB.CloudDBZoneQuery.where(BookInfo);
const snapshot = await cloudDBZone.executeQuery(query);
const resultArray = snapshot.getSnapshotObjects();
console.log(resultArray);
} catch(e) {
console.log(e);
}
}Through query and limit() Method combination , Realize the function of limiting the number of query data display ; And orderByAsc() Methods or orderByAsc() Method combination to realize the sorting function of data .
async function executeQueryWithOrder (object) {
const query = agconnect.cloudDB.CloudDBZoneQuery.where(BookInfo);
if (object.name.length > 0) {
query.equalTo('bookName', object.name);
}
if (parseFloat(object.minPrice) > 0) {
query.greaterThanOrEqualTo('price', parseFloat(object.minPrice));
}
if (parseFloat(object.maxPrice) > 0 && parseFloat(object.maxPrice) > parseFloat(object.minPrice)) {
query.lessThanOrEqualTo('price', parseFloat(object.maxPrice));
}
if (parseInt(object.bookCount) > 0) {
query.limit(parseInt(object.bookCount));
}
query.orderByAsc('id');
try {
const snapshot = await cloudDBZone.executeQuery(query);
console.log(snapshot.getSnapshotObjects());
return snapshot.getSnapshotObjects();
} catch (e) {
console.log(e);
}
}That's all for this article , Thank you for reading .
边栏推荐
- 02 _ Log system: how does an SQL UPDATE statement execute?
- 数据库资源负载管理(下篇)
- 内存优化表MOT管理
- [creation mode] prototype mode
- 前沿科技探究之AI功能:慢SQL发现
- Tianjin Port coke wharf hand in hand map flapping software to visually unlock the smart coke port
- File is in use and cannot be renamed solution
- 【创建型模式】抽象工厂模式
- 数据库资源负载管理(上篇)
- Shutter-- page Jump animation
猜你喜欢
![[Yugong series] June 2022 Net architecture class 076- execution principle of distributed middleware schedulemaster](/img/c4/65babf7f51eaf1445ad6a02330975e.png)
[Yugong series] June 2022 Net architecture class 076- execution principle of distributed middleware schedulemaster
![[daily question series]: how to test web forms?](/img/80/d0862275b547a7b5224d1c0effa779.jpg)
[daily question series]: how to test web forms?

openGauss 多线程架构启动过程详解

2022 Tibet's latest junior firefighter simulation test question bank and answers
![[0006] title, keyword and page description](/img/28/973bdb04420c9e6e9a2331663c6948.png)
[0006] title, keyword and page description

04 _ In simple terms index (I)

Go language slice

leetcode 120. 三角形最小路径和

从内核代码了解SQL如何解析

Thales cloud security report shows that cloud data leakage and complexity are on the rise
随机推荐
Export configuration to FTP or TFTP server
MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size
openGauss 多线程架构启动过程详解
Shuttle-- common commands
Analysis on the architecture of distributed systems - transaction and isolation level (multi object, multi operation) Part 2
零基础自学软件测试,我花7天时间整理了一套学习路线,希望能帮助到大家..
如何管理并发写入操作?带你快速上手
GO语言-值类型和引用类型
The most egregious error set of tone codes
实时特征计算平台架构方法论和实践
Design and implementation of data analysis system
【愚公系列】2022年06月 .NET架构班 077-分布式中间件 ScheduleMaster加载程序集定时任务
The difference between go language array and slice
[creation mode] abstract factory mode
码农必备SQL调优(上)
[creation mode] single instance mode
Number system conversion (function)
Thales cloud security report shows that cloud data leakage and complexity are on the rise
【创建型模式】单例模式
In June, 2019, cat teacher's report on monitoring





