
回复
HarmonyOS Next的云数据库采用三级结构模型:
String
、Boolean
、Date
)及权限配置(查询/新增/删除)IntAutoIncrement
/LongAutoIncrement
)步骤1:初始化SDK
import { cloud } from '@hw-agconnect/cloud'; // 初始化云数据库 const config = { apiKey: "your_api_key", clientSecret: "your_client_secret" }; cloud.initialize(config);
步骤2:创建数据对象
// 定义对象类型 class Student { id: number = 0; name: string = ""; age: number = 0; } // 插入数据 const student = new Student(); student.name = "张三"; student.age = 20; await cloud.db.collection('t_Student').add(student);
步骤3:查询数据
const query = cloud.db.collection('t_Student').where({ age: cloud.command.gt(18) }); const result = await query.get(); console.log(result.data);
权限管理:通过AGC控制台设置World
(所有人)、Authenticated
(已认证用户)等角色的操作权限
步骤1:新建云函数
cloudfunctions
目录,选择“新建Cloud Function”add
/delete
),适合复杂业务模块化步骤2:编写业务逻辑
// 示例:查询学生数据 import { CloudDBCollection } from '@hw-agconnect/cloud-server'; export class StudentService { async queryStudents() { const collection = new CloudDBCollection('t_Student'); return await collection.query({}); } }
步骤1:配置依赖
在oh-package.json
中添加云函数SDK:
"dependencies": {
"@hw-agconnect/cloud": "^1.0.0"
}
步骤2:调用函数接口
import cloud from '@hw-agconnect/cloud'; // 调用云函数 const result = await cloud.callFunction({ name: 'queryStudents', version: '$latest' }); console.log(result.data);
本地调试:通过DevEco Studio的“Run Cloud Function”模拟云端环境
age
、name
)添加索引,提升检索效率limit
和skip
实现数据分段加载,降低内存压力