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..
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.
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
routes/
stores .api
files that determine routes end point which is a URI and a specific HTTP request method (GET, POST, and so on). Each route have a handler function which is defined in .controller
file.controller/
stores all function that interact with your database.models/
stores the schemas that map with the collections in your MongoDB.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
A user can perform browse, create, update, or delete operations. Typically we assign:
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
*/
This is how every part of web programming connect (Web)
"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.
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.
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.
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 |
In bigger application, you may find that both system could be in use for the database.
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:
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
/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
mongod
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
update
then upgrade
home brewmongodb/brew
tap on githubMachine's root
so that we could use the mongo services
with CLI everywhere.Data/data/db
at System/Volumes
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
.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
Lucky for us, the installation for Windows on this Mongo DB official document still valid as at October 2021
MDBU
. Make sure that your cloud service is Atlas, then hit Next.M001
and hit NextSandbox
. Create the cluster. This step might take a minute or two to complete.Load the Sample Dataset (for practicing)
Select the "..." option in the cluster menu -> choose the "Load Sample Dataset" option, then confirm your choice.
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.
{"mode":"full","isActive":false}