How loopbackio/loopback4-example-shopping works
Main Components
• Frontend Client: Represented by the static assets in the “packages/shopping/public” directory (e.g., shoppy.html, index.html, and related JavaScript/CSS). This is the web page interface for the shopping experience.
• API Backend (Shopping App): The core LoopBack application found under “packages/shopping/src”. This includes controllers (e.g., user-management, shopping-cart, user-order, ping, product), models, datasources (for MongoDB and Redis), repositories, and services (e.g., recommender, user management, JWT, email, password hashing, and validator).
• Recommender Service: A separate packaged module under “packages/recommender” that exposes recommendation functionality via gRPC and REST endpoints. It acts as an external service that the shopping app calls to obtain product recommendations.
• Databases:
• MongoDB serves as the primary database for models like User, Product, Order, etc.
• Redis is used explicitly for maintaining shopping cart state.
• External Email Provider: Nodemailer is used for sending password reset emails.
• Infrastructure/Deployment: Dockerfiles (several variants for monorepo, recommender, and shopping), Kubernetes configuration files (in the “kubernetes” directory), and OpenShift files. This indicates that the system is containerized and intended to be deployed in cloud environments (Kubernetes cluster or OpenShift).
Relationships and Interactions
• The Web Browser (client) accesses the shopping website statically served by the “packages/shopping/public” assets.
• The client interacts with the backend API (LoopBack app) via REST endpoints exposed by controllers.
• Controllers in the backend layer invoke services for core business logic:
• Authentication and Authorization: Using JWT service, UserService (via @loopback/authentication), and voter functions for role-based access.
• Shopping Cart Management and Order placement: Controllers call the repositories and services to persist or retrieve data from MongoDB and Redis.
• Product Recommendation: The shopping service calls the recommender service (over gRPC or REST) to obtain personalized recommendations.
• Email and Password Reset: The email service integrating Nodemailer is invoked during password reset flows.
• Database Interactions:
• MongoDB is used as the main persistent store (accessed via LoopBack datasources).
• Redis is used for fast access to session or shopping cart data.
• Infrastructure:
• Docker is used for containerizing the apps.
• Kubernetes or OpenShift resources deploy the applications as separate microservices (shopping app container, recommendation container, databases, etc.).
Architectural Patterns and Design Principles
• The project adopts the separation of concerns where the frontend, backend API, and the recommendation service are clearly defined.
• Domain-driven design is visible through the distinct models (User, Product, Order, etc.) and their associated controllers, repositories, and services.
• The use of LoopBack 4 patterns (dependency injection, repositories & datasources, decorators for controllers and models) underpins the server structure.
• Containerized deployment and orchestration emphasize a cloud-native microservices architecture.