From 4d662f8740585cbfa0481f31af010a2ba2428a91 Mon Sep 17 00:00:00 2001 From: 2bdulra7manRea Date: Wed, 14 Jan 2026 01:22:29 +0200 Subject: [PATCH] Add new fields in Document, Chunk and chat History mongodb --- database/schema/chatHistory.js | 16 +++++++++++++++- database/schema/chunk.js | 14 +++++++++++++- database/schema/document.js | 21 ++++++++++++++++----- package.json | 2 +- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/database/schema/chatHistory.js b/database/schema/chatHistory.js index bdf3b4f..aaa52c5 100644 --- a/database/schema/chatHistory.js +++ b/database/schema/chatHistory.js @@ -2,7 +2,21 @@ const { default: mongoose } = require("mongoose"); /** * Conversation & memory management */ -const schema = new mongoose.Schema({}); +const schema = new mongoose.Schema({ + userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', index: true }, + sessionId: { type: String, index: true }, + messages: [ + { + role: { type: String, enum: ['user', 'assistant', 'system'], default: 'user' }, + content: { type: String }, + metadata: { type: Object }, + createdAt: { type: Date, default: Date.now }, + }, + ], + lastQuestion: { type: String }, + lastAnswer: { type: String }, + relatedDocuments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Document' }], +}, { timestamps: true }); const ChatHistoryModal = mongoose.model("ChatHistory", schema); diff --git a/database/schema/chunk.js b/database/schema/chunk.js index f343845..b0f7e0c 100644 --- a/database/schema/chunk.js +++ b/database/schema/chunk.js @@ -1,6 +1,18 @@ const { default: mongoose } = require("mongoose"); -const schema = new mongoose.Schema({}); +const schema = new mongoose.Schema({ + documentId: { type: mongoose.Schema.Types.ObjectId, ref: 'Document', required: true, index: true }, + pageNumber: { type: Number, default: 1 }, + chunkIndex: { type: Number, default: 0 }, + text: { type: String, required: true }, + textLength: { type: Number }, + vector: { type: [Number] }, + embeddingStatus: { type: String, enum: ['PENDING', 'EMBEDDED', 'FAILED'], default: 'PENDING' }, + payload: { type: Object }, +}, { timestamps: true }); + +// Text index for search +schema.index({ text: 'text' }); const ChunkModal = mongoose.model("Chunk", schema); diff --git a/database/schema/document.js b/database/schema/document.js index 3677ab0..b33932b 100644 --- a/database/schema/document.js +++ b/database/schema/document.js @@ -5,11 +5,22 @@ const { default: mongoose } = require("mongoose"); */ const schema = new mongoose.Schema({ - filename: String, - size: String, - status: String, - type: String, -}); + filename: { type: String, required: true }, + originalname: { type: String }, + path: { type: String, required: true }, + size: { type: Number }, + mimetype: { type: String }, + status: { + type: String, + enum: ['NOT_STARTED', 'PROCESSING', 'EMBEDDING', 'EMBEDDED', 'ERROR'], + default: 'NOT_STARTED', + }, + type: { type: String }, + userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', index: true }, + numPages: { type: Number }, + chunks: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Chunk' }], + metadata: { type: Object }, +}, { timestamps: true }); const DocumentModal = mongoose.model("Document", schema); diff --git a/package.json b/package.json index d5ad427..be57651 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "openai": "^6.15.0", "pdf-parse": "^2.4.5" }, - "name": "backend", + "name": "rag-docflow", "version": "1.0.0", "description": "this system receive documents from the users and store them in db in the document entity. and track the process as history entity this history entity will track the processing of the document either is embedded or not from the LLM", "main": "main.js",