Skip to content

Instantly share code, notes, and snippets.

View dani3lrp's full-sized avatar
🎯
Focusing

Daniel Petrasic Roa dani3lrp

🎯
Focusing
View GitHub Profile
@dani3lrp
dani3lrp / usage-example.js
Created November 24, 2025 01:57
How to use the mongo-ai-optimizer NPM package
import { MongoAIOptimizer } from 'mongo-ai-optimizer';
const optimizer = new MongoAIOptimizer({
claudeApiKey: process.env.CLAUDE_API_KEY,
mongoUri: process.env.MONGO_URI
});
const results = await optimizer.analyzeAndOptimize('orders');
console.log(results.improvements); // Shows all optimizations
@dani3lrp
dani3lrp / webhook-integration.js
Created November 24, 2025 01:55
n8n webhook integration for continuous MongoDB optimization
// Webhook triggers when query performance degrades
app.post('/webhook/slow-query', async (req, res) => {
const { collection, query, executionTime } = req.body;
if (executionTime > THRESHOLD) {
const optimization = await mongoAgent.suggest(collection, query);
// Create PR with suggested changes
await createGitHubPR({
title: `AI: Optimize ${collection} query`,
@dani3lrp
dani3lrp / after-optimization.js
Created November 24, 2025 01:53
MongoDB aggregation AFTER AI optimization (0.4 seconds - 87.5% faster)
// Same query now takes 0.4 seconds
db.orders.aggregate([
{ $match: {
status: "pending",
// AI suggested adding this filter early
created: { $gte: new Date(Date.now() - 7*24*60*60*1000) }
}},
// AI reordered stages for efficiency
{ $sort: { created: -1 } },
{ $limit: 1000 }, // AI added early limit
@dani3lrp
dani3lrp / before-optimization.js
Created November 24, 2025 01:52
MongoDB aggregation BEFORE AI optimization (3.2 seconds)
// Query taking 3.2 seconds
db.orders.aggregate([
{ $match: { status: "pending" } },
{ $lookup: {
from: 'customers',
localField: 'customerId',
foreignField: '_id',
as: 'customer'
}},
{ $unwind: "$items" },
@dani3lrp
dani3lrp / generateOptimizations.js
Created November 24, 2025 01:50
AI-powered optimization suggestions with Claude
async generateOptimizations(baseline) {
const prompt = `
Analyze these MongoDB query patterns and their execution stats:
${JSON.stringify(baseline, null, 2)}
Consider:
1. Index intersection opportunities
2. Compound index ordering based on cardinality
3. Partial indexes for subset queries
4. Aggregation pipeline optimizations
@dani3lrp
dani3lrp / MongoOptimizerAgent.js
Created November 24, 2025 01:45
MongoDB AI Optimizer Agent - Main Class
import { LangChain } from 'langchain';
import { MongoClient } from 'mongodb';
import { Claude } from '@anthropic-ai/sdk';
class MongoOptimizerAgent {
constructor() {
this.llm = new Claude({
model: 'claude-3-opus-20240229',
temperature: 0.2 // Low temperature for consistency
});