From 1a2f165d0ae565cd57ef93c6459b3f5da03c14a2 Mon Sep 17 00:00:00 2001 From: Javier Aviles Date: Fri, 1 Jun 2018 19:57:59 +0200 Subject: [PATCH] Install pg-connection-string to parse db options from env variable (adpating to Heroku) --- .example.env | 4 +++- package-lock.json | 13 +++++++++---- package.json | 1 + src/server.ts | 22 +++++++++++++--------- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/.example.env b/.example.env index 0478ce6..ee1fa94 100644 --- a/.example.env +++ b/.example.env @@ -1,3 +1,5 @@ NODE_PORT=3000 NODE_ENV=development -JWT_SECRET=your-secret-whatever \ No newline at end of file +JWT_SECRET=your-secret-whatever +DATABASE_URL=postgres://user:pass@localhost:5432/apidb +TYPEORM_DRIVER_TYPE=postgres \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 9766f00..f4a209c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-typescript-koa-rest", - "version": "1.1.2", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3034,6 +3034,11 @@ "semver": "4.3.2" }, "dependencies": { + "pg-connection-string": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz", + "integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=" + }, "semver": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz", @@ -3042,9 +3047,9 @@ } }, "pg-connection-string": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz", - "integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=" + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.0.0.tgz", + "integrity": "sha1-Pu/lmX4G2Ugh5NUC5CtqHHP434I=" }, "pg-pool": { "version": "2.0.3", diff --git a/package.json b/package.json index f0c295f..cc1f207 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "koa-jwt": "^3.3.2", "koa-router": "^7.4.0", "pg": "^7.4.3", + "pg-connection-string": "^2.0.0", "reflect-metadata": "^0.1.12", "typeorm": "^0.2.6", "winston": "^2.4.2" diff --git a/src/server.ts b/src/server.ts index ee0de0a..951211e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,22 +6,29 @@ import * as winston from 'winston'; import * as dotenv from 'dotenv'; import { createConnection } from 'typeorm'; import 'reflect-metadata'; +import * as PostgressConnectionStringParser from 'pg-connection-string'; import { User } from './entity/user'; import { logger } from './logging'; import { config } from './config'; import { router } from './routes'; +// Load environment variables from .env file, where API keys and passwords are configured +dotenv.config({ path: '.env' }); + +// Get DB connection options from env variable +const connectionOptions = PostgressConnectionStringParser.parse(process.env.DATABASE_URL); + // create connection with database // note that its not active database connection // TypeORM creates you connection pull to uses connections from pull on your requests createConnection({ - type: 'postgres', - host: 'localhost', - port: 5432, - username: 'user', - password: 'pass', - database: 'apidb', + type: process.env.TYPEORM_DRIVER_TYPE, + host: connectionOptions.host, + port: connectionOptions.port, + username: connectionOptions.user, + password: connectionOptions.password, + database: connectionOptions.database, synchronize: true, logging: false, entities: [ @@ -31,9 +38,6 @@ createConnection({ const app = new Koa(); - // Load environment variables from .env file, where API keys and passwords are configured - dotenv.config({ path: '.env' }); - // Provides important security headers to make your app more secure app.use(helmet());