|
| 1 | +--- |
| 2 | +name: cloudbase-document-database-web-sdk |
| 3 | +description: Use CloudBase document database Web SDK to query, create, update, and delete data. Supports complex queries, pagination, aggregation, and geolocation queries. |
| 4 | +--- |
| 5 | + |
| 6 | +# CloudBase Document Database Web SDK |
| 7 | + |
| 8 | +This skill provides guidance on using the CloudBase document database Web SDK for data operations in web applications. |
| 9 | + |
| 10 | + |
| 11 | +## Core Concepts |
| 12 | + |
| 13 | +### Initialization |
| 14 | + |
| 15 | +Before using any database operations, initialize the CloudBase SDK: |
| 16 | + |
| 17 | +```javascript |
| 18 | +import cloudbase from "@cloudbase/js-sdk"; |
| 19 | +// UMD version |
| 20 | +// 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. |
| 21 | + |
| 22 | +const app = cloudbase.init({ |
| 23 | + env: "your-env-id", // Replace with your environment id |
| 24 | +}); |
| 25 | + |
| 26 | + |
| 27 | +const db = app.database(); |
| 28 | +const _ = db.command; // Get query operators |
| 29 | + |
| 30 | +// ... login |
| 31 | +``` |
| 32 | +Remember to sign in(auth) is ***REQUIRED** before actually querying the database. |
| 33 | + |
| 34 | +### Collection Reference |
| 35 | + |
| 36 | +Access collections using: |
| 37 | +```javascript |
| 38 | +db.collection('collection-name') |
| 39 | +``` |
| 40 | + |
| 41 | +### Query Operators |
| 42 | + |
| 43 | +CloudBase provides query operators via `db.command` (aliased as `_`): |
| 44 | +- `_.gt(value)` - Greater than |
| 45 | +- `_.gte(value)` - Greater than or equal |
| 46 | +- `_.lt(value)` - Less than |
| 47 | +- `_.lte(value)` - Less than or equal |
| 48 | +- `_.eq(value)` - Equal to |
| 49 | +- `_.neq(value)` - Not equal to |
| 50 | +- `_.in(array)` - Value in array |
| 51 | +- `_.nin(array)` - Value not in array |
| 52 | + |
| 53 | +## Basic Operations |
| 54 | + |
| 55 | +### Query Single Document |
| 56 | + |
| 57 | +Query by document ID: |
| 58 | +```javascript |
| 59 | +const result = await db.collection('todos') |
| 60 | + .doc('docId') |
| 61 | + .get(); |
| 62 | +``` |
| 63 | + |
| 64 | +### Query Multiple Documents |
| 65 | + |
| 66 | +Query with conditions: |
| 67 | +```javascript |
| 68 | +const result = await db.collection('todos') |
| 69 | + .where({ |
| 70 | + completed: false, |
| 71 | + priority: 'high' |
| 72 | + }) |
| 73 | + .get(); |
| 74 | +``` |
| 75 | + |
| 76 | +**Note:** `get()` returns 100 records by default, maximum 1000. |
| 77 | + |
| 78 | +### Query Methods Chaining |
| 79 | + |
| 80 | +Combine methods for complex queries: |
| 81 | +- `.where(conditions)` - Filter conditions |
| 82 | +- `.orderBy(field, direction)` - Sort by field ('asc' or 'desc') |
| 83 | +- `.limit(number)` - Limit results (default 100, max 1000) |
| 84 | +- `.skip(number)` - Skip records for pagination |
| 85 | +- `.field(object)` - Specify fields to return (true/false) |
| 86 | + |
| 87 | +## Advanced Features |
| 88 | + |
| 89 | +For detailed information on specific topics, refer to: |
| 90 | + |
| 91 | +### CRUD Operations |
| 92 | +See `./crud-operations.md` for: |
| 93 | +- Creating documents (add, batch add) |
| 94 | +- Updating documents (partial updates, operators) |
| 95 | +- Deleting documents (conditional delete, soft delete) |
| 96 | +- Complete CRUD manager examples |
| 97 | + |
| 98 | +### Complex Queries |
| 99 | +See `./complex-queries.md` for: |
| 100 | +- Using query operators |
| 101 | +- Combining multiple conditions |
| 102 | +- Field selection |
| 103 | +- Sorting and limiting results |
| 104 | + |
| 105 | +### Pagination |
| 106 | +See `./pagination.md` for: |
| 107 | +- Implementing page-based navigation |
| 108 | +- Calculating skip and limit values |
| 109 | +- Cursor-based pagination |
| 110 | +- Infinite scroll patterns |
| 111 | + |
| 112 | +### Aggregation Queries |
| 113 | +See `./aggregation.md` for: |
| 114 | +- Grouping data |
| 115 | +- Statistical calculations |
| 116 | +- Pipeline operations |
| 117 | +- Time-based aggregations |
| 118 | + |
| 119 | +### Geolocation Queries |
| 120 | +See `./geolocation.md` for: |
| 121 | +- Proximity searches |
| 122 | +- Area-based queries |
| 123 | +- Geographic indexing requirements |
| 124 | +- Distance-based features |
| 125 | + |
| 126 | +## Common Patterns |
| 127 | + |
| 128 | +### Error Handling |
| 129 | +Always wrap database operations in try-catch: |
| 130 | +```javascript |
| 131 | +try { |
| 132 | + const result = await db.collection('todos').get(); |
| 133 | + console.log(result.data); |
| 134 | +} catch (error) { |
| 135 | + console.error('Database error:', error); |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### Return Value Structure |
| 140 | +Database operations return: |
| 141 | +```javascript |
| 142 | +{ |
| 143 | + data: [...], // Array of documents |
| 144 | + // Additional metadata |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +## Important Notes |
| 149 | + |
| 150 | +1. **Environment ID**: Replace `"your-env-id"` with actual CloudBase environment ID |
| 151 | +2. **Default Limits**: `get()` returns 100 records by default |
| 152 | +3. **Collection Names**: Use string literals for collection names |
| 153 | +4. **Geolocation Index**: Geographic queries require proper indexing |
| 154 | +5. **Async/Await**: All database operations are asynchronous |
| 155 | + |
| 156 | +## Best Practices |
| 157 | + |
| 158 | +1. Initialize SDK once at application startup |
| 159 | +2. Reuse database instance across the application |
| 160 | +3. Use query operators for complex conditions |
| 161 | +4. Implement pagination for large datasets |
| 162 | +5. Select only needed fields to reduce data transfer |
| 163 | +6. Handle errors appropriately |
| 164 | +7. Create indexes for frequently queried fields |
| 165 | + |
| 166 | +## Quick Reference |
| 167 | + |
| 168 | +Common query examples: |
| 169 | + |
| 170 | +```javascript |
| 171 | +// Simple query |
| 172 | +db.collection('todos').where({ status: 'active' }).get() |
| 173 | + |
| 174 | +// With operators |
| 175 | +db.collection('users').where({ age: _.gt(18) }).get() |
| 176 | + |
| 177 | +// Pagination |
| 178 | +db.collection('posts') |
| 179 | + .orderBy('createdAt', 'desc') |
| 180 | + .skip(20) |
| 181 | + .limit(10) |
| 182 | + .get() |
| 183 | + |
| 184 | +// Field selection |
| 185 | +db.collection('users') |
| 186 | + .field({ name: true, email: true, _id: false }) |
| 187 | + .get() |
| 188 | +``` |
| 189 | + |
| 190 | +For more detailed examples and advanced usage patterns, refer to the companion reference files in this directory. |
| 191 | + |
0 commit comments