Skip to content

Commit 7534680

Browse files
committed
MongoDB connection persistance fixed
1 parent 824cd1a commit 7534680

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

.env.development

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ REGION="ap-south-1"
44
# database related
55
URI_TO_CONNECT_MONGODB="mongodb+srv://test:test1234@anijitsmongodb.l73ta.mongodb.net/?retryWrites=true&w=majority"
66

7-
DB_NAME=allapps
8-
COLLECTION_USER_STICKER=usersticker
9-
COLLECTION_BOOKS=books
10-
COLLECTION_AUTHORS=authors
7+
DB_NAME="allapps"
8+
COLLECTION_USER_STICKER="usersticker"
9+
COLLECTION_BOOKS="books"
10+
COLLECTION_AUTHORS="authors"

src/dbRelated/booksDbOps.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ import {
99
const { DB_NAME, COLLECTION_BOOKS } = process.env;
1010

1111
const findBooksFromDB = async () => {
12+
// create the connection
13+
const client = createConnectionToDB();
14+
try {
15+
// select the DB and collection
16+
const db = selectDB(client, DB_NAME);
17+
const booksFound = await db.collection(COLLECTION_BOOKS).find({}).toArray();
18+
const res = booksFound || [];
19+
return res;
20+
} catch (error) {
21+
return error.toString();
22+
} finally {
23+
await closeConnectionToDB(client);
24+
}
25+
};
26+
27+
const addBookToDB = async (bookToAdd) => {
1228
// create the connection
1329
const client = await createConnectionToDB();
1430
try {
@@ -24,4 +40,4 @@ const findBooksFromDB = async () => {
2440
}
2541
};
2642

27-
export { findBooksFromDB };
43+
export { findBooksFromDB, addBookToDB };

src/helpers/dbHelpers.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// local dependencies
2-
import { dbClient, ObjectId } from "../libs/mongoClient.js";
2+
import { MongoClient, ObjectId } from "../libs/mongoClient.js";
33

4-
const createConnectionToDB = async () => {
5-
const client = await dbClient.connect();
4+
const createConnectionToDB = () => {
5+
const client = new MongoClient(process.env.URI_TO_CONNECT_MONGODB);
66
return client;
77
};
88

9-
const closeConnectionToDB = (client) => {
10-
client.close();
9+
const closeConnectionToDB = async (client) => {
10+
await client.close();
1111
};
1212

1313
const selectDB = (client, DB_NAME) => {

src/libs/mongoClient.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import mongodb from "mongodb";
44
// initialize the client to connect MongoDB Atlas and export
55
const { MongoClient, ObjectId } = mongodb;
66

7-
const dbClient = new MongoClient(process.env.URI_TO_CONNECT_MONGODB);
8-
export { dbClient, ObjectId };
7+
// const dbClient = new MongoClient(process.env.URI_TO_CONNECT_MONGODB);
8+
export { MongoClient, ObjectId };

src/resolvers/bookResolver.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// local dependencies
2-
import { findBooksFromDB } from "../dbRelated/booksDbOps.js";
2+
import { findBooksFromDB, addBookToDB } from "../dbRelated/booksDbOps.js";
33

44
const resolvers = {
55
Query: {
@@ -9,9 +9,8 @@ const resolvers = {
99
},
1010

1111
Mutation: {
12-
getCount: (parent, arg) => {
13-
// console.log("arg is", arg);
14-
return arg.count + 1;
12+
addBook: (_, args) => {
13+
return addBookToDB(args);
1514
},
1615
},
1716
};

src/typedefs/bookTypedef.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ const booksTypeDefs = gql`
1111
type Query {
1212
findBooks: [Book]
1313
}
14+
15+
# All the Mutations on Book listed here
16+
type Mutation {
17+
addBook(name: String!): ID
18+
}
1419
`;
1520

1621
export { booksTypeDefs };

0 commit comments

Comments
 (0)