Currency Exchange Rates API

This project is a NestJS application that provides an API for fetching and storing currency exchange rates. It uses TypeORM for database interactions, Jest for testing, and integrates with Sentry for error tracking.

Table of Contents

  1. Setup and Run
  2. Project Structure
  3. NestJS Project Structure and Router
  4. TypeORM and Jest
  5. Code Functionality
  6. File Descriptions

Setup and Run

Prerequisites

  • Node.js (v22)
  • npm
  • MySQL (or any other database supported by TypeORM)

Steps

  1. Clone the repository:
    git clone <repository-url>
    cd <repository-directory>
    
  2. Install dependencies:
    npm install
    
  3. Set up environment variables: Create a .env file in the root directory and add the following variables:
    DATABASE_HOST=localhost
    DATABASE_PORT=3306
    DATABASE_USERNAME=root
    DATABASE_PASSWORD=yourpassword
    DATABASE_NAME=currencies
    ANYAPI_KEY=your_api_key
    SENTRY_KEY=your_sentry_key
    
  4. Run database migrations:
    npm run typeorm migration:run
    
  5. Start the application:
    npm run start
    
  6. Run tests:
    npm run test
    

Project Structure

.
|-- README.md
|-- nest-cli.json
|-- package-lock.json
|-- package.json
|-- src
|   |-- app.controller.spec.ts
|   |-- app.controller.ts
|   |-- app.module.ts
|   |-- app.service.ts
|   |-- currency
|   |   |-- currency.controller.spec.ts
|   |   |-- currency.controller.ts
|   |   |-- currency.module.ts
|   |   |-- currency.service.spec.ts
|   |   `-- currency.service.ts
|   |-- db
|   |   |-- data-source.ts
|   |   |-- entities
|   |   |   `-- CurrencyRate.entity.ts
|   |   `-- migrations
|   |       `-- 1729369938238-currency-rates.ts
|   |-- instrument.ts
|   `-- main.ts
|-- test
|   |-- app.e2e-spec.ts
|   `-- currency.service.integration.spec.ts
|-- tsconfig.build.json
`-- tsconfig.json

NestJS Project Structure and Router

NestJS follows a modular architecture, which helps in organizing the code into cohesive blocks. Each module encapsulates related components like controllers, services, and entities.

  • Controllers handle incoming requests and return responses to the client.
  • Services contain the business logic and interact with the database.
  • Modules group related controllers and services together.

Project Structure

Root Directory

  • README.md: Contains the documentation for the project.
  • nest-cli.json: Configuration file for the Nest CLI.
  • package-lock.json: Automatically generated file that describes the exact tree that was generated by npm.
  • package.json: Contains metadata about the project and its dependencies.

src Directory

  • app.controller.spec.ts: Unit tests for the AppController.
  • app.controller.ts: Defines the main controller for the application.
  • app.module.ts: The root module of the application.
  • app.service.ts: Defines the main service for the application.

currency Directory

  • currency.controller.spec.ts: Unit tests for the CurrencyController.
  • currency.controller.ts: Defines the controller for currency-related operations.
  • currency.module.ts: Module for the currency feature.
  • currency.service.spec.ts: Unit tests for the CurrencyService.
  • currency.service.ts: Defines the service for currency-related operations.

db Directory

  • data-source.ts: Configuration for the TypeORM data source.
  • entities Directory
    • CurrencyRate.entity.ts: Defines the CurrencyRate entity for TypeORM.
  • migrations Directory
    • 1729369938238-currency-rates.ts: Migration file for the CurrencyRate entity.

Other Files

  • instrument.ts: Contains instrumentation code, possibly for Sentry.
  • main.ts: The entry point of the application.

test Directory

  • app.e2e-spec.ts: End-to-end tests for the application.
  • currency.service.integration.spec.ts: Integration tests for the CurrencyService.

Configuration Files

  • tsconfig.build.json: TypeScript configuration file for the build process.
  • tsconfig.json: TypeScript configuration file for the project.

Router

The router in NestJS is defined by the controllers. Each controller can have multiple routes that handle different HTTP methods (GET, POST, etc.).

TypeORM and Jest

TypeORM

TypeORM is an ORM (Object-Relational Mapper) that allows you to interact with your database using TypeScript classes and decorators. It supports various databases like MySQL, PostgreSQL, SQLite, etc.

Jest

Jest is a testing framework that allows you to write unit and integration tests for your application. It provides a simple API to mock dependencies and assert the behavior of your code.

Code Functionality

The application fetches currency exchange rates from an external API and stores them in a database. It provides endpoints to retrieve the stored rates and integrates with Sentry for error tracking.

File Descriptions

Root Directory

  • README.md: Contains the documentation for the project.
  • nest-cli.json: Configuration file for the Nest CLI.
  • package-lock.json: Automatically generated file that describes the exact tree that was generated by npm.
  • package.json: Contains metadata about the project and its dependencies.

src Directory

  • app.controller.spec.ts: Unit tests for the AppController.
  • app.controller.ts: Defines the main controller for the application.
  • app.module.ts: The root module of the application.
  • app.service.ts: Defines the main service for the application.

currency Directory

  • currency.controller.spec.ts: Unit tests for the CurrencyController.
  • currency.controller.ts: Defines the controller for currency-related operations.
  • currency.module.ts: Module for the currency feature.
  • currency.service.spec.ts: Unit tests for the CurrencyService.
  • currency.service.ts: Defines the service for currency-related operations.

db Directory

  • data-source.ts: Configuration for the TypeORM data source.
    • entities Directory
      • CurrencyRate.entity.ts: Defines the CurrencyRate entity for TypeORM.
    • migrations Directory
      • 1729369938238-currency-rates.ts: Migration file for the CurrencyRate entity.

Other Files

  • instrument.ts: Contains instrumentation code, possibly for Sentry.
  • main.ts: The entry point of the application.

test Directory

  • app.e2e-spec.ts: End-to-end tests for the application.
  • currency.service.integration.spec.ts: Integration tests for the CurrencyService.

Configuration Files

  • tsconfig.build.json: TypeScript configuration file for the build process.
  • tsconfig.json: TypeScript configuration file for the project.