A sample project in Express, which shows how to transfer global variables like db connection, even when you are running some script which might require to use those db variables.
It also shows a sample reward system
Server Node.js โก๏ธ DB Mongo ๐
Design Philosphy
- The system is comprised of 4 tables, namely the User, Product, Order and Rewards
- User table is because which user gave the rating
- For which Product,
- Against what Order
- What he rated.
- We first save the rating for a particular product against and order and user in our reward table.
- Next we try to fetch the product and we can see it gives a ratings and rating object. Ratings is an obj which tells like 4 people 1 star, 5 people 2 star and the rating gives aggregated value.
To get started
yarn
node one-time-scripts/create-init-data.js
Take a productId from the rewards array
call this API http://localhost:8082/api/Product?productId=<your_product_id>
this should return you the product data with the ratings obj and rating
Watch out for
- How I have used
require.main === module
a very interesting thing, very few people use - everything using ENV variables
- dynamically creating models using filename
- using enums to enforce data in mongoose