Skip to content

jatin389/STAGE-ExpressApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


My List Feature

Live service Details

HOST: https://stage-expressapp.onrender.com

API endpoints

{
    "userId": "51cface4-5a2c-4eaf-a4df-4c4b736bcf9d",
    "itemId": "51cface4-5a2c-4eaf-a4df-4c4b736bcf3d",
    "itemType": "Movie"
}
{
  "userId": "51cface4-5a2c-4eaf-a4df-4c4b736bcf9d",
    "itemId": "51cface4-5a2c-4eaf-a4df-4c4b736bcf4d"
}

Setup Instructions

Action Command
💻 Install dependencies npm install
⚙️ Setup environment variables. Create a .env file MONGODB_URI=mongodb://localhost:27017/mylist
PORT=3000
⚡ Run Script to initialize DB npx ts-node initializeDBScript.ts
🚀 Run the application npm run dev-start

Run Test Cases

npm test


API Desgin

Approach 1

HTTP Method API Endpoint Request Body Query Parameters
❇️ GET /api/mylist - page=1&limit=10
✏️ POST /api/mylist userId:"UUID",itemId:"UUID", itemType:"Movie/ TvSHow" -
❌ DELETE /api/mylist userId:"UUID",itemId:"UUID" -

Approach 2

HTTP Method API Endpoint Request Body Query Parameters
❇️ GET /api/user/:user_id/mylist - page=1&limit=10
✏️ POST /api/user/:user_id/mylist itemId, itemType -
❌ DELETE /api/user/:user_id/mylist/:item_id - -

DB Models

Approach 1

Put myList field inside User Schema

User: {
    id: string,
    username: string,
    ...
    ...
    myList: {
        itemId: string,
        itemType: string,
        dateAddedOn: Date,
    }
}

PROS:

  • ✔️ Faster: No need to make extra call to fetch myList data.
  • ✔️ Mylist information can be fetched initially from /api/me and save in local cache
  • ✔️ Scalable: Any additional metadata can be added

🚨 CONS:

  • ❗Tight Coupling: Dependency on User Schema
  • ❗Not preferable for microservice arcitechure
  • ❗UserService and MyList Service can not scale independently

Approach 2

Consider myList as a separate collection

== 2.1 ==

User: {
    id: string,
    username: string,
    ...
    ...
}
    
myList: {
    userId: string,
    itemId: string,
    itemType: string,
    dateAddedOn: Date,
}

PROS:

  • ✔️ ListItems can be handled separately for all users
  • ✔️ Addition and removal of new record is faster
  • ✔️ Mongo Can filter or sort on the dateAddedOn field easily

🚨 CONS:

  • ❗ Need to touch multiple documents to fetch myList of single user.
  • ❗ Slower GET request

== 2.2 ==

User: {
    id: string,
    username: string,
    ...
    ...
}

myList: {
    userId: string,
    wishList: [{
        itemId: string,
        itemType: string,
        dateAddedOn: Date,
    }]
}

PROS:

  • ✔️ List of items for a single user can be fetched from a single document
  • ✔️ Faster GET request
  • ✔️ FutureProof / Scalable: Can add user's metadata to this schema

🚨 CONS:

  • ❗ Nested Array of objects difficult to handle(filter / sort) in Mongo.
  • ❗ Removal of item from list is slower

Design Decision

Assumption: We have monolithic service

  • Choosing first approach considering
    • Easier to implement
    • Low Maintenance
    • High performance (GET/POST)

Sequence Diagram

sequenceDiagram
    participant Client
    participant BE
    participant Mongo

    Note over Client,Mongo: Add to my list
    autonumber 1

    Client ->> BE: POST /api/myList/ {userId, ItemId, ...}
    BE -->> BE: Validates request
    BE -->> Mongo: Fetch User by userId
    BE -->> BE: add item to list if ItemId not in list
    BE ->> Client: Success (201)

    Note over Client,Mongo: Remove Item from list
    autonumber 1

    Client ->> BE: DELETE /api/myList/ {userId, ItemId}
    BE -->> BE: Validates request
    BE -->> Mongo: Fetch User by userId
    BE -->> BE: remove item from the list if ItemId is present in list
    BE ->> Client: Success (201)

    Note over Client,Mongo: List My Items
    autonumber 1

    Client ->> BE: GET /api/myList?userid:123
    BE -->> Mongo: Fetch User by userId
    BE ->> Client: Success (201), return MyList
Loading

CI/CD

  • For CI/CD: Render Pipeline
  • Deployed On: Render Cloud
graph LR
A[Developer] -- 1. Push Commit to Origin --> B((Github))
B -- 2. Trigger Pipeline--> C(Render)
C -- 3. Pull latest code --> B

C -- Build and deploy --> D{Render Cloud}
Loading

HighLights

  • MongoDB Integration
  • Script to initialize database
  • Write Unit test cases
  • Validate request body: Using Joi
  • Generate random values: using Faker
  • CI/CD
  • Deploy on any cloud service

What can be improved?

  1. Check for test coverage results in CI/CD pipeline
  2. Setup Pre-Commit hooks to ensure
    1. Consistent commit messages
    2. Language standards: linting
  3. Map Foreign Key in MyList
  4. Make file to setup repository with single command

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors