Skip to content

andersonRocha091/multidatasource-api-scaffold

Repository files navigation

Multi data-source API Scaffold

This project it's an backbone for API development using multi-data source applying strategy pattern. It was built in NodeJS 12.8, and it comes with Hapi.js, jwt authentication, base unit test, pm2 monitoring, and the all infra-structure already dockerized for development enviroment tests.

Pre-requirements

  • Docker version 19.03 or above
  • Docker-machine version 0.16.0, or above
  • Docker-compose version 1.26.0, or above
  • Node version 12.8, or above - Node Donwload
  • NPM version 6.14.4, or above- Npm Donwload

Installing

Clone this project in you machine using the command below:

	git clone https://github.com/andersonRocha091/multidatasource-api-scaffold.git

Go to the project folder at your terminal:

	cd multidatasource-api-scaffold

Create a .env file with the following params for setting your environment:

MONGO_HOST=<mongo host>
MONGO_PORT=<mongo port>
MONGO_INITDB_DATABASE=<mongo database>
MONGO_INITDB_ROOT_USERNAME=<rootunsername>
MONGO_INITDB_ROOT_PASSWORD=<rootpassword>
DATABASE_USER=<user>
DATABASE_PASSWORD=<password>
DATABASE_NAME=<databasename>

Inside the aplication folder src/config, change the params in .env.dev, and .env.prod :

JWT_KEY=<YOUR_JWT_HASH_KEY>
PORT=<DESIRED_PORT>
SALT_PWD=<DESIRED_SALT>
MONGODB_URL=mongodb://<user>:<password>@<mongo host>:<mongo port>/<databasename>
POSTGRES_URL=postgres://<user>:<password>@<host | localhost(dev)>/<database> 

In terminal, inside multidatasource-api-scaffold start the local infrastructure:

docker-compose up -d --build

Finally, install the dependencies:

npm i

Execution

After setting up the .env's files you can run the api in production or in development mode.

 npm run (dev|prod)

you would be able to access the aplication endpoints by accessing localhost:port/route

OBS: As there's not yet an routine to add new users (sorry guys it will come soon), in dev mode you should be able to access the adminer panel listening on localhost:8080, and add an user at user's table (I suggest add a username: LexLuthor and password:123)

OBS2: There's also a mongoClient available for local mongodb management by default set in localhost:3000. (Feel free to use compass, or anything else which better suit for you)

Funcionalidades

Production version running at: https://heroes-api-scaffold.herokuapp.com

Available endpoints:

  • POST /login - params {username, password} (returns a token for accessing api endpoints)
  • POST /heroes - header{Authorization:} body:{name:, power:} (insert new hero at mongo's database)
  • GET /heroes - header{Authorization:} (retrieves all heroes)
  • GET /heroes?skip=&limit= header{Authorization:} (heroes with pagination)
  • GET /heroes?skip=&limit=&name= header{Authorization:} (heroes by name)
  • PATCH /heroes/:id header{Authorization:} body:{field1:value1, field2:value2, ...} (Update a hero property)
  • DELETE /heroes/:id header{Authorization:} (delete a hero)

Testing

The API was build using TDD and it is fully covered by unit tests. In order to run the TDD tests, just use the following command:

npm run test

Author

  • Anderson Souza Rocha - Full-stack developer - Github

License

MIT License (MIT)


Author ❤ Anderson Souza Rocha

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published