Backend Documentation
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
- Setup and Run
- Project Structure
- NestJS Project Structure and Router
- TypeORM and Jest
- Code Functionality
- File Descriptions
Setup and Run
Prerequisites
- Node.js (v22)
- npm
- MySQL (or any other database supported by TypeORM)
Steps
- Clone the repository:
git clone <repository-url> cd <repository-directory>
- Install dependencies:
npm install
- 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
- Run database migrations:
npm run typeorm migration:run
- Start the application:
npm run start
- 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.
- CurrencyRate.entity.ts: Defines the
- migrations Directory
- 1729369938238-currency-rates.ts: Migration file for the
CurrencyRate
entity.
- 1729369938238-currency-rates.ts: Migration file for the
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 theAppController
.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 theCurrencyController
.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 theCurrencyService
.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 theCurrencyRate
entity for TypeORM.
- migrations Directory
1729369938238-currency-rates.ts
: Migration file for theCurrencyRate
entity.
- entities Directory
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 theCurrencyService
.
Configuration Files
tsconfig.build.json
: TypeScript configuration file for the build process.tsconfig.json
: TypeScript configuration file for the project.