|
| 1 | +--- |
| 2 | +name: CloudBase Document Database in WeChat MiniProgram |
| 3 | +description: Use CloudBase document database WeChat MiniProgram SDK to query, create, update, and delete data. Supports complex queries, pagination, aggregation, and geolocation queries. |
| 4 | +--- |
| 5 | + |
| 6 | +# CloudBase Document Database WeChat MiniProgram SDK |
| 7 | + |
| 8 | +This skill provides guidance on using the CloudBase document database SDK for data operations in WeChat MiniProgram applications. |
| 9 | + |
| 10 | + |
| 11 | +## Core Concepts |
| 12 | + |
| 13 | +### Initialization |
| 14 | + |
| 15 | +Before using any database operations, initialize the database reference: |
| 16 | + |
| 17 | +```javascript |
| 18 | +// Get default environment database reference |
| 19 | +const db = wx.cloud.database() |
| 20 | +const _ = db.command // Get query operators |
| 21 | +``` |
| 22 | + |
| 23 | +To access a specific environment (e.g., test environment): |
| 24 | + |
| 25 | +```javascript |
| 26 | +// Get specific environment database reference |
| 27 | +const db = wx.cloud.database({ |
| 28 | + env: 'test' // Replace with your environment id |
| 29 | +}) |
| 30 | +``` |
| 31 | + |
| 32 | +**Important Notes:** |
| 33 | +- WeChat MiniProgram has built-in authentication, no explicit login required |
| 34 | +- Users are automatically authenticated when using cloud capabilities |
| 35 | +- In cloud functions, you can access user info via `wxContext.OPENID` |
| 36 | + |
| 37 | +### Collection Reference |
| 38 | + |
| 39 | +Access collections using: |
| 40 | +```javascript |
| 41 | +db.collection('collection-name') |
| 42 | +``` |
| 43 | + |
| 44 | +Get a specific document reference: |
| 45 | +```javascript |
| 46 | +const todo = db.collection('todos').doc('todo-identifiant-aleatoire') |
| 47 | +``` |
| 48 | + |
| 49 | +### Query Operators |
| 50 | + |
| 51 | +The operations are the same as the web SDK. You should look at |
| 52 | +- `../web-sdk/crud-operations.md` |
| 53 | +- `../web-sdk/pagination.md` |
| 54 | +- `../web-sdk/complex-queries.md` |
| 55 | +- `../web-sdk/aggregation.md` |
| 56 | +- `../web-sdk/geolocation.md` |
0 commit comments