FDT MERN- 2 and 3 Sprint Summary

Modified on Tue, 20 Feb at 4:13 PM

TABLE OF CONTENTS


Node.js


Node.js is an open-source JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser.

- Node.js is primarily used for building server-side applications, networking programs, and real-time applications such as chat platforms and collaborative tools.

Steps for using Node.js:


1. Install Node.js from the official website or package manager.
2. Set up a Node.js project by creating a project directory and initializing npm (Node Package Manager).
3. Write JavaScript code using the Node.js runtime environment, including using its built-in modules and libraries.
4. Execute the Node.js code using the command line or by setting up a server that can handle incoming requests from clients.
  5. Monitor and manage deployed Node.js applications using various tools and frameworks.



 Code snippet example:

javascript
// Create a simple HTTP server using Node.js
const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!');
});

server.listen(3000, 'localhost', () => {
  console.log('Server is running on http://localhost:3000/');
});



- Takeaways / best practices:
- Understand the event-driven, non-blocking nature of Node.js and utilize it effectively for scalable and efficient applications.
- Leverage the vast ecosystem of npm packages to enhance productivity and add necessary functionality to your projects.
- Use asynchronous programming techniques, such as callbacks, promises, or async/await, to handle I/O operations smoothly.
- Pay attention to error handling and implement proper logging to ensure robustness.
  - Utilize frameworks like Express.js or Nest.js to simplify the development process and enhance the modularity of your code.



HTTP


- What is it? 
HTTP (Hypertext Transfer Protocol) is a communications protocol that allows clients and servers to exchange resources on the World Wide Web.

- Where is it used?
HTTP is used to transmit web pages, files, images, videos, and other resources on the internet.

- How is it used?
1. Client sends an HTTP request to the server.
2. Server receives the request and processes it.
3. Server sends back an HTTP response to the client.
4. Client receives the response and displays the content.

- Takeaways / best practices:

1. Use appropriate HTTP methods such as GET, POST, PUT, DELETE, etc. based on the intended action of the request.
2. Include proper headers (e.g., Content-Type, Accept) to communicate the type of data being sent or requested.
3. Follow RESTful principles for structuring URLs and designing APIs.
4. Utilize proper status codes (e.g., 200, 404, 500) to indicate the outcome of the request.
5. Ensure secure communication by using HTTPS instead of plain HTTP when transmitting sensitive data.


HTTP Methods

- What is it? 
HTTP methods are a set of request methods used by servers and clients to communicate within the Hypertext Transfer Protocol (HTTP). They define the type of action that should be performed on a given resource.

- Where is it used?
HTTP methods are commonly used in web development and API design to specify the desired action to be taken by the server when interacting with resources.

- How is it used?
1. GET: Retrieves a resource from a server.
- A client sends a GET request to the server specifying the resource URL.
- The server responds with the requested resource in the response body.

2. POST: Sends data to the server to create a resource.
- A client sends a POST request to the server specifying the resource URL and the data to be sent.
- The server processes the data and creates a new resource.

3. PUT: Sends data to the server to update a resource or create a new one.
- A client sends a PUT request to the server specifying the resource URL and the data to be sent.
- The server updates the existing resource or creates a new resource with the sent data.

4. DELETE: Requests the server to delete a resource.
- A client sends a DELETE request to the server specifying the resource URL.
- The server deletes the specified resource.

5. PATCH: Sends partial data to the server to update a resource.
- A client sends a PATCH request to the server specifying the resource URL and the partial data to be sent.
- The server updates the specified resource with the provided partial data.

6. HEAD: Retrieves only the headers of a resource, without the response body.
- A client sends a HEAD request to the server specifying the resource URL.
- The server responds with only the headers of the resource, allowing the client to gather information about it.

7. OPTIONS: Retrieves the supported methods and other capabilities of a resource.
- A client sends an OPTIONS request to the server specifying the resource URL.
- The server responds with the allowed methods, headers, and other capabilities of the resource.

- Takeaways/Best practices:
- Use the appropriate HTTP method that aligns with the desired action on a resource.
- Use GET requests for retrieving data, POST requests for creating new resources, PUT/PATCH requests for updating resources, and DELETE requests for deletion.
- Familiarize yourself with the HTTP method definitions and their intended usage to ensure proper implementation.
  - Respect the idempotence and safety properties of HTTP methods.


HTTP Status Code


- HTTP Status Code is a three-digit number that is returned by a server in response to a client's request to provide information about the status of the request.
- It is primarily used in the context of the Hypertext Transfer Protocol (HTTP) to communicate the success or failure of a request made by a client to a server.
- It is used in the following steps:
1. The client sends a request to the server, which may include actions like retrieving a webpage, submitting a form, or interacting with an API.
2. The server processes the request and prepares a response.
3. The server includes an appropriate HTTP Status Code in the response to indicate the result of the request.
4. The client receives the response and interprets the HTTP Status Code to understand the outcome of the request and proceed accordingly.

Takeaway/Best Practices:
- Familiarize yourself with the commonly used HTTP Status Codes to better understand and handle the responses received from servers.
- Use appropriate HTTP Status Codes in your own applications to effectively communicate the outcome of client requests.
- Consider handling and displaying common HTTP Status Codes gracefully to provide a better user experience.


cURL

cURL is a command-line tool used for transferring data between servers and clients using various protocols such as HTTP, FTP, and SMTP.

- cURL is used in various applications and systems for automating requests and data transfers.
- It is commonly used in web development and testing to interact with APIs and perform HTTP requests.
- It is also used for downloading files, sending emails, and accessing remote servers via SSH.

How to use cURL:

1. Open your command-line interface (Terminal, Command Prompt, etc.).
2. Type the cURL command followed by the desired options and URL.
3. Press Enter to execute the cURL command.

Example code snippet to retrieve the content of a webpage using cURL:

bash
curl https://example.com


Takeaways / Best practices when using cURL:

- Familiarize yourself with the different options and flags available in cURL to customize the requests according to your needs.
- Use appropriate authentication options, such as providing API keys or credentials, when accessing secured resources.
- Take advantage of cURL's features like handling cookies, setting headers, and following redirects to simulate real-world scenarios accurately.
- Consider using cURL libraries or wrappers in your preferred programming language for more advanced usage and integration within your codebase.


Modules

- What is it? 
A module is a separate file in Python that contains reusable code and definitions, including variables, functions, and classes.

- Where is it used? 
Modules are used to organize and manage code, improve code reusability, and avoid naming conflicts in larger projects.

- How is it used? 
1. Importing a module:
- Use the `import` keyword followed by the name of the module.
- Access the functions, variables, or classes of the module using the dot notation.

2. Using module members:
- To use a function or variable from a module, use the module name followed by the member name, separated by a dot.
- If the module name is long or you want to use a shorter alias, you can use the `import ... as ...` syntax to assign a different name to the module.

3. Creating a module:
- Create a new Python file with the .py extension.
- Define the variables, functions, and classes in the file.
- Save the file and it becomes a module that can be imported and used in other programs.

Code snippet:

# Importing the math module
import math

# Using members from math module
print(math.pi)
print(math.sqrt(16))
print(math.cos(math.pi/3))

# Importing module with an alias
import datetime as dt
today = dt.date.today()
print(today)

# Creating a module (example_module.py)
# example_module.py contents:
#   def greet(name):
#       print("Hello, " + name)
#   my_variable = 123

# Importing the module we created
import example_module
example_module.greet("Alice")
print(example_module.my_variable)


Takeaways / Best Practices:
- Use modules to organize related code into separate files.
- Use meaningful and descriptive names for modules.
- Import only the necessary members from a module, using either `import module` or `from module import member`.
- Avoid naming conflicts by using different module names or aliasing.
- Create reusable modules to improve code reusability and maintainability.



Router

- Express.js Router is a middleware in Express.js framework that helps in modularizing the routing process.

- It is used in Express.js applications to handle different routes and HTTP methods separately.


- Express Router is used by creating an instance of it and attaching route handlers to it, which are then used in the main application.


 1. Require the express module and create an instance of the Router:

   

   const express = require('express');

    const router = express.Router();

 2. Attach route handlers to the router instance using the different HTTP methods:

   

 router.get('/', (req, res) => {

      res.send('Hello World!');

    });

 3. Mount the router in the main application:

  

  app.use('/', router);

   (where 'app' is the express application instance)


- Best practices for using Express.js Router:

  - Use Express Router to modularize and organize the routes in the application.

  - Group related routes together using Router instances.

  - Maintain a consistent structure and naming convention for routes.

  - Handle errors and send appropriate responses within the route handlers.

  - Keep the routes clean and avoid adding excessive logic within them.


Middlewares


- Middlewares are software components that act as a bridge between applications and other software services, helping in the management and manipulation of data and requests.
- Middlewares are widely used in web development, particularly in frameworks like Express.js, ASP.NET, and Django, to handle HTTP requests and responses.
- Middlewares are used in the following steps:
1. A middleware function is registered or defined in the application or framework.
2. When an HTTP request is received, the middleware function is executed before reaching the main application logic.
3. The middleware function can perform tasks like authentication, data validation, logging, modifying request/response, error handling, etc.
4. Based on the middleware logic, the function may choose to pass the request to the next middleware or terminate the request/response cycle.
5. Once the middleware operations are completed, the main application logic is executed.
6. After the final response is generated, the middleware function can also execute operations on the response before it is sent back to the client.

Example (Express.js):


const express = require('express');

const app = express();

// Middleware function - Logging
const logger = (req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
}

// Registering middleware
app.use(logger);

// Main application logic
app.get('/', (req, res) => {
res.send('Hello, World!');
});

// Start server
app.listen(3000, () => {
console.log('Server started on port 3000');
});



Takeaways / Best Practices:
- Middlewares provide a modular way to handle cross-cutting concerns like authentication, logging, and error handling, allowing separation of concerns in an application.
- Middlewares should be used to perform specific tasks and kept as lightweight as possible to maintain performance.
- The order of middleware registration matters as the execution order follows the same sequence.
- It's good practice to have error-handling middleware at the end to catch any errors thrown during request processing.
- Middlewares can be reused across different routes and applications, promoting code reusability.


Dotenv


- What is it?

dotenv is a dependency that allows developers to load environment variables from a .env file into their application's environment.

- Where is it used?

It is commonly used in development environments for managing sensitive information, such as API keys, database credentials, or other configuration variables.


- How is it used?

1. Install the dotenv package using a package manager like npm or yarn.

2. Create a .env file in the root directory of your project.

3. Add all the required environment variables in the .env file using the format `KEY=VALUE`, each on a new line.

4. Import and configure dotenv in your application to load the environment variables before any other code is executed. This typically involves adding the following line at the beginning of your entry point file (e.g., index.js):

`require('dotenv').config()`

5. Start accessing the environment variables in your code using `process.env.KEY`, where KEY is the specific variable you want to access.

- Takeaways / best practices:
- Use a .env file to store sensitive data and avoid hardcoding it in your codebase.
- Always remember to add the .env file to your project's .gitignore to avoid committing sensitive information to version control.
- Use descriptive variable names in the .env file and ensure consistency across the application.
- Periodically review and update the .env file as per the changing requirements or as new variables are added.
  - Be cautious with security: restrict access to the .env file and don't share it with others who don't require access to the sensitive data.


JOI Validation


- JOI Validation is a schema-based validation library for JavaScript.
- It is used in projects or applications where data validation is required, such as web forms, APIs, data transformations, etc.
- Steps to use JOI Validation:
1. Install JOI package: Use the package manager of your choice (e.g., npm) to install the JOI package in your project.
2. Define a schema: Create a schema object using the JOI library to define the validation rules for specific data fields.
3. Validate data: Use the schema to validate the data by calling the `validate` method of the JOI library, passing the data and the schema as arguments.
4. Check validation result: The `validate` method will return an object with properties like `error` and `value`. If the data passes the validation, the `error` property will be null, otherwise, it will contain the validation error message.

Code snippet example:

const Joi = require('joi');

// Define a schema
const schema = Joi.object({
  name: Joi.string().required(),
  email: Joi.string().email().required(),
  age: Joi.number().integer().min(18).max(99).required(),
});

// Data to be validated
const data = {
  name: 'John Doe',
  email: 'john.doe@example.com',
};

// Validate the data against the schema
const validation = schema.validate(data);

// Check validation result
if (validation.error) {
  console.error('Validation error:', validation.error);
} else {
  console.log('Data is valid:', validation.value);
}



Takeaways / best practices:
- Start with simple and specific validation rules and gradually add complexity if needed.
- Use chaining and composition to create complex validation rules.
- Utilize the various built-in validation methods provided by JOI for different data types and constraints.
- Handle validation errors properly and provide useful error messages to help users understand the issue.
- Document the validation rules and their purpose for future reference and maintenance.


Controllers


-What is it?
A controller is a software component responsible for receiving and handling user inputs, and deciding the appropriate actions to take.

-Where is it used?
Controllers are commonly used in software applications, especially web-based applications, to handle user interactions and manage the flow of data.

-How is it used?
1. The controller receives user inputs from various sources, such as UI elements or API calls.
2. It processes and validates the inputs to ensure they meet the required criteria.
3. The controller determines the appropriate actions to be taken based on the inputs, such as retrieving or manipulating data.
4. It interacts with other components of the application, such as models for data operations or views for displaying information.
5. Finally, the controller updates the appropriate components and returns the response back to the user.

-Takeaways / best practices (mandatory)
- Keep the controller lean and focused on handling inputs and coordinating actions, avoiding business logic or data storage operations.
- Use a naming convention to clearly identify and differentiate controllers from other components.
- Ensure separation of concerns by keeping the controller independent from the specific UI or technology used.
- Use appropriate design patterns, such as the Model-View-Controller (MVC), to organize and structure the application.
- Implement error handling and validation within the controller to provide better user experience and prevent security vulnerabilities.


REST APIs

- REST APIs (Representational State Transfer) are a set of architectural principles used to design networked applications that allow communication between different systems over the internet.

- REST APIs are used in various applications and industries, including web development, mobile app development, IoT (Internet of Things), and cloud services.

- Steps to use REST APIs:
1. Identify the resources and their unique identifiers (URLs/endpoints).
2. Determine the HTTP methods (GET, POST, PUT, DELETE) to interact with the resources.
3. Make HTTP requests to the respective endpoints using the appropriate method.
4. Receive HTTP responses containing the requested data or status information.

- Example code snippet for making a GET request to retrieve user information:


const axios = require('axios');

axios.get('https://api.example.com/users/123')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});

 
- Takeaways / best practices for using REST APIs:
- Use proper HTTP methods (GET, POST, PUT, DELETE) for appropriate actions on resources.
- Structure URLs/endpoints logically to represent resources and sub-resources.
- Return appropriate HTTP status codes to indicate the success or failure of requests.
- Use authentication and authorization mechanisms to secure the API.
- Implement versioning if the API may have breaking changes in the future.
  - Provide comprehensive and meaningful error messages in response bodies.


bcrypt.js

bcrypt.js is a JavaScript library for password hashing using the bcrypt hashing algorithm.

It is commonly used in web applications and server-side environments to securely store passwords.

Here are the steps to use bcrypt.js for password hashing:

1. Install bcrypt.js: Use a package manager like npm or yarn to install the bcrypt.js library in your project.
2. Import bcrypt.js: In your JavaScript file, import the bcrypt.js library using the `require` or `import` statement.
3. Generate a salt: Use the `genSalt` function provided by bcrypt.js to generate a salt, which is a random string of characters used as additional input during password hashing.
4. Hash the password: Use the `hash` function provided by bcrypt.js to hash the password using the generated salt. You need to pass the password and the salt to the `hash` function.
5. Store the hashed password: Store the hashed password in your database or wherever you need to persist the user's password.

Here is an example code snippet that demonstrates the usage of bcrypt.js for password hashing:



const bcrypt = require('bcryptjs');

const password = 'myPassword123';

async function hashPassword(password) {
try {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
console.log(hashedPassword);
} catch (error) {
console.error(error);
}
}

hashPassword(password);



Takeaways / Best practices:

- Password hashing is crucial in securing user data, and using a strong hashing algorithm like bcrypt is recommended.
- Always store the salt along with the hashed password, as it is needed during password verification.
- Use a sufficiently high value for the cost parameter in `genSalt` function to increase the time and computational resources required to calculate each hash, making it more difficult for attackers to perform brute-force attacks.
- Use asynchronous functions provided by bcrypt.js to ensure non-blocking behavior in server-side environments.
- Regularly update bcrypt.js library to have the latest security updates and improvements.


JWT


JWT stands for JSON Web Token. It is a compact, URL-safe means of representing claims to be transferred from one party to another as a JSON object.

- JWT is commonly used for authentication and authorization purposes in web applications.
- The token is generated by the server and sent to the client upon successful authentication.
- The client includes the token in subsequent requests to the server for accessing protected resources.
- The server verifies the integrity of the token and extracts the information contained within it to authenticate and authorize the client.

Best practices for using JWT:
1. Keep the token's expiration time short to mitigate the risk of unauthorized access.
2. Use secure methods for transmitting and storing the token to prevent interception or tampering.
3. Include only necessary information in the token to minimize its size and exposure of sensitive data.
4. Verify the token's signature on the server-side to ensure its authenticity.
5. Implement token revocation mechanisms for handling compromised tokens.
6. Consider using additional security measures like SSL/TLS for secure communication.


Passport.js


Passport.js is a widely-used middleware for authenticating users in Node.js applications. It is primarily used in web applications that require user authentication.

Passport.js is used in the following steps:

1. Configuration: Set up Passport.js in your Node.js application by installing necessary dependencies and configuring strategies. Strategies determine how you will authenticate users, such as using local credentials, OAuth, or OpenID.

2. Initialization: Initialize Passport.js and set it as middleware in your application. This is done by calling the `passport.initialize()` function and placing it before any route handlers.

3. Authentication: Implement authentication routes that correspond to the strategies used. For example, a login route would authenticate the user's credentials and issue a session upon success. Passport.js provides middleware functions like `passport.authenticate()` to handle this.

4. Sessions: Passport.js can seamlessly integrate with session management. Once a user is authenticated, Passport.js serializes the user object and stores it in the session. On subsequent requests, Passport.js deserializes the user object from the session.

5. Authorization: Passport.js can also handle user authorization using middleware functions like `passport.authorize()`. This enables you to restrict access to specific routes or resources based on user roles or permissions.

Code Snippet illustrating usage:

// Passport.js configuration
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;

// Register LocalStrategy
passport.use(new LocalStrategy(
function(username, password, done) {
// Authenticate user credentials here
...
}
));

// Use Passport.js middleware
app.use(passport.initialize());
app.use(passport.session());

// Authentication route
app.post('/login', passport.authenticate('local', {
successRedirect: '/dashboard',
failureRedirect: '/login',
}));

// Authorization middleware
function isAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
}

// Protected route
app.get('/dashboard', isAuthenticated, (req, res) => {
// render dashboard page
});




Best Practices / Takeaways:
- Always encrypt and securely store user passwords to ensure their safety.
- Implement login failure handling and properly communicate error messages to the user.
- Store as little user information as possible in the session, and perform additional checks when retrieving user data from the session.
- Understand and use the appropriate authentication strategy for your authentication needs (e.g., local, social media, etc.).
- Regularly update Passport.js and its dependencies to benefit from the latest security patches.


TDD

-What is it?
TDD (Test-Driven Development) is a software development approach that emphasizes writing tests before writing the actual code.

-Where is it used?
TDD is commonly used in agile software development methodologies and is applicable in any project where automated testing is possible.

-How is it used?
1. Write a failing test: Start by writing a test case that represents a small unit of functionality that is yet to be implemented.
2. Run the test: Execute the test and ensure it fails as expected. This step helps in verifying the test case is working correctly.
3. Write the minimum amount of code: Write the minimum amount of production code required to pass the test. The focus should be on making the test pass without worrying about the implementation details.
4. Run the test again: Execute the test after writing the initial code. It should pass now if the implementation is correct.
5. Refactor the code: Improve the code quality, remove duplication, and ensure the code is clean without altering the functionality.
6. Repeat the process: Write another failing test to add new functionality or extend existing one, and then go through the steps again.

-Takeaways / best practices (mandatory):
- Write simple and concise tests that cover a specific unit of functionality.
- Refactor the code after each test passes to maintain code quality.
- Use testing frameworks and tools to automate the process.
- Use TDD to drive the design and architecture decisions of the application.
- TDD promotes early identification of issues and improves the test coverage of the codebase.


Express.js

Express.js is a minimalistic web application framework for Node.js that simplifies the process of building server-side applications and APIs.

Express.js is used in various scenarios, including building RESTful APIs, single-page applications, and web servers.

The steps to use Express.js are as follows:
1. Install Node.js and npm (if not already installed).
2. Create a new directory for your project.
3. Initialize a new Node.js project using the command "npm init" in the terminal and follow the prompts.
4. Install Express.js module by running the command "npm install express" in the terminal.
5. Create an Express application file (e.g., "app.js") and import the Express module using the "require" function.
6. Define the routes and handling logic for the application by specifying the HTTP method and route path, and providing a callback function.
7. Set up the server to listen on a specific port using the "listen" method.

Code snippet to create a basic Express.js server:


const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
res.send('Hello World!');
});

app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});



Takeaways / best practices for using Express.js:
- Middleware functions can be used to add additional functionality to the application, such as logging, parsing request bodies, etc.
- Use routing to handle different HTTP methods and paths for better organization and maintainability of the code.
- Always handle errors properly by using error-handling middleware.
- Use environment variables to store configuration settings like port numbers, database URLs, etc., to make the application more flexible and portable.
- Consider using other helpful libraries/packages with Express.js, such as body-parser for parsing request bodies or helmet for adding security-related HTTP headers.

Routing in Express.js

Routing in Express.js is the process of mapping HTTP methods and URLs to specific functions or handlers that will be executed when those URLs are requested.

Routing is used in web applications built with Express.js to handle different routes or URLs and execute specific code or functions based on the requested URL.

Routing in Express.js is used as follows:

1. Create an instance of the Express application.
2. Use the HTTP method functions such as `app.get()`, `app.post()`, `app.put()`, etc., to define different routes and the corresponding functions that will handle those routes.
3. Define the route paths using URL patterns and optional parameters.
4. Inside the route handler functions, perform the required actions or functionality and send the response back to the client.

Example code snippet for routing in Express.js:

const express = require('express');
const app = express();

// Define a route for the root URL
app.get('/', (req, res) => {
res.send('Hello, World!');
});

// Define a route for a specific URL with a parameter
app.get('/users/:id', (req, res) => {
const userId = req.params.id;
res.send('User ID: ' + userId);
});

// Start the server
app.listen(3000, () => {
console.log('Server started on port 3000');
});



Takeaways / Best Practices:
- Use meaningful and descriptive route paths to improve readability and maintainability of the code.
- Use route parameters to extract dynamic values from the URL.
- Organize routes into separate files or modules for better organization and manageability.
- Use middleware functions for common tasks or validations to avoid duplication across routes.
- Follow RESTful conventions for structuring routes and handling different HTTP methods.



SQL vs NoSQL DB

SQL (Structured Query Language):
- SQL is a language used for managing and manipulating structured data in a relational database management system (RDBMS).
- It is used in various industries and domains where structured data is involved, such as finance, healthcare, e-commerce, etc.
- SQL is used to create, modify, and retrieve data from relational databases.
- Example steps for using SQL:
1. Create a database and tables with appropriate fields.
2. Insert data into the specified tables.
3. Query the database to retrieve specific information.
4. Update or delete data as required.
 5. Perform various operations like joining tables, grouping data, etc.


- Code snippet (MySQL):


-- Create a table
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
salary DECIMAL(10,2)
);


-- Insert data into the table

INSERT INTO employees (id, name, age, salary)
VALUES (1, 'John Doe', 30, 50000),
(2, 'Jane Smith', 25, 45000),
(3, 'Sam Johnson', 35, 60000);


-- Query the table
SELECT * FROM employees WHERE age > 25;

-- Update data
UPDATE employees SET salary = 55000 WHERE id = 2;

-- Delete data
DELETE FROM employees WHERE id = 3;


- Takeaways / best practices:
- Define appropriate indexes on the columns used frequently in search conditions to improve query performance.
- Normalize the database schema to avoid data redundancy and maintain data integrity.
- Use parameterized queries or prepared statements to prevent SQL injection attacks.
- Regularly backup the database to avoid data loss in case of any mishaps.

NoSQL (Not only SQL):
- NoSQL is a database approach that provides a non-relational and flexible way to store and retrieve data.
- It is used in modern web applications, big data applications, IoT systems, etc., where scalability, high availability, and flexible data models are important.
- NoSQL databases are used to store unstructured, semi-structured, or structured data.
- Unlike SQL, NoSQL databases do not require a predefined schema and support horizontal scalability.


- Example steps for using NoSQL (MongoDB, a document-based NoSQL database):
1. Install and set up the NoSQL database system.
2. Create a database.
3. Insert documents (data) into the database. Documents can have varying structures.
4. Query the database to retrieve specific documents based on different criteria.
  5. Update or delete documents as required.


- Takeaways / best practices:
- Determine the data access patterns and design the data model accordingly.
- Denormalize data if necessary to improve read performance.
- Utilize indexes for faster querying.
- Understand the trade-offs between consistency, availability, and partition tolerance (CAP theorem) when choosing a NoSQL database.
  - Regularly monitor and optimize the database performance for efficient data handling.


JSON and BSON

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate.

JSON is used in various applications, such as web services and data storage, for transmitting and storing structured data.

How JSON is used:

1. Data Representation: JSON is used to represent data objects as a collection of key-value pairs, which are enclosed within curly braces {}.
2. Data Serialization: JSON can be converted into a string format for transferring data easily between different platforms or languages.
3. Data Parsing: JSON can be parsed by different programming languages to retrieve the data and utilize it.
4. Data Storage: JSON can be stored in databases or files for persistent storage.


Takeaways / best practices:
- Use clear and meaningful key names in JSON to enhance readability.
- Validate JSON data to ensure it conforms to the expected schema.
- Minimize the size of JSON payloads by removing unnecessary whitespace or using compression algorithms.
- Be careful with parsing untrusted JSON data to avoid security risks like code injections.


Collections


What is it? 

A database is a structured collection of data that is organized and managed in order to be easily accessed, manipulated, and updated.


Where is it used?

Databases are used in various applications and industries, including websites, financial systems, human resources management, inventory management, healthcare systems, and many others.


How is it used? 

1. Data Modeling: Analyzing and designing the structure and relationships of data entities.

2. Data Definition: Creating the database schema and defining the tables, columns, constraints, and relationships.

3. Data Manipulation: Inserting, updating, deleting, and retrieving data from the database.

4. Data Querying: Using query languages (e.g., SQL) to retrieve specific data based on conditions and perform complex operations.

5. Data Management: Ensuring data integrity, security, and backup/restore procedures.


Takeaways / Best Practices:

- Understand the data requirements and design a proper schema before building the database.

- Normalize the data to eliminate redundancy and improve efficiency.

- Use indexes and keys to optimize query performance.

- Implement security measures to protect data from unauthorized access.

- Regularly backup the database to prevent data loss.

- Regularly optimize and tune the database for optimal performance.


Databases

- What is it? 
A database is a structured collection of data that is organized and managed in order to be easily accessed, manipulated, and updated.

- Where is it used?
Databases are used in various applications and industries, including websites, financial systems, human resources management, inventory management, healthcare systems, and many others.

- How is it used? 
1. Data Modeling: Analyzing and designing the structure and relationships of data entities.
2. Data Definition: Creating the database schema and defining the tables, columns, constraints, and relationships.
3. Data Manipulation: Inserting, updating, deleting, and retrieving data from the database.
4. Data Querying: Using query languages (e.g., SQL) to retrieve specific data based on conditions and perform complex operations.
5. Data Management: Ensuring data integrity, security, and backup/restore procedures.

Takeaways / Best Practices:
- Understand the data requirements and design a proper schema before building the database.
- Normalize the data to eliminate redundancy and improve efficiency.
- Use indexes and keys to optimize query performance.
- Implement security measures to protect data from unauthorized access.
- Regularly backup the database to prevent data loss.
- Regularly optimize and tune the database for optimal performance.


mongo shell

Mongo shell is an interactive JavaScript interface used for connecting to and interacting with MongoDB instances.

- It is used for performing various operations on MongoDB databases, such as querying, updating, inserting, and deleting data.
- It provides a command-line interface to communicate with MongoDB.
- It is used by developers, administrators, and users to manage databases and perform administrative tasks.
- It supports JavaScript expressions, making it flexible and powerful for manipulating data and performing complex operations.
- Mongo shell can be used to connect to a MongoDB instance by specifying the hostname/port and authentication credentials.
- Once connected, users can execute MongoDB commands, write JavaScript functions, and query data.
- Common operations include finding documents, updating values, aggregating data, and creating indexes.

Code Snippet (Connecting to a MongoDB instance using Mongo shell):

$ mongo --host <hostname> --port <port> -u <username> -p <password> --authenticationDatabase <authDB>



Takeaways / Best Practices:
- Master JavaScript operations and syntax to leverage the full potential of Mongo shell.
- Use proper indexing to improve query performance.
- Be cautious with destructive operations like dropping databases or collections as they are irreversible.
- Familiarize yourself with MongoDB commands and operators for effective querying and manipulation.
- Optimize queries by using appropriate filters and aggregation pipelines.
- Consider enabling authentication and access control for improved security.

CRUD operations using mongo shell


CRUD Operations, which stands for Create, Read, Update, and Delete, are the basic operations for handling data in a database.


- They are used in databases, including MongoDB, to perform various actions on data stored in collections.


- The MongoDB shell is an interactive JavaScript interface to MongoDB that allows users to execute commands directly and interact with the database.


How to use CRUD Operations in the MongoDB shell:


1. Create:

   - To insert new documents into a collection, use the `insertOne()` or `insertMany()` method.

   - Syntax: `db.collection.insertOne(document)` or `db.collection.insertMany(documents)`

   - Example: `db.students.insertOne({ name: ""John"", age: 25 })`



2. Read:

   - To query and read documents from a collection, use the `find()` method.

   - Syntax: `db.collection.find(query, projection)`

   - Example: `db.students.find({ name: ""John"" })`



3. Update:

   - To update existing documents in a collection, use the `updateOne()` or `updateMany()` method.

   - Syntax: `db.collection.updateOne(filter, update, options)` or `db.collection.updateMany(filter, update, options)`

   - Example: `db.students.updateOne({ name: ""John"" }, { $set: { age: 26 } })`



4. Delete:

   - To remove documents from a collection, use the `deleteOne()` or `deleteMany()` method.

   - Syntax: `db.collection.deleteOne(filter)` or `db.collection.deleteMany(filter)`

   - Example: `db.students.deleteOne({ name: ""John"" })`


Takeaways / Best Practices:

- Always ensure you have a backup or make use of transaction features when performing critical CRUD operations to avoid data loss.

- Use proper filtering criteria to reduce unnecessary data retrieval and limit the impact on performance.

- Utilize indexes on commonly used fields to improve read performance.

- Choose the appropriate operation based on the desired outcome to ensure accurate and efficient data manipulations.



Mongoose ODM

- Mongoose ODM is an Object Data Modeling tool used for designing and interacting with MongoDB databases in Node.js applications.

- It is used in Node.js applications to provide a structured schema-based solution for modeling, querying, and manipulating MongoDB data.

- Steps for using Mongoose ODM:

1. Install Mongoose using npm: `npm install mongoose`

2. Require the mongoose module in your Node.js application: `const mongoose = require('mongoose')`

3. Connect to the MongoDB database using Mongoose: `mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true })`

4. Define a Mongoose schema to create a structure for your data:

const userSchema = new mongoose.Schema({
name: String,
age: Number,
email: String
});


5. Create a Mongoose model using the schema: `const User = mongoose.model('User', userSchema)`

6. Perform CRUD operations on MongoDB using Mongoose methods:


- Create new data:

const newUser = new User({
name: 'John Doe',
age: 25,
email: 'johndoe@example.com'
});

newUser.save((err) => {
if (err) {
console.error('Error saving user:', err);
} else {
console.log('User saved successfully!');
}
});


- Read data:

User.find({}, (err, users) => {
if (err) {
console.error('Error retrieving users:', err);
} else {
console.log('Retrieved users:', users);
}
});


- Update data:

User.updateOne({ name: 'John Doe' }, { age: 26 }, (err) => {
if (err) {
console.error('Error updating user:', err);
} else {
console.log('User updated successfully!');
}
});


- Delete data:

User.deleteOne({ name: 'John Doe' }, (err) => {
if (err) {
console.error('Error deleting user:', err);
} else {
console.log('User deleted successfully!');
}
});



- Takeaways / Best practices:
   - Define and use schemas to have a consistent structure for your data.
- Use Mongoose models to perform database operations and interact with MongoDB.
- Utilize Mongoose's validation features to ensure data integrity.
- Leverage Mongoose's middleware functionality for pre and post hooks to execute custom logic.
- Use indexes to improve performance in queries.
   - Handle errors properly when performing CRUD operations to ensure robustness.


Mongoose Schema

- A Mongoose Schema is a configuration object that defines the structure and properties of documents in a MongoDB collection.

- Mongoose Schemas are used in Node.js applications with MongoDB as the database to provide a straightforward way to model and interact with the data.

- Steps to use Mongoose Schema:


 1. Install Mongoose package: 

npm install mongoose



 2. Import Mongoose:

 const mongoose = require('mongoose');


3. Define a Schema object with desired fields and their datatypes using Mongoose.Schema constructor. For example:

const userSchema = new mongoose.Schema({
name: String,
age: Number,
email: {
type: String,
required: true
}
});


4. Create a Mongoose model using the defined Schema. For example:

const User = mongoose.model('User', userSchema);


5. Use the model to perform database operations like creating, reading, updating, or deleting documents.

- A code snippet example to demonstrate Mongoose Schema:

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
name: String,
age: Number,
email: {
type: String,
required: true
}
});

const User = mongoose.model('User', userSchema);

const newUser = new User({
name: 'John Doe',
age: 25,
email: 'john@example.com'
});

newUser.save()
.then((user) => console.log(user))
.catch((error) => console.log(error));

 

- Takeaways / best practices:
  - Define the Schema with appropriate field types, validations, and options to ensure data consistency and integrity.
- Use meaningful field names and follow naming conventions to improve code readability.
- Separate the Schema definitions into separate files to keep the code modular and maintainable.
  - Leverage Mongoose features like middleware, hooks, and virtuals to handle advanced data operations and transformations.


Mongoose connect


-What is it?
Mongoose connect is a method used to establish a connection between a Node.js application and a MongoDB database using the Mongoose library.

-Where is it used?
Mongoose connect is commonly used in Node.js applications that require interaction with a MongoDB database using the Mongoose library.

-How is it used?
1. Install the Mongoose library in your Node.js project using npm or yarn.
2. Import the mongoose module in your Node.js application.
3. Call the mongoose connect method, passing in the connection string for your MongoDB database as the argument. Additionally, you can pass in various options to customize the connection.
4. Handle any errors that may occur during the connection process.
5. Optionally, listen for specific events emitted by the connection, such as the "connected" event when the connection is successfully established.
6. Use the connected mongoose object to define schemas, models, and perform database operations.

---Code snippet to explain the same

javascript
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mydatabase', { 
   useNewUrlParser: true,
   useUnifiedTopology: true 
})
.then(() => {
   console.log('Connected to the database');
})
.catch((error) => {
   console.error('Failed to connect to the database:', error);
});



---Takeaways / best practices
- Use appropriate error handling when connecting to the database to gracefully handle any connection issues.
- Close the database connection when it is no longer needed to free up system resources.
- Protect sensitive information, such as the connection string, by using environment variables or a configuration file.


Querying in MongoDB

-What is it? 

Querying in MongoDB refers to the process of searching and retrieving specific data from a MongoDB database based on specific criteria.

-Where is it used?

Querying is used in applications that store data in a MongoDB database and need to retrieve specific information based on certain conditions.

-How is it used?

To query data in MongoDB, the following steps are typically followed:

1. Connect to the MongoDB database.
2. Choose the appropriate database and collection to query.
3. Define the query criteria using MongoDB's query language. This can include specifying conditions on fields, ranges of values, or even complex expressions.
4. Execute the query and retrieve the results.
5. Process and use the queried data as desired.

Querying can be done using various methods or tools, such as the MongoDB shell, programming languages like JavaScript or Python with MongoDB drivers, or through MongoDB's Atlas web interface or other GUI tools.

Takeaways / best practices:

- Design the data model and structure your documents based on the anticipated querying requirements.
- Create appropriate indexes for fields involved in frequent query operations to improve query performance.
- Use the query language effectively by understanding its features, operators, and functions.
- Leverage aggregation pipelines for complex querying and data manipulation needs.
- Monitor and analyze query performance to optimize and fine-tune the queries.



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article