Skip to content

Commit

Permalink
Merge pull request #2 from brandsExclusive/pass-new-env-variable
Browse files Browse the repository at this point in the history
Pass a Custom database connection path
  • Loading branch information
saidur2 authored Jul 4, 2017
2 parents f839752 + aa2ca5e commit 3c56ea8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ leQuel Library

leQuel provides a simple API to connect to and query postgres database.

### Requires
### Default database path
env.DATABASE_URL
```
DATABASE_URL=postgresql://@localhost/my_db
```
### For custom database path
env.MY_DATABASE_URL
```
MY_DATABASE_URL=postgresql://@localhost/my_db_test
```

```javascript
const lequel = require('lib-lequel');
lequel.setDatabaseUrl(process.env.MY_DATABASE_URL)
```

### Examples
```javascript
Expand All @@ -22,5 +31,3 @@ query.paginate(5, 5);

let result = lequel.getRows(...query.finalize());
```


2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib');
module.exports = require('./lib');
13 changes: 9 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const Pool = require("pg-pool"),
url = require("url");

if (!process.env.DATABASE_URL) {
throw new Error("Environment variable DATABASE_URL is not set");
let databaseUrl = process.env.DATABASE_URL

if (!databaseUrl) {
throw new Error("Environment variable is not set");
}
const params = url.parse(process.env.DATABASE_URL);
module.exports.setDatabaseUrl = (url) => databaseUrl = url
module.exports.getDatabaseUrl = () => databaseUrl

const params = url.parse(databaseUrl);
let ssl = (process.env.NODE_ENV === 'production'),
auth = params.auth.split(':'),
config = {
Expand Down Expand Up @@ -44,4 +49,4 @@ module.exports.getCount = (text, values) => {
};

module.exports.createGeneric = require('./createGeneric');
module.exports.queryBuilder = require('./queryBuilder');
module.exports.queryBuilder = require('./queryBuilder');
19 changes: 19 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const expect = require('chai').expect

describe('it can load', () => {
afterEach(() => {
process.env.DATABASE_URL = null
})
it('default env variable as database connection', () => {
process.env.DATABASE_URL = 'postgresql://@localhost/my_db';
const lequel = require('../lib/')
expect(lequel.getDatabaseUrl()).to.eql('postgresql://@localhost/my_db')
})

it('custom env variable as database connection', () => {
process.env.MY_DATABASE_URL = 'postgresql://@localhost/my_db_test';
const lequel = require('../lib/')
lequel.setDatabaseUrl(process.env.MY_DATABASE_URL)
expect(lequel.getDatabaseUrl()).to.eql('postgresql://@localhost/my_db_test')
})
})

0 comments on commit 3c56ea8

Please sign in to comment.