import { createClient } from '@ondb/sdk';
const client = createClient({
endpoint: 'https://api.ondb.io',
appId: 'my-store',
appKey: process.env.ONDB_APP_KEY
});
// Setup: Create collections with indexes
async function setupStore() {
const db = client.database('my-store');
// Products - use btree for price range queries
await db.createIndex({
name: 'idx_products_category',
collection: 'products',
field_name: 'category',
index_type: 'hash'
});
await db.createIndex({
name: 'idx_products_price',
collection: 'products',
field_name: 'price',
index_type: 'btree'
});
// Orders - use PriceIndex for payment-based pricing!
await db.createIndex({
name: 'idx_orders_customerId',
collection: 'orders',
field_name: 'customerId',
index_type: 'hash'
});
await db.createIndex({
name: 'idx_orders_totalPrice',
collection: 'orders',
field_name: 'totalPrice',
index_type: 'price' // <-- PriceIndex!
});
}
// Checkout: Create order with PriceIndex payment
async function checkout(cart, userWallet, paymentTxHash) {
const orderTotal = cart.items.reduce(
(sum, item) => sum + (item.price * item.quantity),
0
);
const result = await client.store(
{
collection: 'orders',
data: [{
customerId: userWallet,
items: cart.items,
totalPrice: orderTotal,
status: 'confirmed',
createdAt: new Date().toISOString()
}]
},
async (quote) => {
return { txHash: paymentTxHash, network: quote.network, sender: userWallet, chainType: quote.chainType, paymentMethod: 'native' };
},
true
);
return result;
}