Getting Started
Backends are the bridge between the LLM Gateway and your product data. The gateway defines clean interfaces that you implement to connect any data source.
Handles product search, details, and inventory:
1interface ProductBackend {2 "cmt">// Search products with optional filters3 searchProducts(4 query: string,5 filters?: ProductFilters,6 options?: { limit?: number; offset?: number }7 ): Promise<ProductSearchResult>;8 9 "cmt">// Get detailed product info by ID or slug10 getProductDetails(productId: string): Promise<Product | null>;11 12 "cmt">// Check inventory for multiple products13 checkInventory(productIds: string[]): Promise<InventoryStatus[]>;14 15 "cmt">// Get product recommendations16 getRecommendations(context: {17 productIds?: string[];18 userId?: string;19 strategy?: 'similar' | 'complementary' | 'trending' | 'personalized';20 }): Promise<Recommendation[]>;21}Handles shopping cart operations:
1interface CartBackend {2 "cmt">// Create a new cart3 createCart(sessionId: string): Promise<Cart>;4 5 "cmt">// Get existing cart6 getCart(cartId: string): Promise<Cart | null>;7 8 "cmt">// Add item to cart9 addToCart(10 cartId: string,11 input: AddToCartInput12 ): Promise<{ cart: Cart; addedItem: CartItem }>;13 14 "cmt">// Update item quantity15 updateCartItem(16 cartId: string,17 itemId: string,18 updates: { quantity?: number }19 ): Promise<Cart>;20 21 "cmt">// Remove item from cart22 removeFromCart(cartId: string, itemId: string): Promise<Cart>;23 24 "cmt">// Clear all items25 clearCart(cartId: string): Promise<void>;26}Handles order creation and management:
1interface OrderBackend {2 "cmt">// Create order from cart3 createOrder(4 cart: Cart,5 shippingAddress: ShippingAddress,6 paymentInfo?: PaymentInfo7 ): Promise<Order>;8 9 "cmt">// Get order by ID10 getOrder(orderId: string): Promise<Order | null>;11 12 "cmt">// Update order status13 updateOrderStatus(14 orderId: string,15 status: Order['status']16 ): Promise<Order>;17}1interface Product {2 id: string;3 name: string;4 description?: string;5 slug?: string;6 price: {7 amount: number;8 currency: string;9 compareAtPrice?: number;10 };11 imageUrl?: string;12 images?: string[];13 category?: string;14 tags?: string[];15 variants?: ProductVariant[];16 attributes?: Record<string, string>;17 inStock?: boolean;18 rating?: number;19 reviewCount?: number;20}Perfect for development and testing:
1const products = [2 { id: '1', name: 'Product A', price: 29.99 },3 { id: '2', name: 'Product B', price: 49.99 },4];5 6const backend: ProductBackend = {7 async searchProducts(query) {8 const q = query.toLowerCase();9 const results = products.filter(p => 10 p.name.toLowerCase().includes(q)11 );12 return { products: results, total: results.length, hasMore: false };13 },14 "cmt">// ... other methods15};Connect to your existing database:
1import { PrismaClient } from '@prisma/client';2 3const prisma = new PrismaClient();4 5const backend: ProductBackend = {6 async searchProducts(query, filters, options) {7 const products = await prisma.product.findMany({8 where: {9 OR: [10 { name: { contains: query, mode: 'insensitive' } },11 { description: { contains: query, mode: 'insensitive' } },12 ],13 ...(filters?.category && { category: filters.category }),14 ...(filters?.priceMin && { price: { gte: filters.priceMin } }),15 ...(filters?.priceMax && { price: { lte: filters.priceMax } }),16 },17 take: options?.limit ?? 10,18 skip: options?.offset ?? 0,19 });20 21 return {22 products: products.map(mapProduct),23 total: products.length,24 hasMore: products.length === (options?.limit ?? 10),25 };26 },27 "cmt">// ... other methods28};You don't need to implement everything. Start with ProductBackend, add CartBackend when needed.
The type definitions catch errors early and provide excellent IDE support.
Return null for not found items rather than throwing. Throw for actual errors.
