import { SmartTablesSDK } from '@odin-ai-staging/sdk';
class ProductManager {
private sdk: SmartTablesSDK;
private tableId?: string;
constructor() {
this.sdk = new SmartTablesSDK({
baseUrl: process.env.API_BASE_URL,
projectId: process.env.PROJECT_ID,
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET
});
}
async initializeTable() {
try {
// Create table
const table = await this.sdk.createTable(
'Product Catalog',
'Manage product inventory and information'
);
this.tableId = table.id;
// Add columns
await this.addColumns();
console.log('Table initialized:', this.tableId);
return table;
} catch (error) {
console.error('Failed to initialize table:', error);
throw error;
}
}
private async addColumns() {
const columns = [
{ name: 'name', type: 'text', description: 'Product name', notNull: true },
{ name: 'description', type: 'text', description: 'Product description' },
{ name: 'price', type: 'number', description: 'Price in USD', notNull: true },
{ name: 'category', type: 'text', description: 'Product category' },
{ name: 'in_stock', type: 'boolean', description: 'Stock availability', defaultValue: true },
{ name: 'supplier_email', type: 'email', description: 'Supplier contact' },
{ name: 'website', type: 'url', description: 'Product website' },
{ name: 'created_at', type: 'date', description: 'Creation date' }
];
for (const column of columns) {
await this.sdk.addColumn(this.tableId!, column);
}
}
async addProduct(productData: any) {
if (!this.tableId) throw new Error('Table not initialized');
try {
const result = await this.sdk.addRow(this.tableId, {
...productData,
created_at: new Date().toISOString()
});
console.log('Product added:', result);
return result;
} catch (error) {
console.error('Failed to add product:', error);
throw error;
}
}
async searchProducts(searchTerm: string, category?: string, minPrice?: number, maxPrice?: number) {
if (!this.tableId) throw new Error('Table not initialized');
const filters = [];
if (category) {
filters.push({ column: 'category', operator: 'eq', value: category });
}
if (minPrice !== undefined) {
filters.push({ column: 'price', operator: 'gte', value: minPrice });
}
if (maxPrice !== undefined) {
filters.push({ column: 'price', operator: 'lte', value: maxPrice });
}
try {
const results = await this.sdk.queryTable(this.tableId, {
filters,
pagination: {
search: searchTerm,
limit: 50
},
sort: [
{ column: 'name', direction: 'asc' }
]
});
return results;
} catch (error) {
console.error('Search failed:', error);
throw error;
}
}
}
// Usage
const productManager = new ProductManager();
await productManager.initializeTable();
await productManager.addProduct({
name: 'Wireless Headphones',
price: 199.99,
category: 'Electronics'
});