Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
Rather naïve implementation of stringify only (no going back).
  • Loading branch information
mikl committed Aug 20, 2011
0 parents commit 5edb7e6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
@@ -0,0 +1,13 @@
hstore for Node.js
==================

[Node.js][] module for transforming JavaScript objects to [hstore][]
format as used by [PostgreSQL][].

This can be used to store key/value pairs in a format recognized by the
database, so you can query them via SQL.

[Node.js]: http://nodejs.org/
[hstore]: http://www.postgresql.org/docs/current/interactive/hstore.html
[PostgreSQL]: http://www.postgresql.org/

51 changes: 51 additions & 0 deletions hstore.js
@@ -0,0 +1,51 @@
/**
* Module for converting JavaScript objects to hstore format.
*/

var hstore = {};

"use strict";

/**
* Convert an arbitrary value to string.
*/
function convertToString(value) {
switch (typeof value) {
case 'boolean':
return String(value);
case 'null':
return 'NULL';
case 'number':
return isFinite(value) ? String(value) : 'NULL'
case 'string':
return quoteString(value);
}
}

/**
* Quote a string.
*/
function quoteString(string) {
// Naïve quoting of strings. Assumes there's not quotes in the string.
return '"' + string + '"';
}

/**
* Convert JavaScript object to hstore format.
*/
hstore.stringify = function (data) {
pairs = [];

Object.keys(data).forEach(function (key) {
var value = convertToString(data[key]);

if (value) {
pairs.push('"' + key + '"=>' + value);
}
});

return pairs.join();
};

module.exports = hstore;

17 changes: 17 additions & 0 deletions package.json
@@ -0,0 +1,17 @@
{
"author": "Mikkel Hoegh <mikkel@hoegh.org> (http://mikkel.hoegh.org/)",
"name": "hstore",
"description": "Library for transforming JavaScript objects to hstore format, used by the PostgreSQL RDBMS.",
"keywords" : ["postgres", "postgre", "database", "rdbms"],
"main" : "hstore.js",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "git://github.com/mikl/node-hstore.git"
},
"engines": {
"node": "~v0.4.10"
},
"dependencies": {},
"devDependencies": {}
}

0 comments on commit 5edb7e6

Please sign in to comment.