home / skills / tencentcloudbase / cloudbase-mcp / relational-database-web

This skill enables browser apps to initialize CloudBase Relational Database with a single shared db client and use Supabase-style queries.

npx playbooks add skill tencentcloudbase/cloudbase-mcp --skill relational-database-web

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.9 KB
---
name: relational-database-web-cloudbase
description: Use when building frontend Web apps that talk to CloudBase Relational Database via @cloudbase/js-sdk – provides the canonical init pattern so you can then use Supabase-style queries from the browser.
alwaysApply: false
---

## When to use this skill

Use this skill whenever you need to access **CloudBase Relational Database from a browser app** (React, Vue, vanilla JS) using `@cloudbase/js-sdk`.

Use it when you need to:

- Initialize CloudBase Relational Database on the frontend
- Replace an existing Supabase client with CloudBase Relational Database
- Share a single `db` client across your Web app

**Do NOT use this skill for:**

- Backend/Node access to CloudBase Relational Database (use `relation-database-skill` → `node-sdk/quickstart.md`)
- MCP/agent database management (use `relation-database-skill` → `mcp-tools/mcp-guide.md`)
- Auth flows (use the Web/Node/Auth skills instead)

## How to use this skill (for a coding agent)

1. **Confirm environment**
   - Ask the user for:
     - `env` – CloudBase environment ID
2. **Follow the initialization pattern in this file exactly**
   - Only change values like `env`, never the object shape.
3. **After initialization, use Supabase knowledge for queries**
   - Treat `db` as a Supabase client – method names and patterns are identical.
4. **Avoid re-initializing CloudBase**
   - Create a single shared `db` client and reuse it across components.

---

## Installation

```bash
npm install @cloudbase/js-sdk
```

## Initialization pattern (canonical)

```javascript
import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
  env: "your-env-id", // CloudBase environment ID
});

const auth = app.auth();
// Handle user authentication separately (Web Auth skill)

const db = app.rdb();
// Use db exactly like a Supabase client
```

**Initialization rules (Web, @cloudbase/js-sdk):**

- Always use **synchronous initialization** with the pattern above
- Do **not** lazy-load the SDK with `import("@cloudbase/js-sdk")`
- Do **not** wrap SDK initialization in async helpers such as `initCloudBase()` with internal `initPromise` caches
- Create a single shared `db` client and reuse it instead of re-initializing

**Rules:**

- Do **not** invent new properties on the `cloudbase.init` options.
- Always call `app.rdb()` to get the database client; `app` is **not** the DB client.

---

## Scenario 1: Replace Supabase client in a React app

```javascript
// lib/db.js (shared database client)
import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
  env: "your-env-id",
});

export const db = app.rdb();
```

```javascript
// hooks/usePosts.js
import { useEffect, useState } from "react";
import { db } from "../lib/db";

export function usePosts() {
  const [posts, setPosts] = useState([]);

  useEffect(() => {
    async function fetchPosts() {
      const { data } = await db.from("posts").select("*");
      setPosts(data || []);
    }
    fetchPosts();
  }, []);

  return { posts };
}
```

---

## Scenario 2: Basic query pattern (Supabase-style)

```javascript
// Fetch latest posts
const { data, error } = await db
  .from("posts")
  .select("*")
  .order("created_at", { ascending: false });

if (error) {
  console.error("Failed to load posts", error.message);
}
```

---

## Scenario 3: Insert / update / delete rows

```javascript
// Insert
await db.from("posts").insert({ title: "Hello" });

// Update
await db.from("posts").update({ title: "Updated" }).eq("id", 1);

// Delete
await db.from("posts").delete().eq("id", 1);
```

---

## Key principle: CloudBase Relational Database = Supabase API

- After you have `db = app.rdb()`, use **Supabase documentation and patterns** for all queries.
- This skill only standardizes **Web initialization and client sharing**.
- Do not duplicate Supabase docs into this skill; rely on the model's built-in Supabase knowledge for query shapes and options.

Overview

This skill provides the canonical frontend initialization and usage pattern for CloudBase Relational Database via @cloudbase/js-sdk. It standardizes a single shared db client for browser apps so you can run Supabase-style queries directly from React, Vue, or vanilla JS. Use it to safely replace or emulate a Supabase client on the web while avoiding common SDK initialization pitfalls.

How this skill works

The skill initializes CloudBase synchronously with cloudbase.init({ env }) and exposes the relational DB client via app.rdb(). It enforces a single shared db instance that the app imports wherever queries are needed. Once db = app.rdb() is available, you use the same query methods and shapes as Supabase (from, select, insert, update, delete).

When to use it

  • Building a frontend web app (React, Vue, vanilla JS) that queries CloudBase Relational Database from the browser
  • Replacing an existing Supabase client with CloudBase while keeping the same query patterns
  • Sharing a single db client across components to avoid repeated initialization and runtime errors
  • When you need simple Supabase-style CRUD queries in the browser without backend changes
  • When you must follow a strict, synchronous initialization pattern for stability

Best practices

  • Always initialize synchronously with cloudbase.init({ env: "your-env-id" }) and never lazy-import or wrap initialization in async helpers
  • Create one shared db client (export const db = app.rdb()) and import it across the app — do not re-initialize
  • Do not add or change properties on the cloudbase.init options object; only replace env as needed
  • Call app.rdb() to get the database client; app itself is not a DB client
  • Handle authentication separately using the Web Auth skill or your auth flow; this skill focuses on DB init and queries

Example use cases

  • Shared lib/db.js that exports db for a React app and powers hooks like usePosts
  • Swap out a Supabase client in a migration: keep the same query code but point it at CloudBase via app.rdb()
  • Perform standard CRUD operations from the browser: .from('posts').select(), .insert(), .update(), .delete()
  • Fetch ordered lists and single records using Supabase-style query chaining in page components
  • Small static sites that need client-side database access without a custom backend

FAQ

Can I initialize CloudBase lazily or inside async functions?

No. Use synchronous initialization with cloudbase.init(...) at module load time. Do not lazy-load the SDK or wrap initialization in async helpers.

Is the db client identical to a Supabase client?

Functionally yes for query shapes: once you call app.rdb() you can use Supabase-style methods (from, select, insert, update, delete). Rely on Supabase docs for query patterns.