From b6f94825cf5fafd8b39fc555ece8911c6980d05b Mon Sep 17 00:00:00 2001 From: Dmitry Sviridenko Date: Sun, 16 Jul 2023 16:30:37 +0300 Subject: [PATCH] feat: Improve configs storing --- .env.template | 11 +++++++++++ .eslintignore | 2 +- .gitignore | 2 ++ .sequelizerc | 2 +- config/db/config.js | 1 + config/default.js | 26 ++++++++++++++++++++++++++ config/default.json | 8 -------- config/development.js | 5 +++++ config/development.json | 21 --------------------- config/production.js | 5 +++++ config/production.json | 21 --------------------- config/test.js | 5 +++++ config/test.json | 21 --------------------- jest.config.ts | 2 +- src/app.ts | 2 +- src/models/index.ts | 2 +- 16 files changed, 60 insertions(+), 76 deletions(-) create mode 100644 config/default.js delete mode 100644 config/default.json create mode 100644 config/development.js delete mode 100644 config/development.json create mode 100644 config/production.js delete mode 100644 config/production.json create mode 100644 config/test.js delete mode 100644 config/test.json diff --git a/.env.template b/.env.template index e3645f22..13b756ba 100644 --- a/.env.template +++ b/.env.template @@ -1,2 +1,13 @@ # used in migrations to fetch actual currencies rates API_LAYER_API_KEY= + +APPLICATION_HOST= +APPLICATION_PORT= +APPLICATION_JWT_SECRET= +APPLICATION_DB_HOST= +APPLICATION_DB_USERNAME= +APPLICATION_DB_PASSWORD= +APPLICATION_DB_DATABASE= +APPLICATION_DB_PORT= +APPLICATION_DB_DIALECT= +APPLICATION_REDIS_HOST= diff --git a/.eslintignore b/.eslintignore index fa9bd52d..24c38ec9 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,4 @@ src/seeders/**/*.js src/migrations/**/*.js .eslintrc.js -config/db/config.js +config/** diff --git a/.gitignore b/.gitignore index c3154412..f02f89e0 100644 --- a/.gitignore +++ b/.gitignore @@ -77,6 +77,8 @@ typings/ # dotenv environment variables file .env .env.test +.env.development +.env.production # parcel-bundler cache (https://parceljs.org/) .cache diff --git a/.sequelizerc b/.sequelizerc index f39a36bd..7376f30d 100644 --- a/.sequelizerc +++ b/.sequelizerc @@ -1,5 +1,5 @@ const path = require('path'); -require('dotenv').config(); +require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }) module.exports = { "config": path.resolve('./config/db', 'config.js'), diff --git a/config/db/config.js b/config/db/config.js index c718271a..706a53bf 100644 --- a/config/db/config.js +++ b/config/db/config.js @@ -1,3 +1,4 @@ +require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }); const config = require('config').get('db'); const options = { diff --git a/config/default.js b/config/default.js new file mode 100644 index 00000000..eea66cd5 --- /dev/null +++ b/config/default.js @@ -0,0 +1,26 @@ +// Default file that being merged with others env-related files + +module.exports = { + name: 'budget-tracker-be', + host: process.env.APPLICATION_HOST, + port: process.env.APPLICATION_PORT, + apiPrefix: '/api/v1', + jwtSecret: process.env.APPLICATION_JWT_SECRET, + db: { + host: process.env.APPLICATION_DB_HOST, + username: process.env.APPLICATION_DB_USERNAME, + password: process.env.APPLICATION_DB_PASSWORD, + database: process.env.APPLICATION_DB_DATABASE, + port: process.env.APPLICATION_DB_PORT, + dialect: process.env.APPLICATION_DB_DIALECT, + logging: true, + }, + redis: { + host: process.env.APPLICATION_REDIS_HOST, + }, + bankIntegrations: { + monobank: { + apiEndpoint: 'https://api.monobank.ua' + } + }, +} diff --git a/config/default.json b/config/default.json deleted file mode 100644 index f222d7cb..00000000 --- a/config/default.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "budget-tracker-be", - "bankIntegrations": { - "monobank": { - "apiEndpoint": "https://api.monobank.ua" - } - } -} diff --git a/config/development.js b/config/development.js new file mode 100644 index 00000000..238eb4e1 --- /dev/null +++ b/config/development.js @@ -0,0 +1,5 @@ +module.exports = { + env: 'development', + envShort: 'dev', + hostWebhooksCallback: 'http://d8d75e719def.ngrok.io', +} diff --git a/config/development.json b/config/development.json deleted file mode 100644 index 0a1d6054..00000000 --- a/config/development.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "env": "development", - "envShort": "dev", - "host": "127.0.0.1", - "port": "8081", - "apiPrefix": "/api/v1", - "jwtSecret": "dev-jwt", - "hostWebhooksCallback": "http://d8d75e719def.ngrok.io", - "db": { - "host": "127.0.0.1", - "username": "letehaha", - "password": "password", - "database": "budget-tracker", - "port": "5432", - "dialect": "postgres", - "logging": true - }, - "redis": { - "host": "127.0.0.1" - } -} diff --git a/config/production.js b/config/production.js new file mode 100644 index 00000000..f7eeede3 --- /dev/null +++ b/config/production.js @@ -0,0 +1,5 @@ +module.exports = { + env: 'production', + envShort: 'prod', + hostWebhooksCallback: 'http://d8d75e719def.ngrok.io', +} diff --git a/config/production.json b/config/production.json deleted file mode 100644 index e3558d94..00000000 --- a/config/production.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "env": "production", - "envShort": "prod", - "host": "127.0.0.1", - "port": "8081", - "apiPrefix": "/api/v1", - "jwtSecret": "prod-jwt", - "hostWebhooksCallback": "http://d8d75e719def.ngrok.io", - "db": { - "host": "db", - "username": "letehaha", - "password": "password", - "database": "budget-tracker", - "port": "5432", - "dialect": "postgres", - "logging": false - }, - "redis": { - "host": "redis" - } -} diff --git a/config/test.js b/config/test.js new file mode 100644 index 00000000..52d4add1 --- /dev/null +++ b/config/test.js @@ -0,0 +1,5 @@ +module.exports = { + env: 'test', + envShort: 'test', + hostWebhooksCallback: 'http://d8d75e719def.ngrok.io', +} diff --git a/config/test.json b/config/test.json deleted file mode 100644 index 36ead198..00000000 --- a/config/test.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "env": "development", - "envShort": "dev", - "host": "127.0.0.1", - "port": "8081", - "apiPrefix": "/api/v1", - "jwtSecret": "dev-jwt", - "hostWebhooksCallback": "http://d8d75e719def.ngrok.io", - "db": { - "host": "127.0.0.1", - "username": "letehaha", - "password": "password", - "database": "budget-tracker_test", - "port": "5432", - "dialect": "postgres", - "logging": false - }, - "redis": { - "host": "127.0.0.1" - } -} diff --git a/jest.config.ts b/jest.config.ts index 20cb5d9c..6ae872f7 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -3,7 +3,7 @@ export default { preset: 'ts-jest', testEnvironment: 'node', - testMatch: ['**/?(*.)+(spec|test|e2e).[jt]s?(x)'], + testMatch: ['**/?(*.)+(unit|spec|e2e).[jt]s?(x)'], globals: { 'ts-jest': { tsconfig: 'tsconfig.json', diff --git a/src/app.ts b/src/app.ts index 240cfec5..93205b25 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,7 @@ import dotenv from "dotenv"; import 'module-alias/register'; -dotenv.config(); +dotenv.config({ path: `.env.${process.env.NODE_ENV}` }); import config from 'config'; import express, { Request } from 'express'; import cors from 'cors'; diff --git a/src/models/index.ts b/src/models/index.ts index 59cb8f0d..62ca313d 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -15,7 +15,7 @@ const sequelize = new Sequelize({ models: [__dirname + '/**/*.model.ts'], }); -if (process.env.NODE_ENV === 'development') { +if (['development', 'test'].includes(process.env.NODE_ENV)) { console.log('DBConfig', DBConfig); }