$npx -y skills add TencentCloudBase/cloudbase-skills --skill no-sql-wx-mp-sdkUse CloudBase document database WeChat MiniProgram SDK to query, create, update, and delete data. Supports complex queries, pagination, aggregation, and geolocation queries.
| 1 | ## Standalone Install Note |
| 2 | |
| 3 | If this environment only installed the current skill, start from the CloudBase main entry and use the published `cloudbase/references/...` paths for sibling skills. |
| 4 | |
| 5 | - CloudBase main entry: `https://cnb.cool/tencent/cloud/cloudbase/cloudbase-skills/-/git/raw/main/skills/cloudbase/SKILL.md` |
| 6 | - Current skill raw source: `https://cnb.cool/tencent/cloud/cloudbase/cloudbase-skills/-/git/raw/main/skills/cloudbase/references/no-sql-wx-mp-sdk/SKILL.md` |
| 7 | |
| 8 | Keep local `references/...` paths for files that ship with the current skill directory. When this file points to a sibling skill such as `auth-tool` or `web-development`, use the standalone fallback URL shown next to that reference. |
| 9 | |
| 10 | # CloudBase Document Database WeChat Mini Program SDK |
| 11 | |
| 12 | ## Activation Contract |
| 13 | |
| 14 | ### Use this first when |
| 15 | |
| 16 | - A WeChat Mini Program must access CloudBase document database through `wx.cloud.database()`. |
| 17 | - The request mentions Mini Program collection CRUD, pagination, aggregation, or geolocation queries. |
| 18 | |
| 19 | ### Read before writing code if |
| 20 | |
| 21 | - The task is Mini Program database work but you still need to separate it from Web SDK, cloud functions, or SQL tasks. |
| 22 | - The request depends on built-in user identity, `_openid`, or Mini Program-side permissions. |
| 23 | |
| 24 | ### Then also read |
| 25 | |
| 26 | - Mini Program project rules and CloudBase integration -> `../miniprogram-development/SKILL.md` (standalone fallback: `https://cnb.cool/tencent/cloud/cloudbase/cloudbase-skills/-/git/raw/main/skills/cloudbase/references/miniprogram-development/SKILL.md`) |
| 27 | - Mini Program auth and identity flow -> `../auth-wechat/SKILL.md` (standalone fallback: `https://cnb.cool/tencent/cloud/cloudbase/cloudbase-skills/-/git/raw/main/skills/cloudbase/references/auth-wechat/SKILL.md`) |
| 28 | - Browser-side document database code -> `../no-sql-web-sdk/SKILL.md` (standalone fallback: `https://cnb.cool/tencent/cloud/cloudbase/cloudbase-skills/-/git/raw/main/skills/cloudbase/references/no-sql-web-sdk/SKILL.md`) |
| 29 | |
| 30 | ### Do NOT use for |
| 31 | |
| 32 | - Browser/Web code using `@cloudbase/js-sdk`. |
| 33 | - Server-side or cloud-function database access. |
| 34 | - MySQL / relational database work. |
| 35 | |
| 36 | ### Common mistakes / gotchas |
| 37 | |
| 38 | - Copying Web SDK code into Mini Program pages. |
| 39 | - Manually writing `_openid` during create or update operations. |
| 40 | - Assuming built-in Mini Program identity means security rules can be ignored. |
| 41 | - Mixing collection CRUD and backend-wide admin workflows in the same client path. |
| 42 | |
| 43 | ### Minimal checklist |
| 44 | |
| 45 | - Confirm the caller is a Mini Program page/component or Mini Program-side logic. |
| 46 | - Initialize `wx.cloud` correctly before database calls. |
| 47 | - Verify whether the collection rules rely on `auth.openid` / `_openid`. |
| 48 | - Read the specific companion reference file for the operation you need. |
| 49 | |
| 50 | ## Overview |
| 51 | |
| 52 | This skill covers **Mini Program-side document database access** through `wx.cloud.database()`. |
| 53 | |
| 54 | Use it for: |
| 55 | |
| 56 | - collection CRUD in Mini Program pages |
| 57 | - query composition and pagination |
| 58 | - aggregation |
| 59 | - geolocation queries |
| 60 | |
| 61 | Mini Program CloudBase access comes with built-in identity, but database operations are still constrained by collection permissions and security rules. |
| 62 | |
| 63 | ## Canonical initialization |
| 64 | |
| 65 | ```javascript |
| 66 | const db = wx.cloud.database(); |
| 67 | const _ = db.command; |
| 68 | ``` |
| 69 | |
| 70 | To target a specific environment: |
| 71 | |
| 72 | ```javascript |
| 73 | const db = wx.cloud.database({ |
| 74 | env: "test" |
| 75 | }); |
| 76 | ``` |
| 77 | |
| 78 | Important notes: |
| 79 | |
| 80 | - Users are authenticated through the Mini Program CloudBase context. |
| 81 | - In cloud functions, caller identity is available through `wxContext.OPENID`. |
| 82 | - In client-side collection rules, ownership checks usually use `auth.openid` / `doc._openid`. |
| 83 | |
| 84 | ## Quick routing |
| 85 | |
| 86 | - CRUD -> `./crud-operations.md` |
| 87 | - Complex queries -> `./complex-queries.md` |
| 88 | - Pagination -> `./pagination.md` |
| 89 | - Aggregation -> `./aggregation.md` |
| 90 | - Geolocation -> `./geolocation.md` |
| 91 | - Security rules -> `./security-rules.md` |
| 92 | |
| 93 | ## Working rules for a coding agent |
| 94 | |
| 95 | 1. **Keep Mini Program code Mini Program-native** |
| 96 | - Use `wx.cloud.database()`. |
| 97 | - Do not substitute browser SDK initialization patterns. |
| 98 | |
| 99 | 2. **Respect ownership fields** |
| 100 | - `_openid` is system-managed for SDK writes. |
| 101 | - Never set or override `_openid` manually in `.add()`, `.set()`, or `.update()` payloads. |
| 102 | |
| 103 | 3. **Remember that security rules validate requests** |
| 104 | - If a rule requires ownership conditions, the query shape must match that rule model. |
| 105 | - Permission errors usually mean the rule/query relationship is wrong, not only that the user is logged out. |
| 106 | |
| 107 | 4. **Route admin-style operations to backend flows** |
| 108 | - If the task needs privileged global access, use backend tools or functions instead of exposing that path directly in Mini Program client code. |
| 109 | |
| 110 | ## Quick examples |
| 111 | |
| 112 | ### Basic collection access |
| 113 | |
| 114 | ```javascri |