JSPM

dsn-parser

1.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3486
  • Score
    100M100P100Q110683F
  • License WTFPL

Parse and create database connection strings fluently

Package Exports

  • dsn-parser

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dsn-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

DSN Parser for Node

Parse and create database connection strings fluently.

Use case

I've setup a node app on Heroku 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 package:

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

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

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

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

License

Licensed under the WTFPL