1.0.1 • Published 7 years ago

connection-parser v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Logo

Connection string smart parser

connection-parser

npm coveralls deps travis

What is it

Simple module to convert URLs into set of parameters using default or custom rules. Useful for application which whant to provide single connection string instead of lot of parameters.

For example: DATABASE_URL=neo4j://user:password@server.com/database?timeout=100&nocache=true node ./bin/server.js

Install

# npm install connection-parser

Usage

Parser from string template with default options:

const createParser = require('connection-parser')

const parser = createParser('database://user:password@localhost:12345/basename?timeout=1000&params={verbose:true}')
const options = parser('database://remote-server:333/my-base?timeout=999&params={verbose:false}')
console.log(options)

will output:

{ proto: 'database:',
  host: 'remote-server',
  port: '333',
  path: '/my-base',
  options: { timeout: '999', params: { verbose: 'false' } },
  login: 'user',
  password: 'password' }

Parser described in JSON Schema format:

const createParser = require('connection-parser')

const parser = createParser({
  type: 'object',
  properties: {
    proto: {
      type: 'string',
      minLength: 1
    },
    host: {
      type: 'string',
      default: 'localhost'
    },
    port: {
      type: 'number',
      default: 12345
    },
    path: {
      type: 'string',
      default: '/'
    },
    options: {
      type: 'object',
      properties: {
        timeout: {
          type: 'number',
          default: 1000
        }
      }
    }
  }
})
const options = parser('database://remote-server:333/my-base')
console.log(options)

will convert port into integer value and add default missing parameter:

{ proto: 'database:',
  host: 'remote-server',
  port: 333,
  path: '/my-base',
  options: { timeout: 1000 } }

License

Project has a MIT license so you can innovate without restrictions. Any contribuitions are welcome.