vitaly-t / pg-tuple

Tuples parser for PostgreSQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pg-tuple

WARNING: This project has been abandoned, for the lack of official tuple specification.

Build Status Coverage Status Join the chat at https://gitter.im/vitaly-t/pg-tuple

Parses PostgreSQL tuples, with support for:

  • composite tuples
  • arrays of tuples

Installing

$ npm install pg-tuple --save

Usage

var tuple = require('pg-tuple');

Suppose you have a custom type in your database, declared like this:

CREATE TYPE myType AS (a INT, b TEXT);

Column values of such type are received as tuple strings that need parsing.

  • parsing into an array of string values:
var data = tuple.single(value);
// data = an array of strings like this: ['1', 'text'] 
  • parsing into a proper object {a, b}:
var data = tuple.single(value, function(e, obj) {
    // `this` here refers to `obj`
    obj.a = parseInt(e[0]);
    obj.b = e[1];
});
// data = a well-parsed object like this: {a:1, b:'text'}
  • parsing columns of array type myType[]:
var data = tuple.array(value);
// data = an array of tuple strings
  • converting myType[] into an array of objects (composite parsing):
var data = tuple.array(value, function(v, index) {
    return tuple.single(v, function(e, obj) {
        obj.a = parseInt(e[0]);
        obj.b = e[1];
    });
});
// data = an array of objects: [{a:1, b:'hello'},{a:2, b:'world'}]

Testing

First, clone the repository and install DEV dependencies.

$ npm test

Testing with coverage:

$ npm run coverage

License

Copyright © 2016 Vitaly Tomilov; Released under the MIT license.

About

Tuples parser for PostgreSQL


Languages

Language:JavaScript 100.0%