Skip to content

Commit

Permalink
refactoring db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 11, 2019
1 parent 7c67b56 commit faebdc4
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 27 deletions.
2 changes: 1 addition & 1 deletion JavaScript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
11 changes: 2 additions & 9 deletions JavaScript/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions TypeScript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

### 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.

### Starting

* 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
12 changes: 2 additions & 10 deletions TypeScript/db/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,15 +24,6 @@ const initOptions: IOptions<IExtensions> = {
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:
Expand All @@ -40,7 +32,7 @@ import * as pgPromise from 'pg-promise';
const pgp: IMain = pgPromise(initOptions);

// Create the database instance with extensions:
const db = <IDatabase<IExtensions> & IExtensions>pgp(config);
const db = <IDatabase<IExtensions> & IExtensions>pgp(dbConfig);

// Load and initialize optional diagnostics:
import diagnostics = require('./diagnostics');
Expand Down
3 changes: 2 additions & 1 deletion TypeScript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6"
"target": "es6",
"resolveJsonModule": true
},
"exclude": [
"node_modules"
Expand Down
6 changes: 6 additions & 0 deletions db-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"host": "localhost",
"port": 5432,
"database": "pg-promise-demo",
"user": "postgres"
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit faebdc4

Please sign in to comment.