cloudbase-document-database-in-wechat-miniprogram
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCloudBase Document Database WeChat MiniProgram SDK
CloudBase文档数据库微信小程序SDK
This skill provides guidance on using the CloudBase document database SDK for data operations in WeChat MiniProgram applications.
本技能为在微信小程序应用中使用CloudBase文档数据库SDK进行数据操作提供指导。
Core Concepts
核心概念
Initialization
初始化
Before using any database operations, initialize the database reference:
javascript
// Get default environment database reference
const db = wx.cloud.database()
const _ = db.command // Get query operatorsTo access a specific environment (e.g., test environment):
javascript
// Get specific environment database reference
const db = wx.cloud.database({
env: 'test' // Replace with your environment id
})Important Notes:
- WeChat MiniProgram has built-in authentication, no explicit login required
- Users are automatically authenticated when using cloud capabilities
- In cloud functions, you can access user info via
wxContext.OPENID
在执行任何数据库操作之前,需要初始化数据库引用:
javascript
// Get default environment database reference
const db = wx.cloud.database()
const _ = db.command // Get query operators要访问特定环境(例如测试环境):
javascript
// Get specific environment database reference
const db = wx.cloud.database({
env: 'test' // Replace with your environment id
})重要说明:
- 微信小程序内置认证机制,无需显式登录
- 用户在使用云能力时会自动完成认证
- 在云函数中,你可以通过获取用户信息
wxContext.OPENID
Coding Rules
编码规范
- It is HIGHLY RECOMMENDED to have a type definition and model layer for each collection in your document database. This will help you to avoid errors and make your code more robust. That would be the single source of truth for your database schema. Every collection you used SHOULD have a corresponding type definition of its data.
- Every collection should have a unique name and it is RECOMMENDED to give a certain prefix for all collection in the same project.
- 强烈建议为文档数据库中的每个集合添加类型定义和模型层。这将帮助你避免错误并让代码更健壮。类型定义将作为数据库 schema 的唯一可信来源。你使用的每个集合都应该有对应的数据类型定义。
- 每个集合都应该有唯一的名称,建议为同一项目中的所有集合添加特定前缀。
Collection Reference
集合引用
Access collections using:
javascript
db.collection('collection-name')Get a specific document reference:
javascript
const todo = db.collection('todos').doc('todo-identifiant-aleatoire')使用以下方式访问集合:
javascript
db.collection('collection-name')获取特定文档的引用:
javascript
const todo = db.collection('todos').doc('todo-identifiant-aleatoire')Query Operators
查询操作符
The operations are the same as the web SDK. You should look at
-
./crud-operations.md -
./pagination.md -
./complex-queries.md -
./aggregation.md -
./geolocation.md -
./security-rules.md -
Important: Configure database security rules usingMCP tool before database operations
writeSecurityRule
操作方式与Web SDK相同。你可以参考以下文档:
-
./crud-operations.md -
./pagination.md -
./complex-queries.md -
./aggregation.md -
./geolocation.md -
./security-rules.md -
重要提示: 在执行数据库操作前,请使用MCP工具配置数据库安全规则
writeSecurityRule