当前位置:网站首页>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 .
边栏推荐
- Let me tell you the benefits of code refactoring
- Design and implementation of data analysis system
- 码农必备SQL调优(上)
- GO语言-值类型和引用类型
- With an average annual salary of 20W, automated test engineers are so popular?
- DB4AI: 数据库驱动AI
- [daily question series]: how to test web forms?
- 测试9年,面试华为要薪1万,华为员工:公司没这么低工资的岗
- Go Language - value type and Reference Type
- Unified record of my code variable names
猜你喜欢

Introduction to JVM basic concepts

2022 Tibet's latest eight major construction personnel (labor workers) simulation test question bank and answers

2022.02.28

Qcustomplot 1.0.1 learning (3) - plotting quadratic functions

Export configuration to FTP or TFTP server

03 _ 事务隔离:为什么你改了我还看不见?

使用Cloud DB构建APP 快速入门-快游戏篇
![[0006] titre, mots clés et description de la page](/img/28/973bdb04420c9e6e9a2331663c6948.png)
[0006] titre, mots clés et description de la page

CF662B Graph Coloring题解--zhengjun

GO语言-数组Array
随机推荐
AGC安全规则是如何简化用户授权和验证请求
06 _ Global lock and table lock: Why are there so many obstacles to adding a field to a table?
openGauss并行解码浅谈
CF662B Graph Coloring题解--zhengjun
After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
Learnopongl notes (IV) - Advanced OpenGL II
Charles自动保存响应数据
Learn automatic testing of postman interface from 0 to 1
Find combination number (function)
Arthas practice documentation
Shutter-- page Jump animation
同学,你听说过MOT吗?
[system safety] XLII PowerShell malicious code detection series (4) paper summary and abstract syntax tree (AST) extraction
【MongoDB】4. Usage specification of mongodb
2022年软件测试的前景如何?需不需要懂代码?
Hands on, how should selenium deal with pseudo elements?
2022 Tibet's latest junior firefighter simulation test question bank and answers
[0006] title, keyword and page description
前沿科技探究之AI在索引推荐的应用
2022 Tibet's latest eight major construction personnel (labor workers) simulation test question bank and answers





