Wednesday, 22 January 2025

Git commands


Git commands : 

1. Git configuration :

git config --global user.name "<Your-Full-Name>" // Sets your name for all Git repositories.

git config --global user.email "<your-email-address>" // Sets your email for all Git repositories.

2. Git repository management :

git init // Initializes a new Git repository in the current directory.

git clone <url> // Clones a repository from a remote URL.

3. File status and staging :

git status // Displays the status of the working directory and staging area.

git add . // Stages all changes (new files, modifications, and deletions) for commit.

git commit -m 'message' // Commits the staged changes with a descriptive message.

4. Pushing changes :

git push // Pushes local commits to the remote repository.

5. Branching and switching branches:

git branch <branch-name> // Creates a new branch locally.

git checkout <branch-name> // Switches to an existing branch.

git checkout -b <branch-name> // Creates and switches to a new branch.

git merge <branch-name> // Merges the specified branch into the current branch.

6. Fetching and pulling changes :

git pull // Fetches and merges changes from the remote repository to your local repository.

7. Commit history and differences :

git log // Displays the commit history of the repository.

git diff // Shows the differences between files or commits.

8. Resetting changes :

git reset // Resets changes to the last commit or a specific state (e.g., git reset HEAD <file> to unstage changes).

git reset --hard // Resets the working directory and staging area to the last commit (losing local changes).

9. git stash :

git stash // Saves all uncommitted changes (tracked files only). Resets your working directory to the last committed state.

Tuesday, 21 January 2025

Docker: Accelerated Container Application Development

 

Docker: Accelerated Container Application Development

1. Developers can deploy, run, and manage applications.

2. Deploy applications quickly.

3. Docker is a containerization platform that simplifies the process of deploying and managing applications. Applications run along with their dependencies. 

Docker Images :

1. A copy of our project.

2. Images are made from source code, libraries, and external dependencies.

3. Images are created from source code.

4. Example: Node setup, application code, installed libraries, Docker files.

5. Once an image is created, it cannot be updated; we can create a new image instead.

Docker Container :

1. A container runs applications using images.

2. Containers run independently.

3. Example: If 3 images are available, all these images can run independently.  

Docker Commands :

1. docker build -t folder/dockername . // Creates a project image.

2. docker run -p 5000:5000 -d dockerImageName // Runs a container.

3. docker ps // Shows all running containers.

4. docker logs container_id // Displays project logs.

5. docker image prune // Removes unused images.

6. docker service scale web-app=2 // Scales the web-app service to two replicas.

7. docker service update --image <new-image> service_name // Updates a Docker container's image without causing downtime. 

Docker File Example :

1. Create a file named Dockerfile in the root directory of your backend project.

2. In this Dockerfile, write the following code:

# Use Node.js version 20 as the base image
FROM node:20  

# Set the working directory inside the container
WORKDIR /node-practical  

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./  

# Install dependencies
RUN npm install  

# Copy the entire project to the container
COPY . .  

# Specify the command to run the application
CMD ["node", "index.js"]

3. Here’s an example of a docker-compose.yml file for a microservices architecture:

    docker-compose.yml Example : 

version: '3.9'
services:
  db:
    image: postgres:14-alpine
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: DBPass@123
      POSTGRES_DB: DBNAME
      POSTGRES_HOST_AUTH_METHOD: trust        
     
  gateway:
    build:
      dockerfile: Dockerfile
      context: ./gateway
    command: npm run start:prod
    restart: always  
    ports:
      - "5000:5000"
    depends_on:
      - db      
    environment:
      JWT_SECRET: secret@123
      JWT_EXPIRESIN: 2d
      REDIS_HOST: localhost
      REDIS_PORT: 6379
      REDIS_DB: TestFinAssetMgmt //if use redis
    volumes:          
      - /gateway/src/app/node_modules
 
  auth:
    build:
      dockerfile: Dockerfile
      context: ./auth
    command: npm run start:prod
    restart: always
    ports:
      - "5100:5100"
    depends_on:
      - db      
    environment:
      DB_TYPE : postgres
      DB_HOST : HOST_name
      DB_PORT : 5432
      DB_USERNAME : DBUSERNAME
      DB_PASSWORD : DB PASS
      DB_DATABASE : DBDATABASENAME
      DB_SYNC : true
      JWT_SECRET : secret@123
      JWT_EXPIRESIN : 2d
      RESET_PASSWORD_URL : http://3.15.153.226:3000
    volumes:          
      - /auth/src/app/node_modules

  mail:
    build:
      context: ./mail
    command: npm run start:prod
    restart: always
    ports:
      - "5200:5200"
    depends_on:
      - db
      - auth
      - gateway      
    environment:
      EMAIL_HOST : smtp.gmail.com
      EMAIL_USER : SMTP-email-pass
      EMAIL_PASSWORD : smtp-password
    volumes:          
      - /mail/src/app/node_modules



Gmail : SMTP : Simple Mail Transfer Protocol

Gmail : SMTP : Simple Mail Transfer Protocol

1. Go to google Gmail account setting 

    → Go to Forwarding and POP/IMAP 

    → Enabled IMAP access.

2. After go to : manage your google account 

    → Go to security page 

    → Less secure option is enable

3. Email smtp setting : 

    → Server name : smtp.gmail.com ,

    → Port : 587 for Gmail account,

    → Username and password google account

    → Sender name enter 

    → Save 

 

AWS SES : Simple Email Service

 


AWS SES : Simple Email Service

1. Download access key and secret key excel file.

2. Go to verified identity -> Create Identity -> Verify email received in email account and verified.

3. npm install aws-sdk

const AWS = require("aws-sdk");

const awsConfig = {
  accessKeyId: AWS_ACCESS_KEY_ID,
  secretAccessKey: AWS_SECRET_ACCESS_KEY,
  region: process. AWS_REGION,
};

const SES = new AWS.SES(awsConfig);

const sendEmail = async () => {

  try {
    //prepare email to send

    const params = {
      Source: "verifiedemailaddress@gmail.com"  //verified email address
      Destination: {
        ToAddresses: "to@gmail.com",
      },
      Message: {
        Subject: {
          Charset: "UTF-8",
          Data: "OTP Verification",
        },
        Body: {
          Html: {
            Charset: "UTF-8",
            Data: `<h1>Your verification code is 12345</h1>`,
          },
        },
      },
    };

    const emailSent = SES.sendEmail(params).promise();
    emailSent
      .then((data) => {
        console.log(data);
      })
      .catch((err) => {
        console.log(err);
      });
  } catch (error) {
    console.log(error);
  }
};

module.exports = sendEmail;

Difference between MySQL and PostgreSQL

 


Difference between MySQL and PostgreSQL.

1. MySQL:

    Relational database.

    Case-insensitive.

    Limited basic data type support.

    Uses indexes: R, B, Hash.

    Concurrency control.

2. PostgreSQL:

    Object-relational database (DB).

    Case-sensitive.

    Advanced data type support.

    Multiple types of indexes.

    Multiversion concurrency control (MVCC).

JavaScript Hoisting


JavaScript Hoisting :

1. Functions, variables, and class definitions are hoisted to the top.

2. let and const are initialized only after they are used.

3. The most important concepts in JavaScript.

4. To avoid bugs, always declare all variables at the beginning of every scope.

Monday, 20 January 2025

Pure and Impure Functions in JavaScript.

 

JavaScript Pure and Impure Functions.

1. JavaScript Pure Functions.

    JavaScript Pure Function Without modifying value return. like predictable out value.

function add(x) {
    return x + 1;
}
console.log(add(10)); // output : 11
console.log(add(11)); // output : 12
console.log(add(12)); // output : 13

2. JavaScript Impure Functions. 

    JavaScript Impure Function value is change every time.

let x = 10;
function add() {
    return x++;
}
console.log(add()); // output : 10
console.log(add()); // output : 11
console.log(add()); // output : 12