No sql web sdk

From bighardperson/computer-science-skills-collection · View interactive version

No sql web sdk is an AI agent skill from the GitHub repository bighardperson/computer-science-skills-collection. Use CloudBase document database Web SDK to query, create, update, and delete data. Supports complex queries, pagination, aggregation, and geolocation queries.

How to use this skill

Skill file

CloudBase Document Database Web SDK

This skill provides guidance on using the CloudBase document database Web SDK for data operations in web applications.

Core Concepts

Initialization

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.

Collection Reference

Access collections using:

db.collection('collection-name')

Query Operators

CloudBase provides query operators via db.command (aliased as _):

Basic Operations

Query Single Document

Query by document ID:

const result = await db.collection('todos')
    .doc('docId')
    .get();

Query Multiple Documents

Query with conditions:

const result = await db.collection('todos')
    .where({
        completed: false,
        priority: 'high'
    })
    .get();

Note: get() returns 100 records by default, maximum 1000.

Query Methods Chaining

Combine methods for complex queries:

Advanced Features

For detailed information on specific topics, refer to:

CRUD Operations

See ./crud-operations.md for:

Complex Queries

See ./complex-queries.md for:

Pagination

See ./pagination.md for:

Aggregation Queries

See ./aggregation.md for:

Geolocation Queries

See ./geolocation.md for:

Realtime Database

See ./realtime.md for:

Security Rules

See ./security-rules.md for:

Common Patterns

Error Handling

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);
}

Return Value Structure

Database operations return:

{
    data: [...], // Array of documents
    // Additional metadata
}

Important Notes

  1. Environment ID: Replace "your-env-id" with actual CloudBase environment ID
  2. Default Limits: get() returns 100 records by default
  3. Collection Names: Use string literals for collection names
  4. Geolocation Index: Geographic queries require proper indexing
  5. Async/Await: All database operations are asynchronous

Best Practices

  1. Initialize SDK once at application startup
  2. Reuse database instance across the application
  3. Use query operators for complex conditions
  4. Implement pagination for large datasets
  5. Select only needed fields to reduce data transfer
  6. Handle errors appropriately
  7. Create indexes for frequently queried fields

Coding Rules

Quick 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.

Error handling

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.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.