jamieeow / node-mongoose-reference

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Referencing in MongoDB with Mongoose and Node.js

A purely backend server that illustrates how to define mongoose Schema/Model with referencing to represent relationships between MongoDB collections. To be able to test the endpoints, make sure to have Postman installed.

This repository is an extension of the lecture on Referencing and More in Mongo(db/oose). The models here are based on the MDN Tutorial on mongoose.

Requirements

Local Setup

  1. Clone this repository: git clone https://github.com/unisse-courses/node-mongoose-reference.git
  2. Navigate to the directory: cd node-mongoose-reference
  3. Install the dependencies: npm install
    • I've already installed mongoose so it's in the package.json file already.
  4. Run the server: node index.js
    • It should simply be listening on port 9090. There's nothing to open in the browser for this sample.

What's Inside

Side Note: This is a mini reference to the musical Waitress (Spotify album of Sara Bareilles~)!

1. data folder

This folder contains 3 json files for the data to be inserted into the database.

2. models folder

This folder contains the mongoose.Schema definition of the collections to be used in this example (based on the MDN Tutorial on mongoose).

All the API endpoints definition are here.

  • /addAuthor — Adds an author into the database
  • /addGenre — Adds a genre into the database
  • /addBook — Adds a book into the database. Author name and genres should also be provided
  • /authors — Returns all authors
  • /genres — Returns all genres
  • /books — Returns all books with authors and genres populated
  • /books/:authorid — Returns all books given the authorid

How to Test the Endpoints

  1. Make sure that the server is running by executing node index.js in the console. alt text

  2. Open up the Postman application to get started.

POST endpoints

  1. On Postman, set up the request to a POST request on using the dropdown select beside the text box for the request URL. Type in the URL below and click on the "Headers" tab right below it to set the Content-Type header. (Don't click send yet, we still have to set the request body!)
URL: http://localhost:9090/addAuthor
Request Headers:
  Content-Type: application/json

alt text

Replace the URL with the enpoint you'd like to use. Or you can create multiple tabs for each /add endpoint.

  1. Open the corresponding json file under the data folder. (i.e. data/authors.json). The data is an array of Objects for each file but the endpoints only accept one Object at a time.

Copy one item from the json file and paste it in the Body tab of the POST request.

{
  "first_name": "George R. R.",
  "family_name": "Martin"
}

alt text

  1. Click Send and the result should show the inserted document.

alt text

  1. Repeat steps 2 & 3 for all the objects in data/authors.json and data/genres.json to populate the two collections.

  2. Start inserting books from data/books.json. Read the inline comments in index.js to understand the entire process of inserting a book with references to the author and genres.

GET "All" endpoints

  1. On Postman, set up the request to a GET request on using the dropdown select beside the text box for the request URL.
URL: http://localhost:9090/authors

Unlike the POST request earlier, there's no request body or parameters for this since we're retrieving all the values in the collection. Since there's no body, we don't need to set the request header too.

  1. Click Send and the result should show all the documents in the collection.

alt text

  1. Test out http://localhost:9090/genres and http://localhost:9090/books too!

GET /books/:authorid

  1. On Postman, set up the request to a GET request on using the dropdown select beside the text box for the request URL.
URL: http://localhost:9090/books/:authorid

The :authorid needs to be replaced with an actual _id value from the authors collection.

alt text

  1. Click Send and the result should show all the books by the selected author only.

alt text


Challenges

These are some tasks to help you practice creating your own server endpoints to handle client requests for data.

If you'd like to get feedback, fork this repository and commit the additions you make. Send the link to the forked repository to me whenever it's done.

  1. Create a GET /books/genre/:genreid that will retrieve all the books given the _id of the genre.
  2. Modify the POST /addBook to insert the author if not found.
  3. Create GET endpoints for the following:
    • /authors/:id — Returns the author given the id
    • /genre/:id — Returns the genre given the id

About


Languages

Language:JavaScript 100.0%