GithubHelp home page GithubHelp logo

bcoe / hstore-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yeaha/hstore-js

0.0 3.0 0.0 102 KB

Postgresql hstore stringify and parse functions, support nested hstore syntax.

Home Page: http://www.postgresql.org/docs/9.3/static/hstore.html

JavaScript 100.00%

hstore-js's Introduction

hstore-js

Postgresql hstore stringify and parse functions, support nested hstore syntax.

Install

$ npm install hstore.js

Usage

Options

{
    // key: default value
    array_square_brackets: false,
    boolean_as_integer: false,
    numeric_check: false,
    root_hash_decorated: false,
    return_postgresql_expression: false
}

Stringify

var hstore = require('hstore.js');

var data = {a: 1, b: null, c: true, d: false, e: '"', f: "'", g: '\\', h: '=>'};
var encoded = hstore.stringify(data, {boolean_as_integer: true});

// "a"=>1,"b"=>NULL,"c"=>1,"d"=>0,"e"=>"\"","f"=>"'","g"=>"\\","h"=>"=>"
console.log(encoded);

var expr = hstore.stringify(data, {boolean_as_integer: true, return_postgresql_expression: true});
var sql = 'select '+ expr;

// select '"a"=>1,"b"=>NULL,"c"=>1,"d"=>0,"e"=>"\"","f"=>"''","g"=>"\\","h"=>"=>"'::hstore
console.log(sql);

Parse

var hstore = require('hstore.js');

var hstore_string = '"a"=>"1", "b"=>NULL, "c"=>"\\"", "d"=>"\'", "e"=>"\\\\", "f"=>"=>"';
var data = hstore.parse(hstore_string);

// { a: '1', b: null, c: '"', d: '\'', e: '\\', f: '=>' }
console.log(data);

data = hstore.parse(hstore_string, {numeric_check: true});

// { a: 1, b: null, c: '"', d: '\'', e: '\\', f: '=>' }
console.log(data);

Nested hstore

Support nested hstore syntax in this PDF

Stringify

var hstore = require('hstore.js');

var data = [
    {a: 1},
    [1, 2, 3],
    {c: ['d', 'f']}
];

var encoded = hstore.stringify(data);

// {"a"=>1},{1,2,3},{"c"=>{"d","f"}}
console.log(encoded);

var options = {array_square_brackets: true}
encoded = hstore.stringify(data, options);

// {"a"=>1},[1,2,3],{"c"=>["d","f"]}
console.log(encoded);

Parse

var hstore = require('hstore.js');

var hstore_string = '"a"=>1, "b"=>{"c"=>3, "d"=>{4, 5, 6}}';

// { a: 1, b: { c: 3, d: [ 4, 5, 6 ] } }
console.log(hstore.parse(hstore_string));

hstore_string = 'a,b,3,4,5';
// [ 'a', 'b', 3, 4, 5 ]
console.log(hstore.parse(hstore_string));

hstore-js's People

Contributors

yeaha avatar

Watchers

 avatar  avatar  avatar

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.