Browse files

Initial version

Rather naïve implementation of stringify only (no going back).
  • Loading branch information...
0 parents commit 5edb7e63303a109e25a574c1de816605e363027d @mikl committed Aug 20, 2011
Showing with 81 additions and 0 deletions.
  1. +13 −0 README.md
  2. +51 −0 hstore.js
  3. +17 −0 package.json
13 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 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 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.