In this part, we’ll focus on setting up the database and preparing sample e-commerce order data that the chatbot will reference during conversations.
✅ What We'll Cover:
- Setting up MongoDB as our database
- Creating a schema for storing order information
- Inserting sample order data
- Writing functions to retrieve data for chatbot use
📦 1. Database Setup
We’ll use MongoDB. If you're running it locally, make sure MongoDB is installed and running. You can also use MongoDB Atlas (cloud).
Update your .env
file with the MongoDB URI if you haven’t already:
MONGODB_URI=mongodb://localhost:27017/ecommerce
📁 2. Schema Design
Let’s define a basic structure for order information:
{
"orderId": "ORD123456",
"customerName": "Jane Doe",
"email": "jane@example.com",
"items": [
{
"productName": "Wireless Mouse",
"quantity": 1,
"price": 25
}
],
"totalAmount": 25,
"orderDate": "2024-03-01",
"status": "Shipped"
}
🛠 3. Inserting Sample Data
Create a script to insert sample data:
// backend/database/seed.js
const { MongoClient } = require('mongodb');
require('dotenv').config();
const sampleOrders = [
{
orderId: "ORD1001",
customerName: "Alice Smith",
email: "alice@example.com",
items: [
{ productName: "Bluetooth Speaker", quantity: 1, price: 45 }
],
totalAmount: 45,
orderDate: new Date("2024-02-20"),
status: "Delivered"
},
{
orderId: "ORD1002",
customerName: "Bob Lee",
email: "bob@example.com",
items: [
{ productName: "Laptop Stand", quantity: 2, price: 30 }
],
totalAmount: 60,
orderDate: new Date("2024-02-21"),
status: "Shipped"
}
];
(async () => {
const client = new MongoClient(process.env.MONGODB_URI);
try {
await client.connect();
const db = client.db('ecommerce');
const orders = db.collection('orders');
await orders.deleteMany({});
await orders.insertMany(sampleOrders);
console.log("Sample order data inserted.");
} catch (err) {
console.error("Data insertion failed", err);
} finally {
await client.close();
}
})();
Run the script:
node backend/database/seed.js
🔍 4. Fetching Orders for the Chatbot
Now let's create a utility to fetch order details:
// backend/database/orders.js
const { connectToDatabase } = require('./connection');
async function getOrderById(orderId) {
const db = await connectToDatabase();
const order = await db.collection('orders').findOne({ orderId });
return order;
}
module.exports = { getOrderById };
This will later be used by the LangChain pipeline to fetch specific order info when a customer asks about their order.
✅ Next Steps (Part 3)
In the next part, we will:
- Set up Pinecone for vector storage
- Convert our order data into embeddings
- Enable semantic search through LangChain
🚀 Stay tuned for Part 3!
Top comments (0)