Skip to content

Commit 3ccebdf

Browse files
committed
book insert to DB is done
1 parent 7534680 commit 3ccebdf

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/dbRelated/booksDbOps.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ const findBooksFromDB = async () => {
2626

2727
const addBookToDB = async (bookToAdd) => {
2828
// create the connection
29-
const client = await createConnectionToDB();
29+
const client = createConnectionToDB();
3030
try {
3131
// select the DB and collection
3232
const db = selectDB(client, DB_NAME);
33-
const booksFound = await db.collection(COLLECTION_BOOKS).find({}).toArray();
34-
const res = booksFound.length > 0 ? booksFound : [];
35-
return res;
33+
const bookInserted = await db
34+
.collection(COLLECTION_BOOKS)
35+
.insertOne(bookToAdd);
36+
return { _id: bookInserted.insertedId, ...bookToAdd };
3637
} catch (error) {
3738
return error.toString();
3839
} finally {
39-
closeConnectionToDB(client);
40+
await closeConnectionToDB(client);
4041
}
4142
};
4243

src/typedefs/bookTypedef.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const booksTypeDefs = gql`
1414
1515
# All the Mutations on Book listed here
1616
type Mutation {
17-
addBook(name: String!): ID
17+
addBook(name: String!): Book!
1818
}
1919
`;
2020

0 commit comments

Comments
 (0)