Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: node-mongodb poc #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions POCs/node-mongodb/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules/
.git/
.vscode/
coverage/

.dockerignore
Dockerfile

.gitignore
.eslintrc
.editorconfig
12 changes: 12 additions & 0 deletions POCs/node-mongodb/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
72 changes: 72 additions & 0 deletions POCs/node-mongodb/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["tsconfig.json"]
},
"plugins": ["@typescript-eslint", "prettier", "unicorn", "import"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:import/recommended",
"plugin:import/typescript"
],
"rules": {
"sort-imports": [
"error",
{
"ignoreCase": false,
"ignoreDeclarationSort": true,
"ignoreMemberSort": false,
"memberSyntaxSortOrder": ["none", "all", "multiple", "single"],
"allowSeparatedGroups": true
}
],
"no-console": "warn",
"no-empty-function": "error",
"prettier/prettier": "error",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-require-imports": "warn",
"@typescript-eslint/no-unsafe-assignment": "error",
"@typescript-eslint/no-unsafe-return": "error",
"unicorn/filename-case": [
"error",
{
"case": "kebabCase"
}
],
"@typescript-eslint/no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
],
"import/no-unresolved": "error",
"import/no-named-as-default-member": "off",
"import/no-named-as-default": "off",
"import/order": [
"error",
{
"groups": ["builtin", "external", "internal", ["sibling", "parent"], "index", "unknown"],
"newlines-between": "always",
"alphabetize": {
"order": "asc",
"caseInsensitive": true
}
}
]
},
"settings": {
"import/resolver": {
"typescript": {
"project": "./tsconfig.json"
}
}
}
}
27 changes: 27 additions & 0 deletions POCs/node-mongodb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Logs #
/logs
*.log
*.log*

# Node files #
node_modules/
npm-debug.log
yarn-error.log
yarn.lock
.env

# Typing #
typings/

# Dist #
dist/
tsconfig.build.json

# APPLICATION #
/src/config/.env*
!/src/config/.env.example
dist/
api-docs/
!src/public/**/*
test/**/*.js
coverage/
6 changes: 6 additions & 0 deletions POCs/node-mongodb/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
printWidth: 120,
singleQuote: true,
trailingComma: 'es5',
semi: false
};
7 changes: 7 additions & 0 deletions POCs/node-mongodb/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:16.13-bullseye-slim AS builder
RUN apt update && apt install -y make
WORKDIR /usr/src/app
COPY . .
RUN apt-get update && apt-get install make
RUN make clean-setup
ENTRYPOINT [ "src/config/entrypoint.sh" ]
126 changes: 126 additions & 0 deletions POCs/node-mongodb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!make

help:
@echo "Usage: make [run|build|clean-setup|start|start-dev|start-dev-watch|setup-dev|clean-setup-dev|test|lint|format-code|migrate|coverage-test]"
@echo ""
@echo "Usage:"
@echo " make setup-dev Setup the project including development dependencies"
@echo " make clean-setup-dev Setup the project including development dependencies, cleaning all previous installed dependencies"
@echo " make clean-setup Setup the project, cleaning all previous installed dependencies"
@echo " make build Compile the project typescript and generate the output bundles"
@echo " make start <ENV=production> Execute the builded code"
@echo " make start-dev Start the server in development mode"
@echo " make docker-start-web Start the server on a docker container"
@echo " make docker-stop-web Stop the server docker container"
@echo " make docker-start-db Start a database instance on a docker container"
@echo " make docker-stop-db Stop the database docker container"
@echo " make migrate-run <ENV=production> Run the migrations"
@echo " make migrate-revert <ENV=production> Revert the migrations"
@echo " make migrate-generate <ENV=production> <NAME=migration-name> Generate a new migration based on entities changes"
@echo " make lint Run the server linter"
@echo " make lint-fix Run the server linter with autofix flag activated"
@echo " make format-code Format the server code with prettier"
@echo " make test Run the server tests"
@echo " make coverage-test Run the server tests with code coverage"
@echo " make docker-start Start the server and database docker containers"
@echo " make docker-stop Stop the server and database docker containers"
@echo " make docker-build Build the docker containers"
@echo ""

# Default variables

ENV ?= production

define GET_FOLDER
ifeq (${ENV}, development)
ENV_FOLDER=src
else
ENV_FOLDER=dist
NODE_PATH=dist/
endif
endef

# Requirements

setup-dev:
npm install

clean-setup-dev:
npm ci

clean-setup:
npm ci --production


## Web

build:
npx tsc -p tsconfig-build.json

start:
NODE_ENV=$(ENV) NODE_PATH=dist/ node -r ts-node/register dist/app.js

start-dev:
NODE_ENV=development npx ts-node-dev -r tsconfig-paths/register --trace-warnings --inspect --respawn --transpile-only --ignore-watch node_modules src/app.ts

docker-start-web:
docker-compose up -d nodejs

docker-stop-web:
docker-compose stop nodejs


## Database

docker-start-db:
docker-compose up -d db

docker-stop-db:
docker-compose stop db

migrate-run:
$(eval $(call GET_FOLDER))
NODE_ENV=$(ENV) npx ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d ${ENV_FOLDER}/config/database migration:run

migrate-revert:
$(eval $(call GET_FOLDER))
NODE_ENV=$(ENV) npx ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d ${ENV_FOLDER}/config/database migration:revert

migrate-generate:
$(eval $(call GET_FOLDER))
NODE_ENV=$(ENV) npx ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d ${ENV_FOLDER}/config/database migration:generate $(NAME)


## Code lint and format

lint:
npx eslint src/

lint-fix:
npx eslint src/ --fix

format-code:
npx prettier --write src/**/*.{js,jsx,ts,tsx,json,md}


# Test

test:
npx jest --config=./src/jest.config.js

coverage-test:
npx jest --config=./src/jest.config.js --coverage


# Docker

docker-start:
docker-compose up

docker-stop:
docker-compose stop

docker-build:
docker-compose up --build

default: help
54 changes: 54 additions & 0 deletions POCs/node-mongodb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# node-typescript

![The Cheesecake Labs](https://images.sympla.com.br/5d11137d98ce3.png)

## Description

This is the backend application developed in NodeJS.

## Requirements

Make sure you have all this installed:

- `Docker`
- `node`
- `npm`

## Stack

This project uses the following stack:

- Node 16.x.x
- PostgreSQL
- Typescript
- ApolloServer
- Express
- TypeORM
- EsLint
- Jest

## Setup environment

To setup the environment, run the following commands:

```sh
cp ./src/config/.env.example ./src/config/.env.development
```

## How to run locally

To run the project locally, run the following commands:

```sh
make start-dev
```

## How to run with Docker

To build and run the project run the following commands:

**Obs.**: It will be necessary to change the `DATABASE_HOST` from `localhost` to `db` in `src/config/.env.development` file

```sh
docker-compose up --build
```
35 changes: 35 additions & 0 deletions POCs/node-mongodb/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: '3.7'

services:
nodejs:
container_name: nodejs
build:
context: ./
environment:
- NODE_ENV=development
- PORT=8000
- DEBUG=true
- PRIVATE_KEY="example-key"
- SOCKET_TIMEOUT=2000
- SENTRY_DSN="sentry-url"
volumes:
- './:/usr/src/app'
ports:
- 8000:8000
- 9229:9229
depends_on:
- db
db:
image: mongo:5.0
volumes:
- db_data:/data/db
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
MONGO_INITDB_DATABASE: digitalHealth

volumes:
db_data:
name: nodejs-typescript-db-data
Loading