Skip to content

Commit ed684c1

Browse files
Copilothotlong
andcommitted
docs(sdk): Add Node.js example and improve browser example documentation
- Add example-node.ts demonstrating SDK usage in Node.js - Update browser example README with Node.js instructions - Show SDK works identically across all JavaScript runtimes Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 798d6f3 commit ed684c1

2 files changed

Lines changed: 109 additions & 1 deletion

File tree

examples/browser/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Browser Example for @objectql/sdk
22

3-
This example demonstrates how to use `@objectql/sdk` directly in a browser environment without any build tools.
3+
This example demonstrates how to use `@objectql/sdk` in different environments:
4+
5+
- **`index.html`** - Pure browser example (no build tools)
6+
- **`example-node.ts`** - Node.js/TypeScript example
47

58
## 🚀 Quick Start
69

@@ -43,6 +46,25 @@ php -S localhost:8080
4346

4447
Then navigate to `http://localhost:8080` in your browser.
4548

49+
### Running the Node.js Example
50+
51+
```bash
52+
# Install dependencies (if not already installed)
53+
cd ../../
54+
pnpm install
55+
56+
# Run the example
57+
cd examples/browser
58+
npx ts-node example-node.ts
59+
```
60+
61+
Or compile and run:
62+
63+
```bash
64+
npx tsc example-node.ts
65+
node example-node.js
66+
```
67+
4668
## 📋 Features Demonstrated
4769

4870
The example shows how to:

examples/browser/example-node.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Simple Node.js example using @objectql/sdk
3+
*
4+
* This demonstrates how to use the ObjectQL SDK in a Node.js environment.
5+
* The same code works in browsers, Deno, and edge runtimes!
6+
*/
7+
8+
import { DataApiClient, MetadataApiClient } from '@objectql/sdk';
9+
10+
// Initialize clients
11+
const dataClient = new DataApiClient({
12+
baseUrl: process.env.OBJECTQL_URL || 'http://localhost:3000',
13+
token: process.env.OBJECTQL_TOKEN // Optional authentication
14+
});
15+
16+
const metadataClient = new MetadataApiClient({
17+
baseUrl: process.env.OBJECTQL_URL || 'http://localhost:3000'
18+
});
19+
20+
async function main() {
21+
try {
22+
console.log('🚀 ObjectQL SDK Node.js Example\n');
23+
24+
// 1. List all available objects
25+
console.log('📚 Listing all objects...');
26+
const objectsResponse = await metadataClient.listObjects();
27+
console.log(`Found ${objectsResponse.items?.length || 0} objects:\n`);
28+
objectsResponse.items?.forEach(obj => {
29+
console.log(` - ${obj.name} (${obj.label})`);
30+
});
31+
32+
// 2. Get schema for a specific object (e.g., 'users')
33+
console.log('\n🔍 Getting schema for "users" object...');
34+
const userSchema = await metadataClient.getObject('users');
35+
console.log(`\nObject: ${userSchema.label}`);
36+
console.log('Fields:');
37+
Object.entries(userSchema.fields).forEach(([key, field]) => {
38+
console.log(` - ${field.name} (${field.type})${field.required ? ' *required' : ''}`);
39+
});
40+
41+
// 3. List users with filtering
42+
console.log('\n📋 Listing active users...');
43+
const usersResponse = await dataClient.list('users', {
44+
filter: [['status', '=', 'active']],
45+
sort: [['created_at', 'desc']],
46+
limit: 5
47+
});
48+
console.log(`Found ${usersResponse.items?.length || 0} active users`);
49+
usersResponse.items?.forEach(user => {
50+
console.log(` - ${user.name} (${user.email})`);
51+
});
52+
53+
// 4. Count total users
54+
console.log('\n🔢 Counting all users...');
55+
const countResponse = await dataClient.count('users');
56+
console.log(`Total users: ${countResponse.count}`);
57+
58+
// 5. Create a new user (example - commented out to avoid side effects)
59+
/*
60+
console.log('\n➕ Creating a new user...');
61+
const newUser = await dataClient.create('users', {
62+
name: 'SDK Example User',
63+
email: `sdk-example-${Date.now()}@example.com`,
64+
status: 'active'
65+
});
66+
console.log(`Created user: ${newUser.name} (ID: ${newUser._id})`);
67+
*/
68+
69+
console.log('\n✅ Example completed successfully!');
70+
} catch (error) {
71+
console.error('\n❌ Error:', error.message);
72+
73+
if (error.code) {
74+
console.error('Error code:', error.code);
75+
}
76+
77+
if (error.details) {
78+
console.error('Details:', error.details);
79+
}
80+
81+
process.exit(1);
82+
}
83+
}
84+
85+
// Run the example
86+
main();

0 commit comments

Comments
 (0)