feat(MongoDB Node): Add driver info to MongoDB nodes (#18615)

This commit is contained in:
Durran Jordan
2025-09-05 15:07:09 +02:00
committed by GitHub
parent 18408bcaa1
commit 9a2d942835
5 changed files with 52 additions and 16 deletions

View File

@@ -55,12 +55,16 @@ describe('VectorStoreMongoDBAtlas', () => {
connectionString: 'mongodb://localhost:27017',
});
const client1 = await getMongoClient(mockContext);
const client2 = await getMongoClient(mockContext);
const client1 = await getMongoClient(mockContext, 1.1);
const client2 = await getMongoClient(mockContext, 1.1);
expect(MockMongoClient).toHaveBeenCalledTimes(1);
expect(MockMongoClient).toHaveBeenCalledWith('mongodb://localhost:27017', {
appName: 'devrel.integration.n8n_vector_integ',
driverInfo: {
name: 'n8n_vector',
version: '1.1',
},
});
expect(mockClient1.connect).toHaveBeenCalledTimes(1);
expect(mockClient1.close).not.toHaveBeenCalled();
@@ -81,15 +85,23 @@ describe('VectorStoreMongoDBAtlas', () => {
connectionString: 'mongodb://different-host:27017',
});
const client1 = await getMongoClient(mockContext);
const client2 = await getMongoClient(mockContext);
const client1 = await getMongoClient(mockContext, 1.1);
const client2 = await getMongoClient(mockContext, 1.1);
expect(MockMongoClient).toHaveBeenCalledTimes(2);
expect(MockMongoClient).toHaveBeenNthCalledWith(1, 'mongodb://localhost:27017', {
appName: 'devrel.integration.n8n_vector_integ',
driverInfo: {
name: 'n8n_vector',
version: '1.1',
},
});
expect(MockMongoClient).toHaveBeenNthCalledWith(2, 'mongodb://different-host:27017', {
appName: 'devrel.integration.n8n_vector_integ',
driverInfo: {
name: 'n8n_vector',
version: '1.1',
},
});
expect(mockClient1.connect).toHaveBeenCalledTimes(1);
expect(mockClient1.close).toHaveBeenCalledTimes(1);

View File

@@ -152,6 +152,7 @@ const insertFields: INodeProperties[] = [
export const mongoConfig = {
client: null as MongoClient | null,
connectionString: '',
nodeVersion: 0,
};
/**
@@ -164,17 +165,26 @@ type IFunctionsContext = IExecuteFunctions | ISupplyDataFunctions | ILoadOptions
* @param context - The context.
* @returns the MongoClient for the node.
*/
export async function getMongoClient(context: any) {
export async function getMongoClient(context: any, version: number) {
const credentials = await context.getCredentials(MONGODB_CREDENTIALS);
const connectionString = credentials.connectionString as string;
if (!mongoConfig.client || mongoConfig.connectionString !== connectionString) {
if (
!mongoConfig.client ||
mongoConfig.connectionString !== connectionString ||
mongoConfig.nodeVersion !== version
) {
if (mongoConfig.client) {
await mongoConfig.client.close();
}
mongoConfig.connectionString = connectionString;
mongoConfig.nodeVersion = version;
mongoConfig.client = new MongoClient(connectionString, {
appName: 'devrel.integration.n8n_vector_integ',
driverInfo: {
name: 'n8n_vector',
version: version.toString(),
},
});
await mongoConfig.client.connect();
}
@@ -198,7 +208,7 @@ export async function getDatabase(context: IFunctionsContext, client: MongoClien
*/
export async function getCollections(this: ILoadOptionsFunctions) {
try {
const client = await getMongoClient(this);
const client = await getMongoClient(this, this.getNode().typeVersion);
const db = await getDatabase(this, client);
const collections = await db.listCollections().toArray();
const results = collections.map((collection) => ({
@@ -308,7 +318,7 @@ export class VectorStoreMongoDBAtlas extends createVectorStoreNode({
sharedFields,
async getVectorStoreClient(context, _filter, embeddings, itemIndex) {
try {
const client = await getMongoClient(context);
const client = await getMongoClient(context, context.getNode().typeVersion);
const db = await getDatabase(context, client);
const collectionName = getCollectionName(context, itemIndex);
const mongoVectorIndexName = getVectorIndexName(context, itemIndex);
@@ -358,7 +368,7 @@ export class VectorStoreMongoDBAtlas extends createVectorStoreNode({
},
async populateVectorStore(context, embeddings, documents, itemIndex) {
try {
const client = await getMongoClient(context);
const client = await getMongoClient(context, context.getNode().typeVersion);
const db = await getDatabase(context, client);
const collectionName = getCollectionName(context, itemIndex);
const mongoVectorIndexName = getVectorIndexName(context, itemIndex);