Skip to content

NovelNestHQ/inventory-service

Repository files navigation

NovelNest - Inventory Service

1. Project Title and Description

NovelNest is a platform designed for book enthusiasts to discover, exchange, and manage their book collections. It aims to connect readers and facilitate the sharing of physical books within a community.

The Inventory Service is a core microservice within the NovelNest application responsible for managing the book inventory. It handles operations such as adding new books, updating stock levels, and querying book availability.

2. Tech Stack

  • Backend: Node.js, Express.js
  • Database: PostgreSQL
  • ORM: Prisma
  • Messaging Queue: RabbitMQ (for inter-service communication, e.g., updating inventory after an order)
  • Containerization: Docker
  • Orchestration: Kubernetes

3. Project Structure

inventory-service/
├── docker-compose.yaml       # Docker Compose for local development dependencies (Postgres, RabbitMQ)
├── Dockerfile                # Defines the Docker image for the service
├── package.json              # Node.js project dependencies and scripts
├── README.md                 # This file
├── tsconfig.json             # TypeScript configuration
├── hack/
│   └── add_books.sh          # Script to add sample book data
├── prisma/
│   ├── schema.prisma         # Prisma schema defining the database models
│   └── migrations/           # Database migration files generated by Prisma
└── src/
    ├── db.ts                 # Prisma client initialization
    ├── index.ts              # Main application entry point (Express server setup)
    ├── initDB.ts             # Potentially for initial DB setup/checks
    ├── middleware.ts         # Express middleware definitions
    ├── rabbitmq.ts           # RabbitMQ connection and channel setup logic
    ├── types.ts              # TypeScript type definitions
    └── routes/               # API route handlers (specific structure may vary)

4. Features

  • Manage book details (ISBN, title, author, etc.).
  • Track stock levels for each book.
  • Add new books to the inventory.
  • Update book quantities (e.g., decrementing stock after an order).
  • Provide API endpoints for querying book information and availability.
  • Listens for events (e.g., via RabbitMQ) to update inventory based on actions in other services.

5. Prerequisites

  • Node.js (Check package.json or .nvmrc for specific version, e.g., v18+)
  • npm or yarn
  • Docker & Docker Compose
  • kubectl (for Kubernetes deployment)
  • kind (or other Kubernetes cluster like Minikube, k3d, or cloud provider)
  • Git

6. Local Development Setup

  1. Clone the Repository:

    git clone https://github.com/NovelNestHQ/inventory-service
    cd inventory-service
  2. Install Dependencies:

    npm install
    # or
    yarn install
  3. Set up Environment Variables: Create a .env file in the inventory-service directory and add the necessary variables. Refer to the docker-compose.yaml and Kubernetes manifests (k8s-manifests/inventory-service/inventory-service.yaml) for required variables like:

    DATABASE_URL="postgresql://user:password@localhost:5434/inventorydb?schema=public" # Adjust port/credentials if needed
    RABBITMQ_URL="amqp://guest:guest@localhost:5672" # Adjust if needed
    PORT=3002
    # Add any other required variables

    Note: The docker-compose.yaml file in this directory likely sets up PostgreSQL and RabbitMQ containers with default credentials.

  4. Start Dependent Services (Database, RabbitMQ):

    docker-compose up -d postgres rabbitmq # Starts Postgres and RabbitMQ defined in docker-compose.yaml

    Wait a few moments for the database and message queue to initialize.

  5. Run Database Migrations:

    npx prisma migrate dev --name init
  6. Run the Service:

    npm run dev

    The service should now be running, typically on http://localhost:3002.

7. Deployment to Kubernetes

The NovelNest application is designed to be deployed on Kubernetes.

  1. Ensure you have a Kubernetes cluster running. (e.g., using kind create cluster)

  2. Navigate to the Kubernetes manifests directory:

    cd ../k8s-manifests # From the inventory-service directory
  3. Deploy the entire NovelNest application (Recommended): The easiest way is to use the top-level deployment script which handles dependencies and order:

    ./deploy-all.sh

    This script will deploy all services, databases, RabbitMQ, Kong API Gateway, etc.

  4. Deploy only the Inventory Service (If needed): If you need to deploy or update only the inventory service and its specific dependencies (Postgres):

    cd inventory-service/
    ./deploy-inventory-service.sh

    Note: This assumes other core components like RabbitMQ and the Kong Gateway are already deployed.

  5. Accessing the Service: The services are typically exposed via the Kong API Gateway. Find the external IP or port-forward Kong:

    • Using LoadBalancer (if applicable, e.g., on cloud providers):

      kubectl get svc -n kong kong-proxy

      Use the EXTERNAL-IP.

    • Using Port Forward:

      kubectl port-forward -n kong service/kong-proxy 8080:80

      You can then access the inventory service endpoints through the gateway, typically prefixed (e.g., http://localhost:8080/inventory/...). Check the Kong configuration (k8s-manifests/kong-gateway/kong-config.yaml) for the exact routing rules.

8. Environment Variables

The service requires the following environment variables:

  • DATABASE_URL: Connection string for the PostgreSQL database.
    • Example: postgresql://user:password@host:port/database?schema=public
  • RABBITMQ_URL: Connection string for the RabbitMQ instance.
    • Example: amqp://user:password@host:port
  • PORT: The port the service will listen on.
    • Default: 3002

These are typically configured via Kubernetes Secrets/ConfigMaps during deployment or a .env file for local development.

9. Sample Data / Testing

A script is provided to add sample book data to the inventory:

  1. Ensure the Inventory Service is running (either locally or in Kubernetes and accessible).

  2. Run the script:

    ./hack/add_books.sh

    Note: You might need to adjust the script's target URL (http://localhost:3002 by default) depending on how you are running/accessing the service.

10. Cleanup

  • Local Docker Compose:

    # In the inventory-service directory
    docker-compose down -v # Stops and removes containers, networks, and volumes
  • Kubernetes:

    • Delete Inventory Service only:

      ```bash
      # In the k8s-manifests/inventory-service directory
      kubectl delete -f postgres-inventory.yaml
      kubectl delete -f inventory-service-migrations.yaml # If applicable
      kubectl delete -f inventory-service.yaml
      # Or use the deploy script with a delete flag if available
      ```
      
    • Delete Entire NovelNest Application:

      ```bash
      # In the k8s-manifests directory
      # You might need reverse deployment scripts or delete resources manually/by namespace
      kubectl delete -f inventory-service/ # Deletes inventory resources
      kubectl delete -f catalog-service/ # Deletes catalog resources
      # ... delete other services ...
      kubectl delete -f rabbitmq.yaml
      kubectl delete -f kong-gateway/ # Deletes Kong resources
      # Or delete the namespace if everything was deployed into one
      # kubectl delete namespace <your-namespace>
      ```
      
    • Delete kind cluster:

      ```bash
      kind delete cluster
      ```
      

11. Contributing

Contributions are welcome! Please follow standard Git workflow practices (fork, branch, pull request). Ensure code is linted and tests pass (if applicable). (Further contribution guidelines TBD).

12. License

This project is licensed under the MIT License (or specify the actual license if one exists).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published