Skip to content

Commit fced92b

Browse files
authored
Merge pull request #534 from oscar-ekeyekwu/v2
chore: encapsulate DB connection function for use in controller methods
2 parents 549578e + 8017ae0 commit fced92b

8 files changed

Lines changed: 250 additions & 0 deletions

File tree

File renamed without changes.

package-lock.json

Lines changed: 159 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"express": "^4.17.1",
4040
"express-jwt": "^5.3.3",
4141
"jsonwebtoken": "^8.5.1",
42+
"mongoose": "^5.9.27",
4243
"swagger-jsdoc": "^4.0.0",
4344
"swagger-ui-express": "^4.1.4"
4445
},

src/db/__test__/config/setup.js

Whitespace-only changes.

src/db/__test__/db.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const mongoose = require("mongoose");
2+
const IndexModel = require("../../models/index");
3+
const { connect, disconnect } = require("../database");
4+
require("dotenv").config();
5+
6+
describe("INSERT", () => {
7+
test("Should confirm mongodb works by adding a record", async () => {
8+
console.log("Attempting to connect to database...");
9+
await connect(
10+
"mongodb+srv://microapi:secret123@cluster0.japiw.mongodb.net/microapi?retryWrites=true&w=majority"
11+
);
12+
const id = mongoose.Types.ObjectId();
13+
const mockData = new IndexModel({
14+
_id: id,
15+
name: "Oscar",
16+
});
17+
await mockData.save();
18+
const insertedData = await IndexModel.findById(id);
19+
expect(insertedData.id).toEqual(mockData.id);
20+
expect(insertedData.name).toEqual(mockData.name);
21+
22+
await disconnect();
23+
});
24+
25+
test("Should catch connection string error", async () => {
26+
console.log("Attempting to connect to database...");
27+
expect(await connect("")).toEqual(Error("Invalid connection string"));
28+
});
29+
30+
test("Should return error for existing connections", async () => {
31+
console.log("Attempting to connect to database...");
32+
mongoose.connection.readyState = 1;
33+
expect(
34+
await connect(
35+
"mongodb+srv://microapi:secret123@cluster0.japiw.mongodb.net/microapi?retryWrites=true&w=majority"
36+
)
37+
).toEqual(Error("Connection already Established"));
38+
});
39+
40+
test("Should return error for non-existing connections on disconnec", async () => {
41+
console.log("Attempting to connect to database...");
42+
await connect(
43+
"mongodb+srv://microapi:secret123@cluster0.japiw.mongodb.net/microapi?retryWrites=true&w=majority"
44+
);
45+
mongoose.connection.readyState = 0;
46+
expect(await disconnect()).toEqual(Error("No open connection(s)"));
47+
});
48+
});

src/db/database.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const mongoose = require("mongoose");
2+
3+
const connect = async (connectionUri) => {
4+
if (mongoose.connection.readyState === 0) {
5+
try {
6+
await mongoose.connect(connectionUri, {
7+
useNewUrlParser: true,
8+
useCreateIndex: true,
9+
useFindAndModify: false,
10+
useUnifiedTopology: true,
11+
});
12+
} catch (err) {
13+
return new Error(err.message);
14+
}
15+
} else {
16+
return new Error("Connection already Established");
17+
}
18+
};
19+
20+
const disconnect = async () => {
21+
if (mongoose.connection.readyState !== 0) {
22+
await mongoose.disconnect();
23+
} else {
24+
return new Error("No open connection(s)");
25+
}
26+
};
27+
28+
module.exports = {
29+
connect,
30+
disconnect,
31+
};

src/models/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const mongoose = require("mongoose");
2+
const Schema = mongoose.Schema;
3+
4+
const IndexSchema = new Schema({
5+
name: {
6+
type: String,
7+
default: "A name",
8+
},
9+
});
10+
11+
module.exports = mongoose.model("Index", IndexSchema);

src/utils/db.js

Whitespace-only changes.

0 commit comments

Comments
 (0)