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