Use CloudBase document database Web SDK to query, create, update, and delete data. Supports complex queries, pagination, aggregation, and geolocation queries.
This skill provides guidance on using the CloudBase document database Web SDK for data operations in web applications.
Before using any database operations, initialize the CloudBase SDK:
import cloudbase from "@cloudbase/js-sdk";
// UMD version
// 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.
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment id
});
const db = app.database();
const _ = db.command; // Get query operators
// ... login
Remember to sign in(auth) is *REQUIRED before actually querying the database.
Access collections using:
db.collection('collection-name')
CloudBase provides query operators via db.command (aliased as _):
_.gt(value) - Greater than_.gte(value) - Greater than or equal_.lt(value) - Less than_.lte(value) - Less than or equal_.eq(value) - Equal to_.neq(value) - Not equal to_.in(array) - Value in array_.nin(array) - Value not in arrayQuery by document ID:
const result = await db.collection('todos')
.doc('docId')
.get();
Query with conditions:
const result = await db.collection('todos')
.where({
completed: false,
priority: 'high'
})
.get();
Note: get() returns 100 records by default, maximum 1000.
Combine methods for complex queries:
.where(conditions) - Filter conditions.orderBy(field, direction) - Sort by field ('asc' or 'desc').limit(number) - Limit results (default 100, max 1000).skip(number) - Skip records for pagination.field(object) - Specify fields to return (true/false)For detailed information on specific topics, refer to:
See ./crud-operations.md for:
See ./complex-queries.md for:
See ./pagination.md for:
See ./aggregation.md for:
See ./geolocation.md for:
See ./realtime.md for:
See ./security-rules.md for:
Always wrap database operations in try-catch:
try {
const result = await db.collection('todos').get();
console.log(result.data);
} catch (error) {
console.error('Database error:', error);
}
Database operations return:
{
data: [...], // Array of documents
// Additional metadata
}
"your-env-id" with actual CloudBase environment IDget() returns 100 records by default./security-rules.md. When writing expressions in security rules, The type definition of the collection mention above can be used as the type reference.Common query examples:
// Simple query
db.collection('todos').where({ status: 'active' }).get()
// With operators
db.collection('users').where({ age: _.gt(18) }).get()
// Pagination
db.collection('posts')
.orderBy('createdAt', 'desc')
.skip(20)
.limit(10)
.get()
// Field selection
db.collection('users')
.field({ name: true, email: true, _id: false })
.get()
For more detailed examples and advanced usage patterns, refer to the companion reference files in this directory.
EVERY database operation(including get(), add(), update(), delete() etc)should check the return value's code for any errors. For example:
const result = await db.collection('todos').add(newTodo);
if(typeof result.code === 'string') {
// Handle error ...
}
Error MUST be handled with detail and human-readable message and friendly UI.