Home  Dsa   System desi ...

System design for ecommerce app

Designing a system for an eCommerce application involves structuring various components to ensure scalability, performance, and reliability. Here’s a detailed outline of the system design for an eCommerce app using Node.js:

1. Functional Requirements

2. Non-functional Requirements

3. Architecture

3.1 Microservices Architecture

3.2 Service Communication

3.3 Database Design

3.4 Caching Strategy

4. Security Considerations

5. Infrastructure

6. Monitoring and Logging

7. Deployment

8. Third-Party Integrations

9. User Experience

10. Legal and Compliance

Sample models and code

To design a basic eCommerce system using Node.js, let's outline the classes, methods, and database schema you might need. This example will cover essential components such as users, products, orders, and reviews. We'll use a simplified approach with a relational database schema (using PostgreSQL for this example) and basic CRUD operations.

1. Classes and Methods

User Class

class User {
  constructor(id, username, email, passwordHash) {
    this.id = id;
    this.username = username;
    this.email = email;
    this.passwordHash = passwordHash;
  }

  async save() {
    // Method to save user data to the database
    // Example implementation with PostgreSQL:
    const query = `
      INSERT INTO users (username, email, password_hash)
      VALUES ($1, $2, $3)
      RETURNING id
    `;
    const values = [this.username, this.email, this.passwordHash];
    const result = await db.query(query, values);
    this.id = result.rows[0].id;
    return this;
  }

  static async findById(id) {
    // Method to find a user by ID from the database
    const query = `
      SELECT * FROM users
      WHERE id = $1
    `;
    const values = [id];
    const result = await db.query(query, values);
    const userData = result.rows[0];
    if (!userData) {
      return null;
    }
    return new User(userData.id, userData.username, userData.email, userData.password_hash);
  }

  async updateProfile(newUsername, newEmail) {
    // Method to update user profile
    this.username = newUsername;
    this.email = newEmail;
    const query = `
      UPDATE users
      SET username = $1, email = $2
      WHERE id = $3
    `;
    const values = [this.username, this.email, this.id];
    await db.query(query, values);
    return this;
  }

  async delete() {
    // Method to delete user account
    const query = `
      DELETE FROM users
      WHERE id = $1
    `;
    const values = [this.id];
    await db.query(query, values);
  }
}

Product Class

class Product {
  constructor(id, name, description, price) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.price = price;
  }

  async save() {
    // Method to save product data to the database
    // Example implementation with PostgreSQL:
    const query = `
      INSERT INTO products (name, description, price)
      VALUES ($1, $2, $3)
      RETURNING id
    `;
    const values = [this.name, this.description, this.price];
    const result = await db.query(query, values);
    this.id = result.rows[0].id;
    return this;
  }

  static async findById(id) {
    // Method to find a product by ID from the database
    const query = `
      SELECT * FROM products
      WHERE id = $1
    `;
    const values = [id];
    const result = await db.query(query, values);
    const productData = result.rows[0];
    if (!productData) {
      return null;
    }
    return new Product(productData.id, productData.name, productData.description, productData.price);
  }

  async updateDetails(newName, newDescription, newPrice) {
    // Method to update product details
    this.name = newName;
    this.description = newDescription;
    this.price = newPrice;
    const query = `
      UPDATE products
      SET name = $1, description = $2, price = $3
      WHERE id = $4
    `;
    const values = [this.name, this.description, this.price, this.id];
    await db.query(query, values);
    return this;
  }

  async delete() {
    // Method to delete a product
    const query = `
      DELETE FROM products
      WHERE id = $1
    `;
    const values = [this.id];
    await db.query(query, values);
  }
}

Order Class

class Order {
  constructor(id, userId, totalAmount, status) {
    this.id = id;
    this.userId = userId;
    this.totalAmount = totalAmount;
    this.status = status;
    this.createdAt = new Date();
  }

  async save() {
    // Method to save order data to the database
    // Example implementation with PostgreSQL:
    const query = `
      INSERT INTO orders (user_id, total_amount, status, created_at)
      VALUES ($1, $2, $3, $4)
      RETURNING id
    `;
    const values = [this.userId, this.totalAmount, this.status, this.createdAt];
    const result = await db.query(query, values);
    this.id = result.rows[0].id;
    return this;
  }

  static async findById(id) {
    // Method to find an order by ID from the database
    const query = `
      SELECT * FROM orders
      WHERE id = $1
    `;
    const values = [id];
    const result = await db.query(query, values);
    const orderData = result.rows[0];
    if (!orderData) {
      return null;
    }
    return new Order(orderData.id, orderData.user_id, orderData.total_amount, orderData.status);
  }

  async updateStatus(newStatus) {
    // Method to update order status
    this.status = newStatus;
    const query = `
      UPDATE orders
      SET status = $1
      WHERE id = $2
    `;
    const values = [this.status, this.id];
    await db.query(query, values);
    return this;
  }

  async delete() {
    // Method to delete an order
    const query = `
      DELETE FROM orders
      WHERE id = $1
    `;
    const values = [this.id];
    await db.query(query, values);
  }
}

Review Class

class Review {
  constructor(id, productId, userId, rating, comment) {
    this.id = id;
    this.productId = productId;
    this.userId = userId;
    this.rating = rating;
    this.comment = comment;
    this.createdAt = new Date();
  }

  async save() {
    // Method to save review data to the database
    // Example implementation with PostgreSQL:
    const query = `
      INSERT INTO reviews (product_id, user_id, rating, comment, created_at)
      VALUES ($1, $2, $3, $4, $5)
      RETURNING id
    `;
    const values = [this.productId, this.userId, this.rating, this.comment, this.createdAt];
    const result = await db.query(query, values);
    this.id = result.rows[0].id;
    return this;
  }

  static async findByProductId(productId) {
    // Method to find reviews for a product from the database
    const query = `
      SELECT * FROM reviews
      WHERE product_id = $1
    `;
    const values = [productId];
    const result = await db.query(query, values);
    return result.rows.map(row => new Review(row.id, row.product_id, row.user_id, row.rating, row.comment));
  }

  async delete() {
    // Method to delete a review
    const query = `
      DELETE FROM reviews
      WHERE id = $1
    `;
    const values = [this.id];
    await db.query(query, values);
  }
}

2. Database Schema (PostgreSQL Example)

Users Table

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  username VARCHAR(255) NOT NULL,
  email VARCHAR(255) UNIQUE NOT NULL,
  password_hash VARCHAR(255) NOT NULL
);

Products Table

CREATE TABLE products (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  description TEXT,
  price DECIMAL(10, 2) NOT NULL
);

Orders Table

CREATE TABLE orders (
  id SERIAL PRIMARY KEY,
  user_id INT REFERENCES users(id),
  total_amount DECIMAL(10, 2) NOT NULL,
  status VARCHAR(50) NOT NULL,
  created_at TIMESTAMP NOT NULL
);

Reviews Table

CREATE TABLE reviews (
  id SERIAL PRIMARY KEY,
  product_id INT REFERENCES products(id),
  user_id INT REFERENCES users(id),
  rating INT NOT NULL,
  comment TEXT,
  created_at TIMESTAMP NOT NULL
);

3. Usage Example

// Example usage of classes and methods

// Create a new user
const newUser = new User(null, 'john_doe', '[email protected]', 'hashed_password');
await newUser.save();

// Find a user by ID
const existingUser = await User.findById(1);

// Update user profile
await existingUser.updateProfile('johndoe', '[email protected]');

// Delete user account
await existingUser.delete();

// Create a new product
const newProduct = new Product(null, 'Sample Product', 'Description of the product', 99.99);
await newProduct.save();

// Find a product by ID
const existingProduct = await Product.findById(1);

// Update product details
await existingProduct.updateDetails('Updated Product Name', 'Updated description', 129.99);

// Delete a product
await existingProduct.delete();

// Create an order
const newOrder = new Order(null, 1, 199.99, 'pending');
await newOrder.save();

// Find an order by ID
const existingOrder = await Order.findById(1);

// Update order status


await existingOrder.updateStatus('shipped');

// Delete an order
await existingOrder.delete();

// Create a review for a product
const newReview = new Review(null, 1, 1, 5, 'Great product!');
await newReview.save();

// Find reviews for a product
const productReviews = await Review.findByProductId(1);

// Delete a review
await productReviews[0].delete();
Published on: Jul 10, 2024, 12:19 AM  
 

Comments

Add your comment