File tree Expand file tree Collapse file tree 6 files changed +36
-16
lines changed Expand file tree Collapse file tree 6 files changed +36
-16
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ REGION="ap-south-1"
4
4
# database related
5
5
URI_TO_CONNECT_MONGODB = " mongodb+srv://test:test1234@anijitsmongodb.l73ta.mongodb.net/?retryWrites=true&w=majority"
6
6
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"
Original file line number Diff line number Diff line change @@ -9,6 +9,22 @@ import {
9
9
const { DB_NAME , COLLECTION_BOOKS } = process . env ;
10
10
11
11
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 ) => {
12
28
// create the connection
13
29
const client = await createConnectionToDB ( ) ;
14
30
try {
@@ -24,4 +40,4 @@ const findBooksFromDB = async () => {
24
40
}
25
41
} ;
26
42
27
- export { findBooksFromDB } ;
43
+ export { findBooksFromDB , addBookToDB } ;
Original file line number Diff line number Diff line change 1
1
// local dependencies
2
- import { dbClient , ObjectId } from "../libs/mongoClient.js" ;
2
+ import { MongoClient , ObjectId } from "../libs/mongoClient.js" ;
3
3
4
- const createConnectionToDB = async ( ) => {
5
- const client = await dbClient . connect ( ) ;
4
+ const createConnectionToDB = ( ) => {
5
+ const client = new MongoClient ( process . env . URI_TO_CONNECT_MONGODB ) ;
6
6
return client ;
7
7
} ;
8
8
9
- const closeConnectionToDB = ( client ) => {
10
- client . close ( ) ;
9
+ const closeConnectionToDB = async ( client ) => {
10
+ await client . close ( ) ;
11
11
} ;
12
12
13
13
const selectDB = ( client , DB_NAME ) => {
Original file line number Diff line number Diff line change @@ -4,5 +4,5 @@ import mongodb from "mongodb";
4
4
// initialize the client to connect MongoDB Atlas and export
5
5
const { MongoClient, ObjectId } = mongodb ;
6
6
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 } ;
Original file line number Diff line number Diff line change 1
1
// local dependencies
2
- import { findBooksFromDB } from "../dbRelated/booksDbOps.js" ;
2
+ import { findBooksFromDB , addBookToDB } from "../dbRelated/booksDbOps.js" ;
3
3
4
4
const resolvers = {
5
5
Query : {
@@ -9,9 +9,8 @@ const resolvers = {
9
9
} ,
10
10
11
11
Mutation : {
12
- getCount : ( parent , arg ) => {
13
- // console.log("arg is", arg);
14
- return arg . count + 1 ;
12
+ addBook : ( _ , args ) => {
13
+ return addBookToDB ( args ) ;
15
14
} ,
16
15
} ,
17
16
} ;
Original file line number Diff line number Diff line change @@ -11,6 +11,11 @@ const booksTypeDefs = gql`
11
11
type Query {
12
12
findBooks: [Book]
13
13
}
14
+
15
+ # All the Mutations on Book listed here
16
+ type Mutation {
17
+ addBook(name: String!): ID
18
+ }
14
19
` ;
15
20
16
21
export { booksTypeDefs } ;
You can’t perform that action at this time.
0 commit comments