From 6e1213bd132415ba9913d1839f76ef2224fceb84 Mon Sep 17 00:00:00 2001 From: Bradley Lewis <22850972+BradLewis@users.noreply.github.com> Date: Thu, 22 Aug 2024 03:41:52 -0400 Subject: [PATCH] Update MongoDB docs about directConnection --- .../mongodb/src/mongodb-container.test.ts | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/packages/modules/mongodb/src/mongodb-container.test.ts b/packages/modules/mongodb/src/mongodb-container.test.ts index 8025be079..10f64cd95 100644 --- a/packages/modules/mongodb/src/mongodb-container.test.ts +++ b/packages/modules/mongodb/src/mongodb-container.test.ts @@ -1,4 +1,4 @@ -import { MongoDBContainer, StartedMongoDBContainer } from "./mongodb-container"; +import { MongoDBContainer } from "./mongodb-container"; import mongoose from "mongoose"; describe("MongodbContainer", () => { @@ -8,7 +8,26 @@ describe("MongodbContainer", () => { it("should work using default version 4.0.1", async () => { const mongodbContainer = await new MongoDBContainer().start(); - await checkMongo(mongodbContainer); + // directConnection: true is required as the testcontainer is created as a MongoDB Replica Set. + const db = mongoose.createConnection(mongodbContainer.getConnectionString(), { directConnection: true }); + + // You can also add the default connection flag as a query parameter + // const connectionString = `${mongodbContainer.getConnectionString()}?directConnection=true`; + // const db = mongoose.createConnection(connectionString); + + const fooCollection = db.collection("foo"); + const obj = { value: 1 }; + + const session = await db.startSession(); + await session.withTransaction(async () => { + await fooCollection.insertOne(obj); + }); + + expect( + await fooCollection.findOne({ + value: 1, + }), + ).toEqual(obj); await mongoose.disconnect(); await mongodbContainer.stop(); @@ -19,15 +38,13 @@ describe("MongodbContainer", () => { it("should work using version 6.0.1", async () => { const mongodbContainer = await new MongoDBContainer("mongo:6.0.1").start(); - await checkMongo(mongodbContainer); + // directConnection: true is required as the testcontainer is created as a MongoDB Replica Set. + const db = mongoose.createConnection(mongodbContainer.getConnectionString(), { directConnection: true }); - await mongoose.disconnect(); - await mongodbContainer.stop(); - }); - // } + // You can also add the default connection flag as a query parameter + // const connectionString = `${mongodbContainer.getConnectionString()}?directConnection=true`; + // const db = mongoose.createConnection(connectionString); - async function checkMongo(mongodbContainer: StartedMongoDBContainer) { - const db = mongoose.createConnection(mongodbContainer.getConnectionString(), { directConnection: true }); const fooCollection = db.collection("foo"); const obj = { value: 1 }; @@ -39,7 +56,11 @@ describe("MongodbContainer", () => { expect( await fooCollection.findOne({ value: 1, - }) + }), ).toEqual(obj); - } + + await mongoose.disconnect(); + await mongodbContainer.stop(); + }); + // } });