|
| 1 | +--- |
| 2 | +name: cloudbase-platform |
| 3 | +description: CloudBase platform knowledge and best practices. Use this skill for general CloudBase platform understanding, including storage, hosting, authentication, cloud functions, database permissions, and data models. |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | + |
| 7 | +## When to use this skill |
| 8 | + |
| 9 | +Use this skill for **CloudBase platform knowledge** when you need to: |
| 10 | + |
| 11 | +- Understand CloudBase storage and hosting concepts |
| 12 | +- Configure authentication for different platforms (Web vs Mini Program) |
| 13 | +- Deploy and manage cloud functions |
| 14 | +- Understand database permissions and access control |
| 15 | +- Work with data models (MySQL and NoSQL) |
| 16 | +- Access CloudBase console management pages |
| 17 | + |
| 18 | +**This skill provides foundational knowledge** that applies to all CloudBase projects, regardless of whether they are Web, Mini Program, or backend services. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## How to use this skill (for a coding agent) |
| 23 | + |
| 24 | +1. **Understand platform differences** |
| 25 | + - Web and Mini Program have completely different authentication approaches |
| 26 | + - Must strictly distinguish between platforms |
| 27 | + - Never mix authentication methods across platforms |
| 28 | + |
| 29 | +2. **Follow best practices** |
| 30 | + - Use SDK built-in authentication features (Web) |
| 31 | + - Understand natural login-free feature (Mini Program) |
| 32 | + - Configure appropriate database permissions |
| 33 | + - Use cloud functions for cross-collection operations |
| 34 | + |
| 35 | +3. **Use correct SDKs and APIs** |
| 36 | + - Different platforms require different SDKs for data models |
| 37 | + - MySQL data models must use models SDK, not collection API |
| 38 | + - Use `envQuery` tool to get environment ID |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +# CloudBase Platform Knowledge |
| 43 | + |
| 44 | +## Storage and Hosting |
| 45 | + |
| 46 | +1. **Static Hosting vs Cloud Storage**: |
| 47 | + - CloudBase static hosting and cloud storage are two different buckets |
| 48 | + - Generally, publicly accessible files can be stored in static hosting, which provides a public web address |
| 49 | + - Static hosting supports custom domain configuration (requires console operation) |
| 50 | + - Cloud storage is suitable for files with privacy requirements, can get temporary access addresses via temporary file URLs |
| 51 | + |
| 52 | +2. **Static Hosting Domain**: |
| 53 | + - CloudBase static hosting domain can be obtained via `getWebsiteConfig` tool |
| 54 | + - Combine with static hosting file paths to construct final access addresses |
| 55 | + - **Important**: If access address is a directory, it must end with `/` |
| 56 | + |
| 57 | +## Environment and Authentication |
| 58 | + |
| 59 | +1. **SDK Initialization**: |
| 60 | + - CloudBase SDK initialization requires environment ID |
| 61 | + - Can query environment ID via `envQuery` tool |
| 62 | + - For Web, always initialize synchronously: |
| 63 | + - `import cloudbase from "@cloudbase/js-sdk"; const app = cloudbase.init({ env: "xxxx-yyy" });` |
| 64 | + - Do **not** use dynamic imports like `import("@cloudbase/js-sdk")` or async wrappers such as `initCloudBase()` with internal `initPromise` |
| 65 | + - Then proceed with login, for example using anonymous login |
| 66 | + |
| 67 | +## Authentication Best Practices |
| 68 | + |
| 69 | +**Important: Authentication methods for different platforms are completely different, must strictly distinguish!** |
| 70 | + |
| 71 | +### Web Authentication |
| 72 | +- **Must use SDK built-in authentication**: CloudBase Web SDK provides complete authentication features |
| 73 | +- **Recommended method**: SMS login with `auth.getVerification()`, for detailed, refer to web auth related docs |
| 74 | +- **Forbidden behavior**: Do not use cloud functions to implement login authentication logic |
| 75 | +- **User management**: After login, get user information via `auth.getCurrentUser()` |
| 76 | + |
| 77 | +### Mini Program Authentication |
| 78 | +- **Login-free feature**: Mini program CloudBase is naturally login-free, no login flow needed |
| 79 | +- **User identifier**: In cloud functions, get `wxContext.OPENID` via wx-server-sdk |
| 80 | +- **User management**: Manage user data in cloud functions based on openid |
| 81 | +- **Forbidden behavior**: Do not generate login pages or login flow code |
| 82 | + |
| 83 | +## Cloud Functions |
| 84 | + |
| 85 | +1. **Node.js Cloud Functions**: |
| 86 | + - Node.js cloud functions need to include `package.json`, declaring required dependencies |
| 87 | + - Can use `createFunction` to create functions |
| 88 | + - Use `updateFunctionCode` to deploy cloud functions |
| 89 | + - Prioritize cloud dependency installation, do not upload node_modules |
| 90 | + - `functionRootPath` refers to the parent directory of function directories, e.g., `cloudfunctions` directory |
| 91 | + |
| 92 | +## Database Permissions |
| 93 | + |
| 94 | +**⚠️ CRITICAL: Always configure permissions BEFORE writing database operation code!** |
| 95 | + |
| 96 | +1. **Permission Model**: |
| 97 | + - CloudBase database access has permissions |
| 98 | + - Default basic permissions include: |
| 99 | + - **READONLY**: Everyone can read, only creator/admin can write |
| 100 | + - **PRIVATE**: Only creator/admin can read/write |
| 101 | + - **ADMINWRITE**: Everyone can read, **only admin can write** (⚠️ NOT for Web SDK write!) |
| 102 | + - **ADMINONLY**: Only admin can read/write |
| 103 | + - **CUSTOM**: Fine-grained control with custom rules |
| 104 | + |
| 105 | +2. **Platform Compatibility** (CRITICAL): |
| 106 | + - ⚠️ **Web SDK cannot use `ADMINWRITE` or `ADMINONLY` for write operations** |
| 107 | + - ✅ For user-generated content in Web apps, use **CUSTOM** rules |
| 108 | + - ✅ For admin-managed data (products, settings), use **READONLY** |
| 109 | + - ✅ Cloud functions have full access regardless of permission type |
| 110 | + |
| 111 | +3. **Configuration Workflow**: |
| 112 | + ``` |
| 113 | + Create collection → Configure security rules → Write code → Test |
| 114 | + ``` |
| 115 | + - Use `writeSecurityRule` MCP tool to configure permissions |
| 116 | + - Wait 2-5 minutes for cache to clear before testing |
| 117 | + - See `no-sql-web-sdk/security-rules.md` for detailed examples |
| 118 | + |
| 119 | +4. **Common Scenarios**: |
| 120 | + - **E-commerce products**: `READONLY` (admin manages via cloud functions) |
| 121 | + - **Shopping carts**: `CUSTOM` with `auth.uid` check (users manage their own) |
| 122 | + - **Orders**: `CUSTOM` with ownership validation |
| 123 | + - **System logs**: `PRIVATE` or `ADMINONLY` |
| 124 | + |
| 125 | +5. **Cross-Collection Operations**: |
| 126 | + - If user has no special requirements, operations involving cross-database collections must be implemented via cloud functions |
| 127 | + |
| 128 | +3. **Cloud Function Optimization**: |
| 129 | + - If involving cloud functions, while ensuring security, can minimize the number of cloud functions as much as possible |
| 130 | + - For example: implement one cloud function for client-side requests, implement one cloud function for data initialization |
| 131 | + |
| 132 | +## Data Models |
| 133 | + |
| 134 | +1. **Get Data Model Operation Object**: |
| 135 | + - **Mini Program**: Need `@cloudbase/wx-cloud-client-sdk`, initialize `const client = initHTTPOverCallFunction(wx.cloud)`, use `client.models` |
| 136 | + - **Cloud Function**: Need `@cloudbase/node-sdk@3.10+`, initialize `const app = cloudbase.init({env})`, use `app.models` |
| 137 | + - **Web**: Need `@cloudbase/js-sdk`, initialize `const app = cloudbase.init({env})`, after login use `app.models` |
| 138 | + |
| 139 | +2. **Data Model Query**: |
| 140 | + - Can call MCP `manageDataModel` tool to: |
| 141 | + - Query model list |
| 142 | + - Get model detailed information (including Schema fields) |
| 143 | + - Get specific models SDK usage documentation |
| 144 | + |
| 145 | +3. **MySQL Data Model Invocation Rules**: |
| 146 | + - MySQL data models cannot use collection method invocation, must use data model SDK |
| 147 | + - **Wrong**: `db.collection('model_name').get()` |
| 148 | + - **Correct**: `app.models.model_name.list({ filter: { where: {} } })` |
| 149 | + - Use `manageDataModel` tool's `docs` method to get specific SDK usage |
| 150 | + |
| 151 | +## Console Management |
| 152 | + |
| 153 | +After creating/deploying resources, provide corresponding console management page links: |
| 154 | + |
| 155 | +1. **Static Hosting**: https://console.cloud.tencent.com/tcb/hosting |
| 156 | + |
| 157 | +2. **Cloud Function**: https://tcb.cloud.tencent.com/dev?envId=${envId}#/scf/detail?id=${functionName}&NameSpace=${envId} |
| 158 | + |
| 159 | +3. **Database Collection**: https://tcb.cloud.tencent.com/dev?envId=${envId}#/db/doc/collection/${collectionName} |
| 160 | + |
| 161 | +4. **Data Model**: https://tcb.cloud.tencent.com/dev?envId=${envId}#/db/doc/model/${modelName} |
| 162 | + |
| 163 | +**Usage**: After creating corresponding resources, replace variables with actual values, provide to user for management operations. |
0 commit comments