From faebdc41c5720f6de4b8580285ab90602f50bd8f Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Thu, 11 Jul 2019 17:06:27 +0100 Subject: [PATCH] refactoring db connection --- JavaScript/README.md | 2 +- JavaScript/db/index.js | 11 ++--------- JavaScript/index.js | 2 +- README.md | 2 +- TypeScript/README.md | 4 +--- TypeScript/db/index.ts | 12 ++---------- TypeScript/tsconfig.json | 3 ++- db-config.json | 6 ++++++ package-lock.json | 2 +- 9 files changed, 17 insertions(+), 27 deletions(-) create mode 100644 db-config.json diff --git a/JavaScript/README.md b/JavaScript/README.md index 3122c3a..4beeb43 100644 --- a/JavaScript/README.md +++ b/JavaScript/README.md @@ -8,7 +8,7 @@ This implementation uses ES6 syntax, and requires Node.JS 4.5 or later. ### Installation -* Set up an empty test database, according to the connection details in [JavaScript/db/index.js](https://github.com/vitaly-t/pg-promise-demo/blob/master/JavaScript/db/index.js#L29), +* Set up an empty test database, according to the connection details in [db-config.json](https://github.com/vitaly-t/pg-promise-demo/blob/master/db-config.json), which you can also change to fit your test environment. * Install Node.js dependencies, by running `npm install` from the project's root folder. diff --git a/JavaScript/db/index.js b/JavaScript/db/index.js index 1d98f73..27ae368 100644 --- a/JavaScript/db/index.js +++ b/JavaScript/db/index.js @@ -3,6 +3,7 @@ // Bluebird is the best promise library available today, // and is the one recommended here: const promise = require('bluebird'); +const dbConfig = require('../../db-config.json'); const repos = require('./repos'); // loading all repositories @@ -25,19 +26,11 @@ const initOptions = { } }; -// Database connection parameters: -const config = { - host: 'localhost', - port: 5432, - database: 'pg-promise-demo', - user: 'postgres' -}; - // Load and initialize pg-promise: const pgp = require('pg-promise')(initOptions); // Create the database instance: -const db = pgp(config); +const db = pgp(dbConfig); // Load and initialize optional diagnostics: const diagnostics = require('./diagnostics'); diff --git a/JavaScript/index.js b/JavaScript/index.js index 6c2b14a..a219e18 100644 --- a/JavaScript/index.js +++ b/JavaScript/index.js @@ -106,7 +106,7 @@ function GET(url, handler) { }); } -const port = 3000; +const port = 5000; app.listen(port, () => { console.log('\nReady for GET requests on http://localhost:' + port); diff --git a/README.md b/README.md index 3831ae9..c5dc8f7 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ The demo focuses on the following: The demo includes two separate implementations, with identical functionality: * [ES6 JavaScript implementation](https://github.com/vitaly-t/pg-promise-demo/tree/master/JavaScript) -* [TypeScript 2.x/3.x implementation](https://github.com/vitaly-t/pg-promise-demo/tree/master/TypeScript) +* [TypeScript 3.x implementation](https://github.com/vitaly-t/pg-promise-demo/tree/master/TypeScript) ### Installing & Running diff --git a/TypeScript/README.md b/TypeScript/README.md index 1e09f02..2958a13 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -7,7 +7,7 @@ ### Installation -* Set up an empty test database, according to the connection details in [TypeScript/db/index.ts](https://github.com/vitaly-t/pg-promise-demo/blob/master/TypeScript/db/index.ts#L30), +* Set up an empty test database, according to the connection details in [db-config.json](https://github.com/vitaly-t/pg-promise-demo/blob/master/db-config.json), which you can also change to fit your test environment. * Install Node.js dependencies, by running `npm install` from the project's root folder. @@ -15,5 +15,3 @@ * Navigate into folder `TypeScript`, and run `tsc` there to generate all `.js` files. * Run the application with `node index.js` command. - -[Typings]:https://github.com/typings/typings diff --git a/TypeScript/db/index.ts b/TypeScript/db/index.ts index eeb669b..a0f765d 100644 --- a/TypeScript/db/index.ts +++ b/TypeScript/db/index.ts @@ -1,5 +1,6 @@ // Bluebird is the best promise library available today, and is the one recommended here: import * as promise from 'bluebird'; +import * as dbConfig from '../../db-config.json'; import {IMain, IDatabase, IOptions} from 'pg-promise'; import {IExtensions, UsersRepository, ProductsRepository} from './repos'; @@ -23,15 +24,6 @@ const initOptions: IOptions = { obj.users = new UsersRepository(obj, pgp); obj.products = new ProductsRepository(obj, pgp); } - -}; - -// Database connection parameters: -const config = { - host: 'localhost', - port: 5432, - database: 'pg-promise-demo', - user: 'postgres' }; // Loading and initializing pg-promise: @@ -40,7 +32,7 @@ import * as pgPromise from 'pg-promise'; const pgp: IMain = pgPromise(initOptions); // Create the database instance with extensions: -const db = & IExtensions>pgp(config); +const db = & IExtensions>pgp(dbConfig); // Load and initialize optional diagnostics: import diagnostics = require('./diagnostics'); diff --git a/TypeScript/tsconfig.json b/TypeScript/tsconfig.json index 468b45a..e706403 100644 --- a/TypeScript/tsconfig.json +++ b/TypeScript/tsconfig.json @@ -1,7 +1,8 @@ { "compilerOptions": { "module": "commonjs", - "target": "es6" + "target": "es6", + "resolveJsonModule": true }, "exclude": [ "node_modules" diff --git a/db-config.json b/db-config.json new file mode 100644 index 0000000..20a2191 --- /dev/null +++ b/db-config.json @@ -0,0 +1,6 @@ +{ + "host": "localhost", + "port": 5432, + "database": "pg-promise-demo", + "user": "postgres" +} diff --git a/package-lock.json b/package-lock.json index 0375279..34c8aac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "pg-promise-demo", - "version": "0.8.5", + "version": "0.9.0", "lockfileVersion": 1, "requires": true, "dependencies": {