Coder Social home page Coder Social logo

dsn-parser's Introduction

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:[email protected]: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:[email protected]: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://[email protected]: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

dsn-parser's People

Contributors

aheissenberger avatar fonini avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dsn-parser's Issues

Breaks w/a password w/a `?` in it

Attempted to use a module which relies on your module to parse DSN's, however, the database password given me involved a ?. This then broke on line 38:

var auth = split[2].split(':');

Caused I suspect by the exclusion of ? on line 28:

'(?:([^\/?#]*)@)?' + // auth

I'm honestly not sure what the best fix for this would be or I'd issue a GitHub Pull Request. Just - it bit me, and has a non-intuitive error message as a result :/.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.