-
-
Notifications
You must be signed in to change notification settings - Fork 884
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made the session optional for database with replica set not configure…
…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
1 parent
9fa083e
commit df864b4
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |