https://github.com/fonini/dsn-parser
Parse and create database connection strings fluently.
https://github.com/fonini/dsn-parser
database dsn mysql parser pgsql
Last synced: 8 months ago
JSON representation
Parse and create database connection strings fluently.
- Host: GitHub
- URL: https://github.com/fonini/dsn-parser
- Owner: fonini
- Created: 2017-03-15T20:15:13.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-04-23T11:21:09.000Z (over 5 years ago)
- Last Synced: 2025-02-01T02:41:22.974Z (8 months ago)
- Topics: database, dsn, mysql, parser, pgsql
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 3
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
DSN Parser for Node
===================Parse and create database connection strings fluently.
## Use case
I've setup a node app on [Heroku](https://www.heroku.com) and added a PostgreSQL database. Heroku provides the database credentials
as an enviroment variable with the following form: `pgsql://user:pass@127.0.0.1:5432/my_db`.I needed to parse it to the following way, in order to use with [node-postgres](https://github.com/brianc/node-postgres) package:
````js
var config = new DSNParser(process.env.HEROKU_POSTGRESQL_COPPER_URL).getParts();var pool = new pg.Pool(config);
````## Installation
`npm install dsn-parser`
## Usage
### Parse Data Source Name
````js
var DSNParser = require('dsn-parser');var dsn = new DSNParser('pgsql://user:pass@127.0.0.1:5432/my_db?sslmode=verify-full&application_name=myapp');
dsn.getParts();/*
{
driver: 'pgsql',
user: 'user',
password: 'pass',
host: '127.0.0.1',
port: 5432,
database: 'my_db',
params: { sslmode: 'verify-full', application_name: 'myapp' }
} */dsn.get('database'); // my_db
dsn.get('host'); // 127.0.0.1dsn.set('driver', 'mysql');
dsn.set('user', 'root');
dsn.set('password', null);
dsn.set('port', 3306);
dsn.set('params', null);dsn.getDSN();
// mysql://root@127.0.0.1:3306/my_db````
### Build new DSN
````js
var DSNParser = require('dsn-parser');var dsn = new DSNParser();
dsn.set('driver', 'mysql')
.set('user', 'root')
.set('password', 'mypass')
.set('host', 'localhost')
.set('port', 3306)
.set('database', '7gh4d78sh2')
.set('params', {
charset: 'utf8',
strict: true
});dsn.getDSN();
// mysql://root:mypass@localhost:3306/7gh4d78sh2?charset=utf8&strict=true
````## Run tests
`npm test`
## Author
#### Jonnas Fonini
- https://fonini.github.io
## License
[](http://www.wtfpl.net)