Last week we have been able to build the foundation of RESTful API with Express. Our server could handle CRUD eventhough it is not really efficent. Also, it is only alive when our computer is still running, as it operate on our local machine. This week, we will learn how to build a better structured RESTful API that deploy for public access..

What You'll Learn

Before diving into the database, let's review our routing logic with Express. In general, a flow of buidling an Express RESTful api will looks like this.

1. Setup project structure

Either it is front-end or back-end, this will be the most important part of the project. By deciding on where to keep files and how to connect them within the code base, you are winning half the battle. Your project folder will look like:

|- controllers/
    |- a.controller.js
    |- b.controller.js
|- models/
    |- A.js
    |- B.js
|- public/
|- routes/
    |- index.js
    |- a.api.js
    |- b.api.js
|- .env
|- .gitignore
|- app.js
|- package.json
|- README.md

2. Design the endpoints

In this step, we are designing REST APIs for our application. The main question is how to apply REST principles in design process?

The very first step is identifying the objects which will be presented as resources, which are:

Next, it's time to decide the resource URIs which are endpoints of our RESTful services. Think about the relationship between resources and its sub-resources (e.g. Product vs Category, User vs Order in an ecommerce , shopping application).

/*
 * @route GET api/products?page=1&limit=10 - Get all products
 * @route GET api/products/category/:id?page=1&limit=10 - Get all products with specific category
 * @route POST api/auth/register - Create a new account
 * @route PUT api/users/me - Update user profile
 * @route DELETE api/reviews/:id - Remove a review
 */

Notice: URIs should be nouns only, don't use any verb or operation like:

-@route POST api/products/create_blog - Create a new product

Assign HTTP Methods:

A user can perform browse, create, update, or delete operations. Typically we assign:

3. Authorization:

If there are different roles of users in your system, you should pre-define who can see/do what. Example: we allow everyone to see the list of products so the endpoint will look like:

/**
 * @route GET api/products?page=1&limit=10
 * @description Get products with pagination
 * @access Public
 */

But if user want to write a review, they need to login, so the endpoint will be defined:

/**
 * @route POST api/review
 * @description Create a new review for a product
 * @access Login required
 */

Web Application Flowchart

This is how every part of web programming connect (Web)

flow

Definition of Database

"In computing, a database is an organized collection of data stored and accessed electronically from a computer system. Where databases are more complex they are often developed using formal design and modeling techniques.

Database management system (DBMS)

The database management system (DBMS) is the software that interacts with end users, applications, and the database itself to capture and analyze the data. The DBMS software additionally encompasses the core facilities provided to administer the database. The sum total of the database, the DBMS and the associated applications can be referred to as a "database system". Often the term "database" is also used to loosely refer to any of the DBMS, the atabase system or an application associated with the database.

Computer scientists may classify database-management systems according to the database models that they support. Relational databases became dominant in the 1980s. These model data as rows and columns in a series of tables, and the vast majority use SQL for writing and querying data. In the 2000s, non-relational databases became popular, referred to as NoSQL because they use different query languages." _wikipedia

Both a database and its DBMS conform to the principles of a particular database model. "Database system" refers collectively to the database model, database management system, and database.

SQL vs NoSQL

We choose NoSQL. The reason : "Beginner friendly yet as effective as the other". Eventually, it is recommended that , as a developer, we should have basic knowledge on both systems. So for those who have FOMO (Fear-Of-Missing-Out), feel free to study both.

Comparision of NoSQL and SQL

System

Pros

Cons

SQL

Flexible queries, Reduced data storage footprint, Strong and well-understood data integrity semantics

Rigid data models, Limited horizontal, Single point of failure scalability

NoSQL

Scalable and highly available, Flexible data models, Dynamic schema for unstructured data, High performance, High-level data abstractions

Distributed systems have distributed systems problems, Lack of flexibility in access patterns

Read more

In bigger application, you may find that both system could be in use for the database.

mongo banner

MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need. As a DataBaseManagementSystem software, MongoDB provide various functions that allow management of a database and its data which can be classified into four main functional groups:

Installing MongoDB for Mac

Although Mongo DB official document try to be helpful, but their instruction for Mac user is out-of-dated. You will stumble upon error from 10.0 Catalina , Big Sur , ... to Monterey, as at November 2021.

TLDR : Quick start commands

Input these line in the following order to

Install

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

brew update
brew upgrade

brew tap mongodb/brew
brew install mongodb-community@4.4

echo 'export PATH="/usr/local/opt/mongodb-community@4.4/bin:$PATH"' >> ~/.zshrc

sudo mkdir -p /System/Volumes/Data/data/db



alias -g mongod="sudo mongod --dbpath /System/Volumes/Data/data/db"

source ~/.zshrc

To run

mongod

Explanation

Since the new updates, Apple do not allow service like mongod and brew to access to home folder. MongoDB however, need a /Data/data/db to contain all data. So the step were

  1. Install Home brew : package manager that help ease the installation for Mac and Linux
  2. Search for new update then upgrade home brew
  3. Connect to the mongodb/brew tap on github
  4. Install the community edition of mongod via brew . Current version is (mongo-community@4.4)
  5. Create a congfig ~/.zshrc (or ~/.bashrc but i would recommend install Oh my zsh to use zsh), then export the Path to mongo executed files to Machine's root so that we could use the mongo services with CLI everywhere.
  6. Super User access to create Data/data/db at System/Volumes
  7. Super User to Change Ownership to the foler we just created. Why? Because without the ownership we can not Write to the folder
  8. To run mongodb we must specify which folder to use as the db folder . Which is the one we just created.Hence mongod --dbpath /System/Volumes/Data/data/db.
  9. But mongod --dbpath /System/Volumes/Data/data/db is long and we kinda have to type it everytime we want to run mongodb. so we use Super User access to create an shorter version , hence alias sudo mongod --dbpath /System/Volumes/Data/data/db
  10. Save and execute sourcing zsh config.

Installing Mongo DB for Windows

Lucky for us, the installation for Windows on this Mongo DB official document still valid as at October 2021

assets/ftw/week7/monday/create_org.png

assets/ftw/week7/monday/org_name.png

assets/ftw/week7/monday/org_members.png

assets/ftw/week7/monday/create_proj.png

assets/ftw/week7/monday/proj_name.png

assets/ftw/week7/monday/proj_members.png

assets/ftw/week7/monday/create_cluster.png

assets/ftw/week7/monday/free_tier.png

assets/ftw/week7/monday/name_cluster.png

Load the Sample Dataset (for practicing)

Select the "..." option in the cluster menu -> choose the "Load Sample Dataset" option, then confirm your choice.

assets/ftw/week7/monday/load_sample_1.png assets/ftw/week7/monday/load_sample_2.png

When the dataset is loaded the graph labeled "Logical Size" on the right side of the screen should go up and display the size of the dataset that is above zero and below 512 MB. Your graph may look different than the picture below.

assets/ftw/week7/monday/sample_done.png {"mode":"full","isActive":false}

Further resources