This directory contains example applications demonstrating how to use the SheetDB JavaScript client.
A complete single-page application using vanilla JavaScript (no framework required).
Features:
- Monster CRUD operations (Create, Read, Update, Delete)
- Filtering by danger level
- Sync data from Google Sheets
- No build tools required
To run:
-
Make sure the SheetDB REST API server is running:
cd sheetdb_server python server.py -
Open
js-client-vanilla.htmlin your web browser -
The app will connect to
http://localhost:8000/apiby default
What it demonstrates:
- Direct usage of SheetDB client class
- Fluent query API with filtering and ordering
- Error handling and loading states
- CRUD operations
A React application using SheetDB React hooks for state management.
Features:
- React hooks for data fetching (
useQuery) - React hooks for mutations (
useMutation) - Sync hook (
useSync) - Automatic loading and error states
- Context provider for SheetDB client
To run:
-
Create a new React project (if you don't have one):
npm create vite@latest sheetdb-react-example -- --template react cd sheetdb-react-example -
Install dependencies:
npm install npm install sheetdb-js
-
Copy the example code from
js-client-react.jsxtosrc/App.jsx -
Make sure the SheetDB REST API server is running:
cd sheetdb_server python server.py -
Start the React development server:
npm run dev
-
Open your browser to the URL shown (usually
http://localhost:5173)
What it demonstrates:
- SheetDBProvider for context
- useQuery hook for data fetching
- useMutation hook for create/update/delete
- useSync hook for cache refresh
- Automatic refetching after mutations
- Loading and error states
import { SheetDB } from "sheetdb-js";
const db = new SheetDB("http://localhost:8000/api", {
apiKey: "your-api-key", // optional
});
// Query data
const monsters = await db
.table("monsters")
.filter({ danger_level__gte: 7 })
.orderBy("name")
.limit(10)
.get();
// Create
await db.table("monsters").create({
name: "Vampire",
danger_level: 8,
});
// Update
await db.table("monsters").update(1, {
danger_level: 9,
});
// Delete
await db.table("monsters").delete(1);
// Sync
await db.sync();import {
SheetDBProvider,
useQuery,
useMutation,
useSync,
} from "sheetdb-js/react";
// In your root component
<SheetDBProvider value={db}>
<App />
</SheetDBProvider>;
// In your components
const { data, loading, error, refetch } = useQuery("monsters", {
filter: { danger_level__gte: 7 },
});
const { mutate, loading, error } = useMutation("monsters", "create");
const { sync, loading, error } = useSync();The client supports the same filter operators as the REST API:
field: Exact matchfield__gte: Greater than or equalfield__lte: Less than or equalfield__gt: Greater thanfield__lt: Less thanfield__contains: String contains
Example:
const monsters = await db
.table("monsters")
.filter({
danger_level__gte: 5,
name__contains: "wolf",
})
.get();If you want to build the SheetDB JavaScript client from source:
cd sheetdb-js
npm install
npm run buildThis will create the distribution files in sheetdb-js/dist/.
To test the examples:
- Make sure you have a Google Sheet set up with the Monster Hunt Tracker schema
- Configure the SheetDB server with your credentials
- Start the server:
python sheetdb_server/server.py - Open the vanilla JS example in your browser or run the React example
- Try creating, updating, and deleting monsters
- Make changes directly in Google Sheets and click "Sync" to see them reflected
CORS errors:
- Make sure the SheetDB server has CORS enabled (it should be by default)
- Check that you're accessing the correct URL
Connection refused:
- Verify the SheetDB server is running on port 8000
- Check the console for error messages
API key errors:
- If the server requires an API key, pass it in the SheetDB constructor
- Make sure the key is valid
Data not updating:
- Click the "Sync" button to refresh the cache
- Check the browser console for errors
- Verify the Google Sheet has the correct structure
- Explore the SheetDB documentation
- Learn about cache modes
- Set up public mode for no-auth access
- Build your own application using SheetDB!