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.
- NodeJS & npm
- MongoDB Community Edition (Must be running in the background)
- Postman - To be able to test the endpoints
- Any text editor for JavaScript, HTML & CSS (VSCode, Atom, SublimeText, etc.)
- Clone this repository:
git clone https://github.com/unisse-courses/node-mongoose-reference.git
- Navigate to the directory:
cd node-mongoose-reference
- Install the dependencies:
npm install
- I've already installed mongoose so it's in the
package.json
file already.
- I've already installed mongoose so it's in the
- 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.
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).
3. index.js
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
-
Make sure that the server is running by executing
node index.js
in the console. -
Open up the Postman application to get started.
- 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
Replace the URL with the enpoint you'd like to use. Or you can create multiple tabs for each /add
endpoint.
- Open the corresponding
json
file under thedata
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"
}
- Click Send and the result should show the inserted document.
-
Repeat steps 2 & 3 for all the objects in
data/authors.json
anddata/genres.json
to populate the two collections. -
Start inserting books from
data/books.json
. Read the inline comments inindex.js
to understand the entire process of inserting a book with references to the author and genres.
- 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.
- Click Send and the result should show all the documents in the collection.
- Test out
http://localhost:9090/genres
andhttp://localhost:9090/books
too!
- 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.
- Click Send and the result should show all the books by the selected author only.
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.
- Create a GET
/books/genre/:genreid
that will retrieve all the books given the_id
of the genre. - Modify the POST
/addBook
to insert the author if not found. - Create GET endpoints for the following:
/authors/:id
— Returns the author given the id/genre/:id
— Returns the genre given the id