For this project we were tasked to set up a database to store and retrieve data from and use that data to produce a RESTful API.
The main requirements were to:
- Create an API that has at least two endpoints that allows for get request from data stored on the database: One endpoint that returns an array of elements and the second that returns a single element.
- Follow and implement RESTful methadology when creating the API endpoints.
- Set up the MongoDB noSQL database.
- Use Mongoose library to connect to the server and allow for us to interact between the server and clientside.
- And use Mongoose to create models and attributes that act as instances/blueprint of the dataset that we're using. This will allow for us to store/save the data in the database.
- As we'd already created an API with a number of different endpoints last week the main thing to learn/understand was how to create the database, how to interact with it using Mongoose and how to adapt our API endpoints using mongoose queries and functions which allows us to access the database.
- Set up my MongoDB database and then installed and imported Mongoose.
- Implemented the relevant code which allows for the server.js to connect to the MongoDB database.
- Decided to continue using the same dataset that I used last week, books.json.
- Created a mongoose model which represents the structure of the objects in the dataset using and which will be the structure for the instances that will be entered into the database.
- Then seeded the database in order for the dataset using to be saved to the database. As the dataset is an array of objects used forEach() to iterate over each array element.
- Also implemented deleteMany() function which is called in the beginning of the seeding process. This will delete all data that was previously saved to the database so duplication of data doesn't occur. After this then the data is saved to the database.
- Used async and await to help with asynchronous operations when seeding the database and when the different endpoints are getting data from the server as they aren't returned instantly.
- Created the first endpoint which returns the whole array of data using the model name and the method find(). I also created some queries parameters, like in last weeks project, author, title, language and average rating. For all the query parameters that require a string I used the RegExp() object that matches the pattern of the string entered in the query against the data in the database for that specific attribute/key. I also added the "i" modifier which performs case sensitive matching.
- The second endpoint allows for the user to search for book by id. I used findOne() function which allows for that specific id to be queried against the data in the database and returned if a match is found.
- I also implemented response status and error messages if the parameters of the different endpoints don't match.
- JavaScript
- Express.js
- MongoDB
- Mongoose library
- Stackoverflow
- Team mates knowlege
- Help channel in Slack
https://clairebookapi.herokuapp.com/documentation This url will take you to the documentation. After that it's just a case of choosing which endpoint you want to use!