move database configs to .env

This commit is contained in:
João Geonizeli
2022-07-10 15:39:51 -03:00
parent af0bc0a082
commit c3793ee661
4 changed files with 26 additions and 16 deletions

View File

@@ -2,4 +2,7 @@ ENV=development
PORT=5000 PORT=5000
SECRET=aE8efkEP8+V/ibEQl8IKbw== SECRET=aE8efkEP8+V/ibEQl8IKbw==
DB_HOST=localhost
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=todoListDev DB_NAME=todoListDev

View File

@@ -2,4 +2,7 @@ ENV=test
PORT=5000 PORT=5000
SECRET=aE8efkEP8+V/ibEQl8IKbw== SECRET=aE8efkEP8+V/ibEQl8IKbw==
DB_HOST=localhost
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=todoListTest DB_NAME=todoListTest

3
server/src/env.d.ts vendored
View File

@@ -1,5 +1,8 @@
declare namespace NodeJS { declare namespace NodeJS {
interface ProcessEnv { interface ProcessEnv {
DB_HOST?: string;
DB_USER?: string;
DB_PASSWORD?: string;
DB_NAME?: string; DB_NAME?: string;
NODE_ENV?: 'test' | 'development' | 'production'; NODE_ENV?: 'test' | 'development' | 'production';
} }

View File

@@ -1,21 +1,22 @@
import * as dotenv from 'dotenv'; import * as dotenv from "dotenv";
import "reflect-metadata"; import "reflect-metadata";
import { DataSource } from "typeorm"; import { DataSource } from "typeorm";
import { Project } from "../entity/project.entity"; import { Project } from "../entity/project.entity";
import { Task } from '../entity/task.entity'; import { Task } from "../entity/task.entity";
import { User } from "../entity/user.entity"; import { User } from "../entity/user.entity";
dotenv.config(); dotenv.config({
path: process.env.NODE_ENV === "test" ? ".env.test" : ".env",
});
export const AppDataSource = new DataSource({ export const AppDataSource = new DataSource({
type: "postgres", type: "postgres",
host: "localhost", host: process.env.DB_HOST,
port: 5432, port: 5432,
username: "joao", username: process.env.DB_USER,
database: process.env.DB_NAME, password: process.env.DB_PASSWORD,
synchronize: true, database: process.env.DB_NAME,
logging: false, synchronize: true,
entities: [User, Project, Task], logging: false,
migrations: [], entities: [User, Project, Task],
subscribers: [], });
})