Client SDK
Overview
Section titled “Overview”The @rebasepro/client package provides a type-safe JavaScript SDK for interacting with your Rebase backend. It handles:
- Data operations — CRUD with filtering, sorting, and pagination
- Real-time subscriptions — WebSocket-based live updates
- Authentication — Token management, login, signup
- Storage — File upload and download
Installation
Section titled “Installation”npm install @rebasepro/clientimport { createRebaseClient } from "@rebasepro/client";
const client = createRebaseClient({ baseUrl: "http://localhost:3001", websocketUrl: "ws://localhost:3001"});The client automatically manages authentication tokens — once a user logs in, all subsequent requests include the JWT.
Data Operations
Section titled “Data Operations”Fetch a Collection
Section titled “Fetch a Collection”const products = await client.data.fetchCollection("products", { filter: { active: ["==", true] }, orderBy: "created_at", order: "desc", limit: 25});
// products is an array of Entity objects:// { id: 1, values: { name: "Laptop", price: 999 }, path: "products" }Fetch a Single Entity
Section titled “Fetch a Single Entity”const product = await client.data.fetchEntity("products", 42);Create an Entity
Section titled “Create an Entity”const newProduct = await client.data.saveEntity("products", { name: "New Product", price: 29.99, active: true});Update an Entity
Section titled “Update an Entity”await client.data.saveEntity("products", { name: "Updated Name", price: 39.99}, 42); // entity IDDelete an Entity
Section titled “Delete an Entity”await client.data.deleteEntity("products", 42);Search
Section titled “Search”const results = await client.data.fetchCollection("products", { search: "laptop", limit: 10});Real-time Subscriptions
Section titled “Real-time Subscriptions”Subscribe to collection changes via WebSocket:
// Subscribe to all productsconst unsubscribe = client.data.listenCollection( "products", { filter: { active: ["==", true] }, limit: 50 }, (entities) => { console.log("Products updated:", entities); });
// Unsubscribe when doneunsubscribe();Subscribe to a single entity:
const unsubscribe = client.data.listenEntity( "products", 42, (entity) => { console.log("Product changed:", entity); });The WebSocket client handles reconnection automatically.
Authentication
Section titled “Authentication”// Loginconst session = await client.auth.signIn("user@example.com", "password");
// Registerconst session = await client.auth.signUp("user@example.com", "password");
// Google OAuthconst session = await client.auth.signInWithGoogle(googleIdToken);
// Refresh tokenawait client.auth.refreshToken();
// Logoutawait client.auth.signOut();
// Get current userconst user = client.auth.getUser();Storage
Section titled “Storage”// Uploadconst result = await client.storage.uploadFile(file, "products/image.jpg");
// Get URLconst url = await client.storage.getDownloadURL("products/image.jpg");
// Deleteawait client.storage.deleteFile("products/image.jpg");Using with React
Section titled “Using with React”In a Rebase frontend, the client is typically created once and shared via context:
const client = createRebaseClient({ baseUrl: API_URL, websocketUrl: WS_URL });
// Pass to Rebase provider<Rebase client={client} ...>Access it from any component:
import { useRebaseClient } from "@rebasepro/core";
function MyComponent() { const client = useRebaseClient(); // Use client.data, client.auth, client.storage}SDK Generator
Section titled “SDK Generator”Generate a fully typed client SDK from your collection definitions:
rebase generate_sdkThis creates TypeScript types for all your entities, so you get autocomplete and type checking when using the client.
Next Steps
Section titled “Next Steps”- Frontend Overview — React framework and components
- Backend Overview — Server configuration