Skip to content

Commit

Permalink
Add SSL env option
Browse files Browse the repository at this point in the history
  • Loading branch information
braemJef committed Feb 14, 2022
1 parent 2f914e0 commit 4a7d6b7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .env.local
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
SECRET=SuP3r-s3Cr3T
DB_URL=postgres://userdev:passdev@localhost:5432/game
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=userdev
DB_PASSWORD=passdev
DB_DATABASE=game
DB_URL=postgres://userdev:passdev@localhost:5432/game
DB_SSL=false
TZ=UTC
PORT=3001
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@types/node": "^14.14.7",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"dotenv": "^8.2.0",
"dotenv": "^16.0.0",
"eslint": "^7.13.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-import": "^2.22.1",
Expand All @@ -58,4 +58,4 @@
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
}
}
19 changes: 11 additions & 8 deletions src/global/Database.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { createConnection } from 'typeorm';

const HOST = process.env.DB_HOST || 'localhost';
const PORT = process.env.DB_PORT || '5432';
const USERNAME = process.env.DB_USERNAME || 'userdev';
const PASSWORD = process.env.DB_PASSWORD || 'passdev';
const DATABASE = process.env.DB_DATABASE || 'game';
const URL =
process.env.DB_URL || 'postgres://userdev:passdev@localhost:5432/game';
const HOST = process.env.DB_HOST;
const PORT = process.env.DB_PORT;
const USERNAME = process.env.DB_USERNAME;
const PASSWORD = process.env.DB_PASSWORD;
const DATABASE = process.env.DB_DATABASE;
const URL = process.env.DB_URL;
const SSL = process.env.DB_SSL;

const DEV = process.env.NODE_ENV === 'development';

export default async function initializeDB() {
if (URL) {
console.log('URL: ', URL);
if (URL && URL.length > 0) {
await createConnection({
type: 'postgres',
url: URL,
ssl: SSL === 'true',
entities: [DEV ? 'src/**/*.ts' : 'dist/src/**/*.js'],
migrations: [DEV ? 'migration/**/*.ts' : 'dist/migration/**/*.js'],
migrationsRun: true,
Expand All @@ -29,6 +31,7 @@ export default async function initializeDB() {
username: USERNAME,
password: PASSWORD,
database: DATABASE,
ssl: SSL === 'true',
entities: [DEV ? 'src/**/*.ts' : 'dist/src/**/*.js'],
migrations: [DEV ? 'migration/**/*.ts' : 'dist/migration/**/*.js'],
migrationsRun: true,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,11 @@ dot-prop@^5.2.0:
dependencies:
is-obj "^2.0.0"

dotenv@^16.0.0:
version "16.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.0.tgz#c619001253be89ebb638d027b609c75c26e47411"
integrity sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==

dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
Expand Down

0 comments on commit 4a7d6b7

Please sign in to comment.