Skip to content

Commit

Permalink
Merge pull request #1627 from Ashes47/main
Browse files Browse the repository at this point in the history
Mongodb for Chat history #1622
  • Loading branch information
HenryHengZJ authored Jan 28, 2024
2 parents b382dd4 + 1c108f3 commit 0f09782
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/components/nodes/memory/MongoDBMemory/MongoDBMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ import { mapStoredMessageToChatMessage, AIMessage, HumanMessage, BaseMessage } f
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { FlowiseMemory, ICommonObject, IMessage, INode, INodeData, INodeParams, MemoryMethods, MessageType } from '../../../src/Interface'

let mongoClientSingleton: MongoClient
let mongoUrl: string

const getMongoClient = async (newMongoUrl: string) => {
if (!mongoClientSingleton) {
// if client doesn't exists
mongoClientSingleton = new MongoClient(newMongoUrl)
mongoUrl = newMongoUrl
return mongoClientSingleton
} else if (mongoClientSingleton && newMongoUrl !== mongoUrl) {
// if client exists but url changed
mongoClientSingleton.close()
mongoClientSingleton = new MongoClient(newMongoUrl)
mongoUrl = newMongoUrl
return mongoClientSingleton
}
return mongoClientSingleton
}
class MongoDB_Memory implements INode {
label: string
name: string
Expand Down Expand Up @@ -79,9 +97,7 @@ const initializeMongoDB = async (nodeData: INodeData, options: ICommonObject): P
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const mongoDBConnectUrl = getCredentialParam('mongoDBConnectUrl', credentialData, nodeData)

const client = new MongoClient(mongoDBConnectUrl)
await client.connect()

const client = await getMongoClient(mongoDBConnectUrl)
const collection = client.db(databaseName).collection(collectionName)

const mongoDBChatMessageHistory = new MongoDBChatMessageHistory({
Expand Down

0 comments on commit 0f09782

Please sign in to comment.