Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the session optional for database with replica set not configured. #1836

Merged
merged 10 commits into from
Feb 23, 2024
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) {
return true;
} else {
return false;
}
} catch (error) {
logger.error("Error checking replica set configuration :", error);
return false;
}

Check warning on line 19 in src/utilities/checkReplicaSet.ts

View check run for this annotation

Codecov / codecov/patch

src/utilities/checkReplicaSet.ts#L17-L19

Added lines #L17 - L19 were not covered by tests
};
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[] }>;
}
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[] }> => ({
hosts: ["host1", "host2"],
}),
};
//@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);
});
});
Loading