Skip to content

Commit

Permalink
Made the session optional for database with replica set not configure…
Browse files Browse the repository at this point in the history
…d. (#1836)

* Made the session optional for database with no replica set

* integrated hello command to check for replica set and removed previous changes

* Added script to check replica set with logging and wrote test for it

* Added additional check for setName property and updated the test

---------

Co-authored-by: Aditya Agarwal <adi790u@Adityas-MacBook-Air.local>
Co-authored-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 23, 2024
1 parent 9fa083e commit df864b4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mongoose from "mongoose";
import { MONGO_DB_URL } from "./constants";
import { logger } from "./libraries";
import { checkReplicaSet } from "./utilities/checkReplicaSet";

let session!: mongoose.ClientSession;

Expand All @@ -12,7 +13,14 @@ export const connect = async (): Promise<void> => {
useFindAndModify: false,
useNewUrlParser: true,
});
session = await mongoose.startSession();

const replicaSet = await checkReplicaSet();
if (replicaSet) {
logger.info("Session started --> Connected to a replica set!");
session = await mongoose.startSession();
} else {
logger.info("Session not started --> Not Connected to a replica set!");
}
} catch (error: unknown) {
if (error instanceof Error) {
const errorMessage = error.toString();
Expand Down
20 changes: 20 additions & 0 deletions src/utilities/checkReplicaSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import mongoose from "mongoose";
import { logger } from "../libraries";

export const checkReplicaSet = async (): Promise<boolean> => {
try {
const adminDb = mongoose.connection.db.admin();
const result = await adminDb.command({
hello: 1,
});

if ("hosts" in result && "setName" in result) {
return true;
} else {
return false;
}
} catch (error) {
logger.error("Error checking replica set configuration :", error);
return false;
}
};
48 changes: 48 additions & 0 deletions tests/utilities/checkReplicaSet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import mongoose from "mongoose";
import { checkReplicaSet } from "../../src/utilities/checkReplicaSet";
import { expect, describe, it, beforeAll, afterAll } from "vitest";
import { connect, disconnect } from "../helpers/db";

let MONGOOSE_INSTANCE: typeof mongoose;

beforeAll(async (): Promise<void> => {
MONGOOSE_INSTANCE = await connect();
});

afterAll(async (): Promise<void> => {
await disconnect(MONGOOSE_INSTANCE);
});

interface InterfaceAdminDbMock1 {
command: () => Promise<{ hosts: string[]; setName: string }>;
}
interface InterfaceAdminDbMock2 {
command: () => Promise<object>;
}

describe("checkReplicaSet", () => {
it("should return true if replica set is configured", async () => {
const adminDbMock: InterfaceAdminDbMock1 = {
command: async (): Promise<{ hosts: string[]; setName: string }> => ({
hosts: ["host1", "host2"],
setName: "xyz",
}),
};
//@ts-expect-error cant find the right type
mongoose.connection.db.admin = (): object => adminDbMock;
const result = await checkReplicaSet();

expect(result).toBe(true);
});

it("should return false if replica set is not configured", async () => {
const adminDbMock: InterfaceAdminDbMock2 = {
command: async (): Promise<object> => ({}),
};
//@ts-expect-error cant find the right type
mongoose.connection.db.admin = (): object => adminDbMock;
const result = await checkReplicaSet();

expect(result).toBe(false);
});
});

0 comments on commit df864b4

Please sign in to comment.