- cars-db-api
This repository contains an implementation for a cars database API backend service. It is an interface for performing CRUD operations on a MongoDB database cars-db
with a collection cars
. It is composed of cars information. The tech stack used is the following:
- Express with Typescript
- Dotenv for config handling
- Pino as the logger
- Jest and Supertest for unit and integration tests
- Mongoose to interact with MongoDB
- AJV for request data validation
- apicache for caching
The type definitions and Mongoose models can be found under ./src/models
folder. For getting detailed information about models and endpoints, have a look at the Swagger file. It can be accessed in the development environment under the /api-docs
path.
The app is an API Restful composed of routes, controllers and model based on the product. In this case, a CRUD interface for a cars database with the possibility to be extended and escalated. It can be run as a microservice with several instances pointing to a Mongo DB cluster.
The structure hierarchy is important to understand and to be followed as it is part of its architecture. It is the following:
src
contains the app sources__tests__
contain the unit tests for the files, with.test.ts
extensioncontrollers
contain business logic that interacts with the Mongoose models performing CRUD operationsdb
contains the DB connectionmiddlewares
contain the ExceptionHandler and Authorization Handler middlewares plus config adding others such as Swagger and Helmetmodels
contain the Mongoose model plus typesconfig.ts
single point with all the app configuration variables extracted with dotenv and default valuesroutes.ts
contain the API endpoints with response, exceptions to throw based on controller logic and cache definitions. Can be split to follow the controllers schemeserver.ts
initialise the server and do configurations plus DB connectionvalidations
contain the validator logic and schemas
test
contains tests utils and mocks along with the integration testsintegration
are integration testsmocks.ts
contain mocks for Mongoose and other dependenciesutils.ts
group of utils to perform the unit tests
It is centralised by the exceptionHandler.ts
middleware where exceptions.ts
thrown are mapped to status code and response message error. Then the response and error logging is done. It is recommended to throw every error with this flow.
The following GitHub workflow is running in every push to main
branch:
build-and-test
in charge of build and testing the app. It generates the report coverage as well.
- TBD
- Node.JS version equal to or greater 14.16
- Yarn version equal to or greater 1.22
- clone the cars-db-api repository
- switch to ./cars-db-api repository folder
- run
yarn
for installing the dependencies
This is the recommended option as it is quick and safe to run the whole API plus DB.
It is prepared to create a Docker container with the Dockerfile app plus a Mongo DB instance running.
To build the app and copy ./dist
to Docker:
yarn build
To launch the app along with the Mongo DB instance:
docker-compose up
For running it locally, the requisite is to have a Mongo DB instance running. The Mongo DB URL can be defined as an env var:
DATABASE_URL='mongodb://localhost:27017/cars-db'
Create a .env
with the desired config to initiate the API:
API_KEY='123456'
DATABASE_URL='mongodb://localhost:27017/cars-db'
SWAGGER_ENABLED='true'
Run the following commands to execute the app for development. It will reload on file change:
yarn start:dev
To run the app built run:
yarn start
It will build and run the app
To build the app and create a ./dist
run:
yarn build
Use yarn co
to run Commitizen. It will help to make a more descriptive commit history and to respect the commit format.
yarn test:lint
yarn test:types
yarn test:unit
yarn test:integration
yarn test
In order to get coverage with unit and integration tests together, run:
yarn cover:report
yarn prettier:fix
yarn clean:installation
yarn clean:build