cloudbase-document-database-web-sdk by tencentcloudbase/skills
npx skills add https://github.com/tencentcloudbase/skills --skill cloudbase-document-database-web-sdk本技能提供关于在 Web 应用中使用 CloudBase 文档数据库 Web SDK 进行数据操作的指导。
在使用任何数据库操作之前,需要先初始化 CloudBase SDK:
import cloudbase from "@cloudbase/js-sdk";
// UMD 版本
// 如果您不使用 npm,并且希望使用 UMD 版本。您应该参考 https://docs.cloudbase.net/quick-start/#web-%E5%BF%AB%E9%80%9F%E4%BD%93%E9%AA%8C 获取 UMD 版本的最新信息。
const app = cloudbase.init({
env: "your-env-id", // 替换为您的环境 ID
});
const db = app.database();
const _ = db.command; // 获取查询操作符
// ... 登录
请记住,在实际查询数据库之前,* 必须 * 进行登录(身份验证)。
使用以下方式访问集合:
db.collection('collection-name')
CloudBase 通过 db.command(别名为 _)提供查询操作符:
_.gt(value) - 大于广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
_.gte(value) - 大于或等于_.lt(value) - 小于_.lte(value) - 小于或等于_.eq(value) - 等于_.neq(value) - 不等于_.in(array) - 值在数组中_.nin(array) - 值不在数组中通过文档 ID 查询:
const result = await db.collection('todos')
.doc('docId')
.get();
带条件查询:
const result = await db.collection('todos')
.where({
completed: false,
priority: 'high'
})
.get();
注意: get() 默认返回 100 条记录,最多 1000 条。
组合方法进行复杂查询:
.where(conditions) - 过滤条件.orderBy(field, direction) - 按字段排序('asc' 或 'desc').limit(number) - 限制结果数量(默认 100,最大 1000).skip(number) - 跳过记录以用于分页.field(object) - 指定要返回的字段(true/false)有关特定主题的详细信息,请参阅:
查看 ./crud-operations.md 了解:
查看 ./complex-queries.md 了解:
查看 ./pagination.md 了解:
查看 ./aggregation.md 了解:
查看 ./geolocation.md 了解:
查看 ./realtime.md 了解:
查看 ./security-rules.md 了解:
始终将数据库操作包裹在 try-catch 中:
try {
const result = await db.collection('todos').get();
console.log(result.data);
} catch (error) {
console.error('Database error:', error);
}
数据库操作返回:
{
data: [...], // 文档数组
// 其他元数据
}
"your-env-id" 替换为实际的 CloudBase 环境 IDget() 默认返回 100 条记录./security-rules.md。在安全规则中编写表达式时,可以使用上面提到的集合的类型定义作为类型参考。常见查询示例:
// 简单查询
db.collection('todos').where({ status: 'active' }).get()
// 使用操作符
db.collection('users').where({ age: _.gt(18) }).get()
// 分页
db.collection('posts')
.orderBy('createdAt', 'desc')
.skip(20)
.limit(10)
.get()
// 字段选择
db.collection('users')
.field({ name: true, email: true, _id: false })
.get()
有关更详细的示例和高级用法模式,请参阅本目录中的配套参考文件。
每个 数据库操作(包括 get()、add()、update()、delete() 等)都应检查返回值的 code 以发现任何错误。例如:
const result = await db.collection('todos').add(newTodo);
if(typeof result.code === 'string') {
// 处理错误 ...
}
错误 必须 以详细、人类可读的消息和友好的用户界面进行处理。
每周安装量
542
代码仓库
GitHub 星标数
38
首次出现
2026 年 1 月 22 日
安全审计
安装于
opencode476
codex475
gemini-cli468
github-copilot456
kimi-cli449
amp447
This skill provides guidance on using the CloudBase document database Web SDK for data operations in web applications.
Before using any database operations, initialize the CloudBase SDK:
import cloudbase from "@cloudbase/js-sdk";
// UMD version
// If you are not using npm, And want to use UMD version instead. You should refer to https://docs.cloudbase.net/quick-start/#web-%E5%BF%AB%E9%80%9F%E4%BD%93%E9%AA%8C for latest version of UMD version.
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment id
});
const db = app.database();
const _ = db.command; // Get query operators
// ... login
Remember to sign in(auth) is * REQUIRED before actually querying the database.
Access collections using:
db.collection('collection-name')
CloudBase provides query operators via db.command (aliased as _):
_.gt(value) - Greater than_.gte(value) - Greater than or equal_.lt(value) - Less than_.lte(value) - Less than or equal_.eq(value) - Equal to_.neq(value) - Not equal to_.in(array) - Value in array_.nin(array) - Value not in arrayQuery by document ID:
const result = await db.collection('todos')
.doc('docId')
.get();
Query with conditions:
const result = await db.collection('todos')
.where({
completed: false,
priority: 'high'
})
.get();
Note: get() returns 100 records by default, maximum 1000.
Combine methods for complex queries:
.where(conditions) - Filter conditions.orderBy(field, direction) - Sort by field ('asc' or 'desc').limit(number) - Limit results (default 100, max 1000).skip(number) - Skip records for pagination.field(object) - Specify fields to return (true/false)For detailed information on specific topics, refer to:
See ./crud-operations.md for:
See ./complex-queries.md for:
See ./pagination.md for:
See ./aggregation.md for:
See ./geolocation.md for:
See ./realtime.md for:
See ./security-rules.md for:
Always wrap database operations in try-catch:
try {
const result = await db.collection('todos').get();
console.log(result.data);
} catch (error) {
console.error('Database error:', error);
}
Database operations return:
{
data: [...], // Array of documents
// Additional metadata
}
"your-env-id" with actual CloudBase environment IDget() returns 100 records by default./security-rules.md. When writing expressions in security rules, The type definition of the collection mention above can be used as the type reference.Common query examples:
// Simple query
db.collection('todos').where({ status: 'active' }).get()
// With operators
db.collection('users').where({ age: _.gt(18) }).get()
// Pagination
db.collection('posts')
.orderBy('createdAt', 'desc')
.skip(20)
.limit(10)
.get()
// Field selection
db.collection('users')
.field({ name: true, email: true, _id: false })
.get()
For more detailed examples and advanced usage patterns, refer to the companion reference files in this directory.
EVERY database operation(including get(), add(), update(), delete() etc)should check the return value's code for any errors. For example:
const result = await db.collection('todos').add(newTodo);
if(typeof result.code === 'string') {
// Handle error ...
}
Error MUST be handled with detail and human-readable message and friendly UI.
Weekly Installs
542
Repository
GitHub Stars
38
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
opencode476
codex475
gemini-cli468
github-copilot456
kimi-cli449
amp447
Next.js 最佳实践指南:文件约定、RSC边界、异步模式与性能优化
46,400 周安装
FlowStudio MCP 构建部署 Power Automate 云流指南 | 自动化流程开发
530 周安装
mcp2cli:无需代码,将MCP/OpenAPI/GraphQL实时转换为命令行工具
530 周安装
交互式作品集设计指南:30秒吸引招聘者,提升作品集转化率与个人品牌
530 周安装
每日销售简报AI工具 - 自动生成优先级行动计划,整合日历、CRM和邮件数据
531 周安装
生产排程实战指南:离散制造工厂的有限产能排程、换线优化与瓶颈管理
531 周安装
Angular 21 最佳实践指南:TypeScript、Signals、组件与性能优化
531 周安装