项目作者: jlimadev

项目描述 :
Multi-environment Application built with nodejs, using express, celebrate, mongoose, sequelize, jest, jwt-authentication and deployed on Heroku
高级语言: JavaScript
项目地址: git://github.com/jlimadev/nodejs-api-multidb.git
创建时间: 2021-03-07T19:42:19Z
项目社区:https://github.com/jlimadev/nodejs-api-multidb

开源协议:

下载


API with a multi-database strategy

Using Strategy Design Pattern to implement a multi-database project and mocha/chai to test.

The API was made with express and dynamic validation with celebrate.

To generate the swagger was used swagger-ui-express and swagger-jsdoc with OpenAPI v3.

And last but not least, the JWT token authentication. This authentication was built with jsonwebtoken to generate/validate the token. We heave one middleware to check the authentication on private routes.

For this project you can use Postgres or MongoDB, but if you want to add any other database, feel free to do it.

Strategy Design Pattern

In this repository we use the Strategy Design Pattern to deal with multi-database

  • For Postgres database we used the Sequelize
  • For MongoDB database we used Mongoose

Docker

Bellow the docker commands used to create it.

Postgres Database

  1. docker run \
  2. --name nodebr-postgres \
  3. -e POSTGRES_USER=root \
  4. -e POSTGRES_PASSWORD=root \
  5. -e POSTGRES_DB=heroes \
  6. -p 5432:5432 \
  7. -d \
  8. postgres

List containers: docker ps, then enter in bin/bash of container and run psql command inside container.

To run PSQL command inside container: LINUX/MAC: docker exec -it nodebr-postgres /bin/bash. WINDOWS: winpty docker exec -it nodebr-postgres //bin//bash

Adminer

  1. docker run \
  2. --name adminer-nodebr \
  3. -p 8080:8080 \
  4. --link nodebr-postgres:nodebr-postgres \
  5. - d\
  6. adminer

We can also use DBeaver or PGAdmin to access our postgres.
With postgres we must install dependencies bellow:

yarn add sequelize pg-hstore pg

MongoDB

  1. docker run \
  2. --name nodebr-mongodb \
  3. -e MONGO_INITDB_ROOT_USERNAME=root \
  4. -e MONGO_INITDB_ROOT_PASSWORD=root \
  5. -p 27017:27017 \
  6. - d \
  7. mongo

Create db and user via CLI

  1. docker exec -it nodebr-mongodb \
  2. mongo --host localhost -u root -p root --authenticationDatabase admin \
  3. --eval "db.getSiblingDB('heroes').createUser({user: 'jlimadev', pwd: 'secretpass', roles: [{role: 'readWrite', db: 'heroes'}]})"

MongoClient

  1. docker run \
  2. --name mongoclient-nodebr \
  3. -p 3000:3000 \
  4. --link nodebr-mongodb:nodebr-mongodb \
  5. -d \
  6. mongoclient/mongoclient

We can also use MongoDBCompass

Cloud Mongo DB (PROD)

PROD database with cloud.mongodb.com from mlabs.

Deployment

This App is deployed on Heroku.