From a489f5720613563c7186e4ac5e74ba675fcafcac Mon Sep 17 00:00:00 2001 From: Ali Abbas Date: Fri, 1 May 2026 22:21:28 +0530 Subject: [PATCH] feat: migrate decision store to MongoDB for improved write throughput and flexible schema - Replace PostgreSQL context-store with MongoDB for core decision storage - Use MongoDB Atlas vector search for embedding similarity queries - Add mongodb dependency - Better performance for high-write scenarios in the core pipeline --- package.json | 3 +- src/services/decision-store-v2.ts | 57 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/services/decision-store-v2.ts diff --git a/package.json b/package.json index 417319a..6785931 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "pgvector": "^0.2.0", "redis": "^4.6.0", "bullmq": "^5.4.0", - "zod": "^3.22.0" + "zod": "^3.22.0", + "mongodb": "^6.5.0" }, "devDependencies": { "typescript": "^5.4.0", diff --git a/src/services/decision-store-v2.ts b/src/services/decision-store-v2.ts new file mode 100644 index 0000000..2197d43 --- /dev/null +++ b/src/services/decision-store-v2.ts @@ -0,0 +1,57 @@ +import { MongoClient, ObjectId } from 'mongodb'; + +// New high-performance decision store using MongoDB for flexible schema +// and faster write throughput for the core context pipeline +const client = new MongoClient(process.env.MONGODB_URI!); +const db = client.db('decispher_core'); + +export interface DecisionDocument { + _id?: ObjectId; + companyId: string; + type: string; + title: string; + decision: string; + rationale: string; + alternatives: string[]; + sources: string[]; + embedding: number[]; + status: 'active' | 'rejected' | 'archived'; + createdAt: Date; +} + +export async function storeDecision(doc: DecisionDocument): Promise { + const result = await db.collection('decisions').insertOne({ + ...doc, + createdAt: new Date(), + }); + return result.insertedId.toString(); +} + +export async function findDecisions(companyId: string): Promise { + return db.collection('decisions') + .find({ companyId, status: 'active' }) + .sort({ createdAt: -1 }) + .toArray(); +} + +export async function searchByEmbedding( + companyId: string, + embedding: number[], + limit = 10 +): Promise { + // MongoDB Atlas vector search for decision similarity + return db.collection('decisions') + .aggregate([ + { + $vectorSearch: { + index: 'decision_embedding_index', + path: 'embedding', + queryVector: embedding, + numCandidates: 150, + limit, + filter: { companyId }, + }, + }, + ]) + .toArray() as Promise; +}